Java for循环格式化控制台

时间:2018-09-26 05:50:40

标签: java loops

我确定我的问题的标题可能会更好,因此请提前对不起。我有一个Java项目,我正在尝试分析字符串中的文本。我想计算3个数字并将其相加,计算以a开头的单词并显示它。计算以“ s”或“ s”结尾的词,计算总词数,最后计算句子。到目前为止,我已经能够做所有事情,除非我去运行程序,否则显示非常混乱。希望将每个部分放在一起,但由于某种原因,我无法做到这一点。

import java.util.Arrays;

class TextAnalysisHW {

public static void main(String[] args) {

    String text = "This is a long string that   contains numbers like 100, 34.0, 21 and odd symbols like &, %, #. We are supposed to determine how many words this string has in it and also the number of sentences. We are also required to determine how many numbers are in it and sum them all up. Finally, we must count and display all the words that end with  a 's' and start with an 'a'.";


    // create a copy of string
    String temp = text;


    // convert text to lowercase
    temp = temp.toLowerCase();



    // split tokens by one or more spaces
    String [] tokens = temp.split(" +");



    int j;
    int numCount = 0;
    double total = 0;
    int sentCount = 0;

    for (j=0;j<tokens.length;j=j+1) 
        {

            // check to see if token ends with a comma
            if (tokens[j].endsWith(",")==true) 
            {
            // get rid of the comma
                tokens[j]=tokens[j].replaceAll(",","");
            }
            // otherwise check to see if it ends with a period
            else if (tokens[j].endsWith(".")) 
            {
                tokens[j]=tokens[j].replaceAll("\\.","");
                sentCount = sentCount + 1;
            }
            if (tokens[j].matches("-?[0-9]+|-?[0-9]*\\.[0-9]+"))
            {
                System.out.println("  Number found: " + tokens[j]);
                numCount++;
                // Convert number into a Double
                double num = Double.parseDouble(tokens[j]);
                total = total + num;
            }

               if(tokens[j].startsWith("a"))
                {
                    //  Print that token and count it 
                    System.out.println("Word starts with a:  "+tokens[j]);

                }
        }

    System.out.println("Sum of the 3 three numbers: " + total);

    System.out.println((tokens.length - numCount) + " words");

    System.out.println(sentCount +" sentences" );

    // main
}
// class

}

代码一直显示如下:

    Word starts with a:  a
    Number found: 100
    Number found: 34.0
    Number found: 21
    Word starts with a:  and
    Word starts with a:  are
    Word starts with a:  and
    Word starts with a:  also
    Word starts with a:  are
    Word starts with a:  also
    Word starts with a:  are
    Word starts with a:  and
    Word starts with a:  all
    Word starts with a:  and
    Word starts with a:  all
    Word starts with a:  a
    Word starts with a:  and
    Word starts with a:  an
    Sum of the 3 three numbers: 155.0
    71 words
    4 sentences

所以我的问题是如何使第一行显示与“ Word以a:开头”的其余部分保持一致。当我尝试添加“以s结尾的单词”时,它与其他部分混在一起。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

在for循环之前,创建两个列表来存储单词和数字:

ArrayList<String> numbersFound = new ArrayList<>();
ArrayList<String> prefixAFound = new ArrayList<>();

在if语句中,不要打印出找到的内容,而是先将它们添加到列表中:

if (tokens[j].matches("-?[0-9]+|-?[0-9]*\\.[0-9]+"))
{
    numbersFound.add(tokens[j])
    numCount++;
    double num = Double.parseDouble(tokens[j]);
    total = total + num;
}

if(tokens[j].startsWith("a"))
{
    prefixAFound.add(tokens[j])

}

在for循环之后,再添加两个for循环,以首先打印找到的所有数字,然后打印所有以A开头的单词:

for (String number : numbersFound) {
    System.out.println("Number Found: " + number);
}

for (String prefixA : prefixAFound) {
    System.out.println("Word starts with a: " + prefixA);
}