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.