ThirtyOneTwentySeven
ThirtyOneTwentySeven

Reputation: 340

!= 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

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. 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); 
}
}

Upvotes: 0

Views: 6566

Answers (2)

Raffaele
Raffaele

Reputation: 20885

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.

Upvotes: 2

eric.m
eric.m

Reputation: 1612

You can check if there is more input with 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

Upvotes: 2

Related Questions