Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java scanNextInt()忽略空行_Java - Fatal编程技术网

Java scanNextInt()忽略空行

Java scanNextInt()忽略空行,java,Java,我使用以下代码: public String processFile(Scanner scanner) { String result = ""; SumProcessor a = new SumProcessor(); AverageProcessor b = new AverageProcessor(); String line = null; while (scanner.hasNext()) { if (scanner.hasNe

我使用以下代码:

public String processFile(Scanner scanner) {
    String result = "";
    SumProcessor a = new SumProcessor();
    AverageProcessor b = new AverageProcessor();
    String line = null;
    while (scanner.hasNext()) {

        if (scanner.hasNext("avg") == true) {

            c = scanner.next("avg");
           while(scanner.hasNextInt()){

                int j = scanner.nextInt();
                a.processNumber(j);
            }
           System.out.println("Exit a");
            result += a.getResult();
            a.reset();
        }
        if (scanner.hasNext("sum") == true) {

            c = scanner.next("sum");
           while(scanner.hasNextInt()){

          int j = scanner.nextInt();
                b.processNumber(j);                    
            }
            System.out.println("Exit b");
             result += b.getResult();
             b.reset();
        }

    }
    return result;
}
当我按enter键或send空行时,我需要结束while循环(hasNexInt())

我尝试使用String==null的一些方法,但Java只是忽略空行

输出

run:
avg
1
2
3
4

sum
Exit a
1
2
3
4
但我需要:

run:
avg
1
2
3
4

Exit a
sum    
1
2
3
4

只需使用以下内容:

String line = null;
while(!(line = keyboard.nextLine()).isEmpty()) {
// Your actions
}

只需使用以下内容:

String line = null;
while(!(line = keyboard.nextLine()).isEmpty()) {
// Your actions
}

只需添加
scanner.nextLine()
即可忽略行中的其余条目,如下所示:

            while (scanner.hasNextLine()){
                String line = scanner.nextLine();
                if("".equals(line)){
                   //exit out of the loop
                   break;
                }
                //assuming only one int in each line
                int j = Integer.parseInt(line);
                a.processNumber(j);
            }

只需添加
scanner.nextLine()
即可忽略行中的其余条目,如下所示:

            while (scanner.hasNextLine()){
                String line = scanner.nextLine();
                if("".equals(line)){
                   //exit out of the loop
                   break;
                }
                //assuming only one int in each line
                int j = Integer.parseInt(line);
                a.processNumber(j);
            }
在第二个
while
循环中使用
hasNextInt()
。当您没有传递
int
值时,while循环将中断

或者,您也可以确定一个特定的值,您可以通过该值来中断循环。例如,您可以传递字符
“x”
,然后检查是否传递了“x”->中断循环

while (scanner.hasNext()) {
        if (scanner.hasNext("avg") == true) {

            c = scanner.next("avg");
           while (scanner.hasNextInt()){ //THIS IS WHERE YOU USE hasNextInt()                       
                int j = scanner.scanNextInt();
                a.processNumber(j);
            }
           System.out.println("End While");
            result += a.getResult();
            a.reset();
        }
在第二个
while
循环中使用
hasNextInt()
。当您没有传递
int
值时,while循环将中断

或者,您也可以确定一个特定的值,您可以通过该值来中断循环。例如,您可以传递字符
“x”
,然后检查是否传递了“x”->中断循环

while (scanner.hasNext()) {
        if (scanner.hasNext("avg") == true) {

            c = scanner.next("avg");
           while (scanner.hasNextInt()){ //THIS IS WHERE YOU USE hasNextInt()                       
                int j = scanner.scanNextInt();
                a.processNumber(j);
            }
           System.out.println("End While");
            result += a.getResult();
            a.reset();
        }

如果应用程序中扫描仪的使用不是绝对必须的,我可以提供:

BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
for(;;) {
    String lile = rdr.readLine();
    if (lile.trim().isEmpty()) {
        break;
    }
    // process your line
}

这段代码肯定会在控制台的空行中停止。现在您可以使用Scanner进行行处理或正则表达式。

如果您的应用程序中不一定要使用Scanner,我可以提供以下服务:

BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
for(;;) {
    String lile = rdr.readLine();
    if (lile.trim().isEmpty()) {
        break;
    }
    // process your line
}

这段代码肯定会在控制台的空行中停止。现在您可以使用Scanner进行行处理或正则表达式。

我不需要忽略空行,我不需要忽略空行line@VladislavIl'ushin我的意思是,它将忽略您与nextInt()一起输入的新行字符。我进一步改进了答案,在最后忽略新行字符之前读取该行的所有
int
类型输入。@VladislavIl'ushin这有帮助吗?还是仍然存在问题?如果有问题,请突出显示,什么不起作用?我需要在内部循环时结束,因为我在内部循环后处理数字break@VladislavIl'ushin但是您的
processNumber
方法只接受一个int作为参数。我不需要忽略空行,我不需要忽略空行line@VladislavIl“我是说,它将忽略与nextInt()一起输入的新行字符。我进一步改进了答案,在最后忽略新行字符之前读取该行的所有
int
类型输入。@VladislavIl'ushin这有帮助吗?还是仍然存在问题?如果有问题,请突出显示,什么不起作用?我需要在内部循环时结束,因为我在内部循环后处理数字break@VladislavIl'ushin,但是您的
processNumber
方法只接受一个int作为参数。我尝试以下方法:)请参阅我的固定代码。但只有当我在
avg
之后键入
sum
avg
sum
@VladislavIl'ushin之后键入
sum
时,它才是中断的。您是否尝试过String.equals(“”)?我想我明白你想做什么了。只需在控制台输入中输入一个空字符串,然后在while循环中检查字符串是否等于(“”)。如果是,则打破循环。希望这些信息有帮助!我试试这个:)看看我的固定代码。但只有当我在
avg
之后键入
sum
avg
sum
@VladislavIl'ushin之后键入
sum
时,它才是中断的。您是否尝试过String.equals(“”)?我想我明白你想做什么了。只需在控制台输入中输入一个空字符串,然后在while循环中检查字符串是否等于(“”)。如果是,则打破循环。希望这些信息有帮助!