如何摆脱那个讨厌的领先加号?

时间:2015-12-02 19:54:32

标签: java arrays java.util.scanner

我有一个基本上从文本文件中读取的代码。输出时有一个前导加号,因为答案是循环打印的。如何摆脱那个奇异的领先加号?我所能想到的只是将整个过程转换为字符串,然后取出前三个索引,但这太复杂了。是否有一种简单的方法可以重新排列逻辑并完成它?

我的代码:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package importtextfiles;

/**
 *
 * @author Hana
 */
import java.io.*;
import java.util.*;
public class InputNumData {
    public static void main(String args[]) throws IOException
    {
        Scanner sf = new Scanner(new File("C:\\Users\\Hana\\SkyDrive\\CompSci\\Programming\\importTextFiles\\meow.txt"));
        int maxIndx = -1; //so the first index is 0

        String text[] = new String[1000];

        while(sf.hasNext()){
            maxIndx++;
            text[maxIndx] = sf.nextLine();
            System.out.println(text[maxIndx]);
        }

        sf.close();

        String answer = "";
        int sum;

        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;
            }
            answer = answer + " = " + sum;
            System.out.println(answer);
        }
    }
}

我的输出:

run:
12 10 3 5
18 1 5 92 6 8
2 9 3 22 4 11 7
 + 12 + 10 + 3 + 5 = 30
 + 18 + 1 + 5 + 92 + 6 + 8 = 130
 + 2 + 9 + 3 + 22 + 4 + 11 + 7 = 58
BUILD SUCCESSFUL (total time: 0 seconds)

meow.txt:

12 10 3 5
18 1 5 92 6 8
2 9 3 22 4 11 7

3 个答案:

答案 0 :(得分:5)

只需将此行更改为

即可
answer = answer.isBlank() ? i : answer + " + " + i;

有关其工作原理的详细信息,请参阅this

答案 1 :(得分:1)

进行while循环,并修复第一个值:

//Set up first value
int i = sc.nextInt();  //might want to check for hasNext() here 
answer = i;
sum = sum + i;

while(sc.hasNext())
{
   i = sc.nextInt();
   answer = answer + " + " + i;
   sum = sum + i;
}

答案 2 :(得分:1)

首先,不要在循环中使用连接。类似的东西:

String result = ""
for (...) {
    result = result + "some additonal data";
}

创建几个中间字符串对象,这是不好的做法。它应该替换为:

StringBuilder sb = new StringBuilder();
for (...) {
    sb.append( "some additional data" );
}
result = sb.toString();

允许您在完成追加之前添加字符串而不创建新的字符串对象。

现在我们正在使用StringBuilder,您可以为初始加问题提供多种解决方案。第一个,也适用于非推荐的字符串连接,是保留一个标志,告诉你这是否是“第一个操作数”。将您的while循环更改为:

StringBuilder sb = new StringBuilder();
boolean firstOperand = true;

while(sc.hasNext()){
    int i = sc.nextInt();
    if ( firstOperand ) {
        firstOperand = false;
    } else {
        sb.append( " + " );
    }
    sb.append( i );
    sum = sum + i;
}
answer = sb.toString();

使用StringBuilder可能的另一种方法是在完成循环后删除额外的" + "。在这种情况下,最好在每个操作数之后添加" + " ,以便额外的操作数将在最后。从StringBuilder的末尾删除比从其开头删除更有效:

StringBuilder sb = new StringBuilder();

while(sc.hasNext()){
    int i = sc.nextInt();
    sb.append( i ).append( " + " );
    sum = sum + i;
}
if ( sb.length() > 0 ) {
    sb.setLength( sb.length() - 3 );
}
answer = sb.toString();