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

StackOverflow https://stackoverflow.com/questions/21971079

  •  15-10-2022
  •  | 
  •  

Вопрос

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.

Это было полезно?

Решение

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!

Другие советы

Looks like some of your code may be in the wrong place. You might use the strategy:

  1. Loop through all answers, and set id of winner (closest without going over)
  2. AFTER the loop, then print the results. (Your code has the process winner inside the loop

NoSuchElementException is thrown because you are attempting to read past the end of your data. You should call the hasNext() method on Scanner before reading the next value. In your case, it looks like you're expecting the input to alternate between 'id' and 'answer' but you're getting out of sync because you don't always read the next id before reading the next answer.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top