简体   繁体   English

在Java中中断文件txt

[英]break file txt in java

my data in notepad 1,2,3,4,5,6,7,8,9,10... why when str ==5 using break does not good work, my data is supposed stop when in numbers 5 ...just display 1,2,3,4,5,6,7,8,9,10... 我在记事本1,2,3,4,5,6,7,8,9,10...数据1,2,3,4,5,6,7,8,9,10...为什么当str ==5使用break不好用,我的数据应该在数字5时停止。 。只显示1,2,3,4,5,6,7,8,9,10...

 Scanner sc = new Scanner (new File("c:/Users/ASUS/Desktop/Numbers.txt"));
            while (sc.hasNextInt()){
                int str = sc.nextInt();
                for (int i=0; i<str; i++){
                    if (str == 5);
                    break;
                }

                System.out.print(str+ " ");
            }       
            sc.close();
        }
    }

You need to get rid of the ; 您需要摆脱;; after if(str==5); if(str==5); There should be no ;. 应该没有;。

for (int i=0; i<str; i++){
    if (str == 5)
    {
        break;
    }
}

The thing is your loop isn't actually doing any processing. 关键是您的循环实际上没有进行任何处理。 You are looping a max of 6 times but you aren't doing anything in your loop. 您最多可以循环6次,但循环中什么也没做。 You aren't changing your output at all the loop is completely useless in this case... 您根本不需要更改输出,在这种情况下,循环是完全没有用的...

You are just breaking out of the for loop not the while loop. 您只是打破了for循环,而没有while循环。 If you want to break out of the while loop you should do like this: 如果要打破while循环,您应该这样做:

Scanner sc = new Scanner (new File("c:/Users/ASUS/Desktop/Numbers.txt"));
            while (sc.hasNextInt()){
                int str = sc.nextInt();
                System.out.print(str+ " ");
                if (str == 5)
                {
                   break;
                }
            }       
            sc.close();
        }
}

You could just change your code to be easier and do: 您可以将代码更改为更简单并执行以下操作:

 Scanner sc = new Scanner (new File("c:/Users/ASUS/Desktop/Numbers.txt"));
            int str = sc.nextInt();
            while (sc.hasNextInt() && str != 6){
                System.out.print(str+ " ");
                str = sc.nextInt();
            }       
            sc.close();
        }
    }

Just to respond with the example, the ; 仅以示例为例; after your if ends that if statement. 您的if结束之后,如果if语句。 Use braces {} 使用花括号{}

if(str==5){
    break;
}

so: 所以:

 Scanner sc = new Scanner (new File("c:/Users/ASUS/Desktop/Numbers.txt"));
        while (sc.hasNextInt()){
            int str = sc.nextInt();
                if (str == 5){
                    break;
                }

            System.out.print(str+ " ");
        }       
        sc.close();
    }
}

An alternative way to do it would be to load the entire line, split on , and then iterate through the following array and print out if there is a 5 and break if you find it 另一种方法是加载整个行,在上拆分,然后遍历以下数组,并打印出5,如果找到则中断

I think the solution is to loop as long as the number has not been found. 我认为解决方案是在没有找到该数字的情况下循环。

boolean foundFive = false;
Scanner sc = new Scanner (new File("c:/Users/ASUS/Desktop/Numbers.txt"));
while (sc.hasNextInt() && !foundFive) {
    int number = sc.nextInt();
    System.out.print(number+ " ");
    if (number == 5) {
        foundFive = true;
    }
}       
sc.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM