我的问题是:
数组限制为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();
}
}
答案 0 :(得分:0)
根据最新评论,以下内容对您有用 - 请注意我没有处理错误检查等。
public static void fileProcessing() throws FileNotFoundException {
String reading = "28 6\n9 5\n2000 2001\n0\n1 23 4";
Scanner lineByLine = new Scanner(reading); // reads lines by line to
// count the total lines
String lines = "";
while (lineByLine.hasNextLine()) {
lines = lineByLine.nextLine();
String[] nums = lines.split(" ");
StringBuilder sb = new StringBuilder();
BigInteger sum = new BigInteger("0");
for(String num : nums){
sb.append("+").append(num);
sum = sum.add(new BigInteger(num));
}
System.out.println(sb.toString().substring(1)+"="+sum);
}
}