简体   繁体   English

Java循环中的!= null

[英]!= null in a java while loop is not working

So I'm doing Usacogate's Greedy Gift Givers and using a while loop that checks if the next line in the file is null or not, specifically 因此,我正在做Usacogate的Greedy Gift Givers,并使用while循环检查文件中的下一行是否为空,特别是

while((tempName = sc.nextLine()) != null){

The issue is, once it has read the last line and returns up to the top to check if there is a next line, Java throws the NoSuchElementException instead of seeing that there is no line and exiting the loop. 问题是,一旦读取了最后一行并返回到顶部以检查是否存在下一行,Java就会抛出NoSuchElementException而不是看到没有行并退出循环。 All the other parts work, and the code returns the correct answers. 所有其他部分都起作用,并且代码返回正确的答案。

How can I fix this problem? 我该如何解决这个问题?

public static void main(String [] args) throws IOException {
        Scanner sc = new Scanner(new File("gift1.in"));
        int n = sc.nextInt();
         System.out.printf("n is: %d\n", n);

        String[] names = new String[10];
        sc.nextLine();
        int[] account = new int[10];
 for (int i = 0; i < n; i++) {
            names[i]=sc.nextLine().trim();
            System.out.printf("current Name  is: %s\n", names[i]);

            account[i]=0;  
 }
 String tempName; 
 while((tempName = sc.nextLine()) != null){
     System.out.printf("tempName is: %s\n", tempName.trim());

     int indexDummy = -1;
     for (int j=0; (j< names.length); j++){
            if (names[j].equals(tempName)) {
                indexDummy = j;
                break;
            }
     }
     System.out.printf("indexDummy is: %d\n", indexDummy);


     String nextLine = sc.nextLine();
     System.out.printf("Next line is: %s\n", nextLine);
     StringTokenizer st = new StringTokenizer(nextLine);
     int int1 = Integer.parseInt(st.nextToken());    
     int int2 = Integer.parseInt(st.nextToken()); 

     if(int2 == 0) {
        for(int i=0; i<n;i++){
            System.out.println(names[i]+" "+account[i] );
        }
        continue;
     }
     int quotient = int1 / int2;
     int tempNum = int2 * quotient;
     int remainder = int1-tempNum;
     System.out.printf("quotient and remainder are: %d %d\n", quotient, remainder);


     account[indexDummy]=account[indexDummy]+remainder-int1;//parsing the two numbers
     System.out.println(indexDummy);
     System.out.println(names[indexDummy]+" has "+account[indexDummy]);
     for (int k=0;k < int2 ;k++){
         tempName = sc.nextLine();
         for (int j=0; j< names.length; j++){
                if (names[j].equals(tempName.trim() ) ) {
                    //indexDummy2 = j;
                    account[j] =account[j]+ quotient;
                    System.out.printf("%s has balance  %d\n", names[j], account[j]);
                    break;
                }

            }   //sort out the output        
        }

     //tempName = sc.next();             
 }

 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
for(int i=0; i<n;i++){
    out.println(names[i]+" "+account[i] );
}
for(int i=0; i<n;i++){
    System.out.println(names[i]+" "+account[i] );
}
out.close();                                  
System.exit(0); 
}
}

You can check if there is more input with Scanner.hasNextLine() . 您可以使用Scanner.hasNextLine()检查是否有更多输入。

while(sc.hasNextLine() && (tempName = sc.nextLine()) != null) {

Or: 要么:

while(sc.hasNextLine()) {
    tempName = sc.nextLine(); //if you know it's not going to have a null as input

This is how you are supposed to use a scanner 这就是您应该使用扫描仪的方式

while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
}

and you can safely skip the null check because nextLine() returns a non-null object. 并且您可以放心地跳过null检查,因为nextLine()返回一个非null对象。

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

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