-1

I want a program that repeatedly prompts the user to input a number and then prints whether that number is prime or not. This works once, then on the next iteration it gives a No Such Element Exception before the user can enter another input.

I've tried using IF statements with hasNext() or hasNextInt() but those never seem to be true for the same reason. I also tried using a FOR loop to iterate a fixed number of times but that gives the same error. Why is this allowing for user input the first time it loops through but not after that?

public static void primeChecker() 
{ 
      Scanner scan = new Scanner(System.in);
      System.out.print("Please enter a number: ");
      int number = scan.nextInt();
      if (isPrime(number)) {
        System.out.println(number + " is prime");
      }
      else {
        System.out.println(number + " is not prime");
      }
      scan.close();
}

public static void main(String[] args) 
{
    int y=1;
    while(y!=0)
    {
      primeChecker();
}
jgalvin
  • 13
  • 1
  • 1
    Don't close your scanner. Otherwise, `System.in` is closed as well and you can't get any user input any more – QBrute Jun 04 '21 at 19:49
  • 2
    What you mean in the "next iteration"? where is it? what does your `primteChecker()` have to do with `y`, which is a local variable of the main method? – Giorgi Tsiklauri Jun 04 '21 at 19:52
  • 1
    You should take a look at https://stackoverflow.com/questions/26245468/what-does-scanner-close-do – azurefrog Jun 04 '21 at 20:00
  • As presented, the source code as a syntax error. A single iteration through a loop is called a *trip* when not wanting to use *iteration*. – greybeard Jun 05 '21 at 10:51

3 Answers3

0

Remove scan.close();, as it is closing the scanner.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
-1

In order to repeatedly ask the user for a number without stopping, you need to put a while around the code.

Here is my take on this challenge:

public static void primeChecker() {

    while(true) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        int number = scan.nextInt();
        if (isPrime(number)) {
            System.out.println(number + " is prime");
        } else {
            System.out.println(number + " is not prime");
        }
      
    }      
}

public static void main(String[] args) {
    primeChecker();
}

This code should repeatedly ask you for a number(after telling you what the previous answer was). It worked for me!

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
FairOPShotgun
  • 125
  • 2
  • 25
-1

First of all what does your local variable have to do with the function? even if you pass it to function it won't change.

by looking at your code one thing you can do is safely remove y it has nothing to with it, and check in prime checker if number is equal to 0 System.exit(), this is one way to do it without changing much of this code.

while (true) {
    primeChecker();
}

and inside primeChecker

int number = scanner.nextInt();
if (number == 0) {
    System.exit(0);
}

it will terminate the programme when input is 0.

here you can learn more about System.exit:- https://www.baeldung.com/java-system-exit

NoOne
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 20 '22 at 12:51