Java 额外的+;在我的节目上签名

Java 额外的+;在我的节目上签名,java,Java,我为我的计算机科学课写了一个程序,它读取一个文件并导入数据,然后只添加数字,但似乎添加了一个额外的加法符号 import java.io.*; //necessary for File and IOException import java.util.*; //necessary for Scanner public class Tester { public static void main( String args[] ) throws IOException { Scann

我为我的计算机科学课写了一个程序,它读取一个文件并导入数据,然后只添加数字,但似乎添加了一个额外的加法符号

import java.io.*; //necessary for File and IOException 
import java.util.*; //necessary for Scanner
public class Tester
{
public static void main( String args[] ) throws IOException
   {
    Scanner sf = new Scanner(new File("/Volumes/DVLUP Flash/Numbers.txt"));

    int maxIndx = -1; //-1 so when we increment below, the first index is 0
    String text[] = new String[1000]; //To be safe, declare more than we
        while(sf.hasNext( ))
        {
            maxIndx++;
            text[maxIndx] = sf.nextLine( ); 
            //System.out.println(text[maxIndx]); //Remove rem for testing
        }
        sf.close();

        for(int j =0; j <= maxIndx; j++)
        {
            Scanner sc = new Scanner(text[j]);
        }
        String answer = ""; //We will accumulate the answer string here. 
        int sum; //accumulates sum of integers
        for(int j = 0; j <= maxIndx; j++)
        {
            Scanner sc = new Scanner(text[j]);
            sum = 0;
            answer = ""; 
            while(sc.hasNext())
            {
                int i = sc.nextInt();
                answer = answer + i + " + ";
                sum = sum + i; 
            }
            //sc.next();
            answer = answer + " = " + sum; 
            System.out.println(answer);
        } 
    }
}

最后一个数字后面有一个额外的数字,如何解决这个问题?

一个选项是在附加每个数字之前有条件地加上一个加号(第一个数字除外):

answer = ""; 
while(sc.hasNext()) {
    int i = sc.nextInt();
    if (answer.length() > 0) {
        answer += " + ";
    }
    answer = answer + i;
    sum = sum + i; 
}
之后


在上一次迭代之后,您将得到一个“额外”加号,因为这是您打印它的方式。在while循环中可以看到,
字符串的结尾是
+

要更改它,请在值之前添加
+
,如下所示:

if(sc.hasNext()) {
   int i = sc.nextInt();
   answer = i + "";
   sum += i;
   while(sc.hasNext())
      {
          i = sc.nextInt();
          answer = answer + " + " + i;
          sum = sum + i; 
      }
}
或者,如果您使用Java8,您可以使用
StringJoiner
作为

StringJoiner joiner = new StringJoiner(" + ");
while(sc.hasNext())
{
    i = sc.nextInt();
    // This automaticly includes a " + " between the values.
    joiner.add(String.valueOf(i));
    sum = sum + i; 
}

OP甚至可以用集合替换输入数组,并使用流+连接符。->没有邪恶的while循环。请注意,您正在混合成对的
扫描器
方法:如果要调用
sf.nextLine()
,请检查是否有下一行带有
sf.hasNextLine()
,而不是
sf.hasNext()
;类似地,在调用
nextInt()
之前调用
hasNextInt()
,而不是
hasNext()
answer = answer.substring(0, answer.length()-1);
if(sc.hasNext()) {
   int i = sc.nextInt();
   answer = i + "";
   sum += i;
   while(sc.hasNext())
      {
          i = sc.nextInt();
          answer = answer + " + " + i;
          sum = sum + i; 
      }
}
StringJoiner joiner = new StringJoiner(" + ");
while(sc.hasNext())
{
    i = sc.nextInt();
    // This automaticly includes a " + " between the values.
    joiner.add(String.valueOf(i));
    sum = sum + i; 
}