私の問題は:javaで配列要素を追加するには?
配列の制限は25です。私はそれをどのように追加しますか?私はArrayList
のinteger.parseInt、append、およびStringbuilderを使うべきではない。それはまたfileReading部分を持っています。
ファイルの内容は以下のようにフォーマットされています。以下は
28 6
9 5
2000 2001
0
1 23 4
が予想される出力です:
28 + 6 = 34
9 + 5 = 14
2000 + 2001 = 4001
0 = 0
1 + 23 + 4 = 28
私は助け要素を追加する必要があります。これまでのところ、私のコードです。
以下の最新のコメントをもとにimport java.io.*;
import java.util.*;
public class Sum {
public static final int arbitraryNum = 25; // class constant for setting constant values
// main method
public static void main(String[] args) throws FileNotFoundException {
fileProcessing(); // call the file reading method
}
//reads from a given file and then passes it to the next method.
public static void fileProcessing() throws FileNotFoundException{
File reading = new File("sum.txt"); // file call
Scanner lineByLine = new Scanner(reading); // reads lines by line to count the total lines
Scanner wordByWord = new Scanner(reading);
Scanner console = new Scanner(reading); // total number of tokens
Scanner tokensInLine = new Scanner(reading); //number
int totalLines = 0;
int a = 0;
int b = 0;
int tokenCount = 0;
String lines = "";
String words = "";
String tokens ="";
String[] token;
String[] lineStore = null;
while(lineByLine.hasNextLine()){
totalLines++;
lines = lineByLine.nextLine();
}
lineStore = new String[totalLines];
while(tokensInLine.hasNextLine()){
tokens = tokensInLine.nextLine();
totalLines++;
lineStore[b++] = tokens;
}
while(console.hasNext()){
tokens = console.next();
tokenCount++;
}
token = new String[tokenCount];
while(wordByWord.hasNext()){
words = wordByWord.next();
token[a++] = words;
}
leadingZeros(token, lineStore);
System.out.println("Total lines = " + totalLines);
System.out.println("Token count = " + tokenCount);
}
// adds leading zeros.
public static String leadingZeros(String[] tokens, String[] Line){
String[] number = tokens;
String s = "";
for(int i = 0; i < tokens.length; i++){
s= "0000000000000000000000000"+number[i];
String[] hello = {s.substring(s.length()-25)} ;
StringToIntConversion(tokens, (s.substring(s.length()-25)), Line);
// System.out.println(s.substring(s.length()-25));
}
return s;
}
//converts the string to integer meaning, string ====> char ====> int
public static int[] StringToIntConversion(String[] tokens, String num, String[] line) {
int[] a = new int[arbitraryNum];
int sum = 0;
for(int v = 0; v < arbitraryNum; v++){
a[v] = Character.getNumericValue(num.charAt(v)) ;
sum += a[v];
//System.out.print(sum);
}
System.out.println(Arrays.toString(a)); // to show array structure
//
//int[][] add = new int[line.length][tokens.length];
//for(int r = 0; r < add.length; r++){
// for(int p = 0; p < add[0].length; p++){
// for(int v = 0; v < arbitraryNum; v++){
// add[r][p] = Character.getNumericValue(num.charAt(v));
// System.out.print(add[r][p] + sum);
// }
// System.out.println();
// }
//}
//
addition(a, tokens, line);
return a;
}
// adds data from the previous method
public static void addition(int[] kilo, String[] token, String[] lineStore){
int sum = 0;
for(int a = 0; a < arbitraryNum; a++){
//how should i add
enter code here
}
System.out.println();
}
}
あなたが必要な大きさの新しい配列、コピーを宣言古いものからの内容を取得し、新しい配列を古い変数に代入します。 – MadProgrammer
@MadProgrammer --- StringToIntConversion()の後の出力は、ex:00000000 ... 6(6の前に24のゼロ)のようになります。 - 0,0,0、...、6。しかし、助けてくれてありがとう。 – TeslaCarsForLife
私はその数とその次の数の和を考えましたが、少し混乱しました。 '0 = 0 1 + 23 + 4 = 28'部分について説明できますか? –