Search code examples
javajcreator

Java Beginner: Message coming up under General output in java although process was completed


I wrote a program to display the id number of a person who guesses the closest number of marbles in jar. This is the program:

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class SuperMarbles
{
  public static void main(String[] args) throws IOException
  {
    int guess = 0;
    int contestants = 0;
    int num_marbles, answer, id, winner = 0;

    Scanner scanner = new Scanner(new FileReader("promo.txt"));
    PrintWriter printWriter = new PrintWriter(new FileWriter("winner.txt"));

    num_marbles = scanner.nextInt();
    id = scanner.nextInt();
    while (id != 0)
    {
      answer = scanner.nextInt();
      if (answer > guess && answer <= num_marbles)
      {
        guess = answer;
        winner = id;
        id = scanner.nextInt();
        contestants++;
      }
    }

    if (guess == 0)
    {
      printWriter.printf("There are %d contestants", contestants);
      printWriter.printf("\nThere are no winners");
    }
    else
    {
      printWriter.printf("There are %d contestants", contestants);
      printWriter.printf("\nThe winner is ID#%d", winner);
    }
    scanner.close();
    printWriter.close();

  }

}

This is the data in the input file "promo.txt":
258
0001 200
0002 198
0003 430
0004 12
0005 30
0006 45
0007 154
0008 250
0009 120
0

Now, I use JCreator, when I try to compile, under 'Build Output', I am getting no errors and 'Process Completed' but under 'General Output', this is what I'm getting:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at SuperMarbles.main(SuperMarbles.java:25)

Process completed.

Also, the output file "winner.txt" is not being created. I'm quite a newbie when it comes to java, but any help would be greatly appreciated.


Solution

  • I've added some debugging to your code. Take a look at what it shows us:

    ...
    
    num_marbles = in.nextInt();
    System.out.println("num_marbles=" + num_marbles);
    id = in.nextInt();
    System.out.println("id=" + id);
    while (id!=0) {
        answer = in.nextInt();
        System.out.println("answer=" + answer);
    
    ...
    

    Output:

    [C:\java_code\]java SuperMarbles
    num_marbles=258
    id=1
    answer=200
    answer=198
    answer=3
    answer=430
    answer=4
    answer=12
    answer=5
    answer=30
    answer=6
    answer=45
    answer=7
    answer=154
    answer=8
    answer=250
    answer=120
    answer=0
    Exception in thread "main" java.util.NoSuchElementException
        at java.util.Scanner.throwFor(Unknown Source)
        at java.util.Scanner.next(Unknown Source)
        at java.util.Scanner.nextInt(Unknown Source)
        at java.util.Scanner.nextInt(Unknown Source)
        at SuperMarbles.main(SuperMarbles.java:16)
    

    You've gotten all the input correctly, but you keep trying to get it even when there's no more to get.

    You need to stop when in.hasNext() is false. But you're not testing for that at all.

    In addition, if you change the last debugging output to

    System.out.println("answer=" + answer + ", id=" + id);
    

    you'll find that id never equals 0, and therefore your while loop will never end.

    There may be other problems, but these are two good ones to start with. Good luck!