1

So I'm working on this energy tree project for my computer programming class, I was trying to make a do-while loop that asked the user if they were a producer or consumer and if any other input was entered loop would start over. My code keeps getting the error

"java.util.NoSuchElementException" on line 45 

where it says

char Type = ProducerOrConsumer.nextLine().toUpperCase().charAt(0);


        boolean isChoiceValid = false;

        do
        {
            PETCScanner.close();
            Scanner ProducerOrConsumer = new Scanner(System.in);

            System.out.println("Are you a Producer or a Consumer?\\\\n (P = Producer, C = Consumer");
            char Type = ProducerOrConsumer.nextLine().toUpperCase().charAt(0);
            if (Type == 'P' || Type =='C')
            {


            }
            else 
                System.out.println("Sorry try Again");
            {

            }
            String Name = ProducerOrConsumer.nextLine().toUpperCase();

            System.out.println(Name);


        }while(isChoiceValid == (true));
        System.out.println("You're Done!");


    }
2
  • 1
    Why do you have {} outside the else? Commented Jan 16, 2020 at 2:32
  • Is there any way you can post code that will compile without change, and that will reproduce the error? Commented Jan 16, 2020 at 3:01

3 Answers 3

1

It seems you are closing a Scanner, which closes all attached resources. When trying to read again, you have "no such element" to read.

You only need one scanner per application, there is no need to re-create one, too.

Scanner sc = new Scanner(System.in);
boolean isChoiceValid = false;
do {
     System.out.println("Are you a Producer or a Consumer?\\\\n (P = Producer, C = Consumer");
    char type = sc.nextLine().toUpperCase().charAt(0);
    if (type == 'P' || type =='C') {
        boolean isChoiceValid = true;
    } else {
        System.out.println("Sorry try Again");
        isValidChoice = false;  // if you want
        continue;
    }
    if (sc.hasNextLine()) {
        String name = sc.nextLine().toUpperCase();
        System.out.println(name);
    }
} while(!isChoiceValid);
System.out.println("You're Done!");
Sign up to request clarification or add additional context in comments.

Comments

0

I saw that the exception was thrown from the calling ProducerOrConsumer.nextLine().

There was no element found as the result == null. You can have a look more detailed in the java.util.Scanner.nextLine()

public String nextLine() {
        ...
        if (result == null)
            throw new NoSuchElementException("No line found");
        ...
        if (result == null)
            throw new NoSuchElementException();
        ...
}

Best,

Comments

0

After removing PETCScanner.close(); I ran your code, and I don't get any errors. I get this:

Are you a Producer or a Consumer?\\n (P = Producer, C = Consumer
Neither
Sorry try Again
O
O
You're Done!

5 Comments

Well, PETCScanner is never defined in the question, so how did you compile it?
I added import java.util.Scanner; and public class testing7 { and public static void main(String[] args) { I then commented out // PETCScanner.close(); Added a } as the end (closing the class). Then I used javac .. really quite simple.
We're just trying to understand... Read the code, think about it... It's a while loop. The variable is never changed. Why does the loop stop?
There are two kinds of while loops. One is while (condition) {doSomething} here the condition is checked in the beginning and the doSomething will only be done if the condition is true. The other is a do {dosomethingelse} while (condition). This will always run at least once, because the condition is checked at the end of every run. Since isChoiceValid is false, the statement runs once and exists. See docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
:) I get it now. Apologies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.