简体   繁体   中英

Exception in thread "main" java.util.NoSuchElementException (how to fix error)

This error is getting on my nerves.

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Main.main(Main.java:266)
    at Ideone.test(Main.java:72)
    at Ideone.test(Main.java:32)
    at Ideone.main(Main.java:23)

My code:

import java.util.Scanner;
import java.lang.Math;

class Main {
  public static void main(String[] args){


      Scanner scan = new Scanner(System.in);

      System.out.println("Welcome. What is your name?");
      String x;
      x = scan.nextLine();

      System.out.println("Hello " + x + ". Try your best to crack the code!");
      System.out.println(" ");

//Phase 1
      System.out.println("PHASE 1");

      System.out.println("Enter a number:");
      int y = scan.nextInt();
      if (y == 3){
        System.out.println("Correct!");
        System.out.println(" ");
//Phase 2
        System.out.println("PHASE 2");
        System.out.println("Enter a number:");
      }
      int z = scan.nextInt();
      if (z == 1 || (z >= 33 && z <= 100)){
        System.out.println("Correct!");
        System.out.println(" ");
//Phase 3
        System.out.println("PHASE 3");
        System.out.println("Enter a number:");}
      int c = scan.nextInt();
      if (c % 3 == 0 || c % 7 == 0){
        System.out.println("Correct!");
        System.out.println("You have cracked the code!");}
      else{
        System.out.println("Sorry, that was incorrect!");
        System.out.println("Better luck next time!");}


    }
}

Want to end the whole program if the input is incorrect. Not sure what is exactly wrong. Been stuck on this for awhile now. How do you find out what is wrong with the scanner and the inputs?

Your errors are purely logic errors. You have }'s everywhere they shouldnt be. You also are not consuming the /n " New line" that pressing enter generates on nextInt().

To avoid confusing your self use opening and closing comments on nested if's It is a good practice to create your frame work when creating if's and spacing everything out using whitespace.

if( conditon) 
{
      if_statement(s);
}
else
{
      else_statement(s);
}

Here is your corrected code

import java.util.Scanner;
import java.lang.Math;

class BrockTaylor
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Welcome. What is your name?");
        String x;
        x = scan.nextLine();
        System.out.println("Hello " + x + ". Try your best to crack the code!");
        System.out.println(" ");
        System.out.println("PHASE 1");
        System.out.println("Enter a number:");
        int y = scan.nextInt();
        // consume the /n
        scan.nextLine();
        //Phase 1
        if (y == 3)
        {
            System.out.println("Correct!");
            System.out.println(" ");
            System.out.println("PHASE 2");
            System.out.println("Enter a number:");
            int z = scan.nextInt();
            // consume the /n
            scan.nextLine();
            //Phase 2
            if (z == 1 || z >= 33 && z <= 100)
            {
                System.out.println("Correct!");
                System.out.println(" ");
                System.out.println("PHASE 3");
                System.out.println("Enter a number:");
                int c = scan.nextInt();
                // consume the /n
                scan.nextLine();
                //Phase 3
                if (c % 3 == 0 || c % 7 == 0)
                {
                    System.out.println("Correct!");
                    System.out.println("You have cracked the code!");
                }
                else // phase 3
                {
                    System.out.println("Sorry, that was incorrect!");
                    System.out.println("Better luck next time!");
                }
            }
            else // phase 2
            {
                System.out.println("Sorry, that was incorrect!");
                System.out.println("Better luck next time!");
            }
        }
        else// phase 1
        {
        System.out.println("Sorry, that was incorrect!");
        System.out.println("Better luck next time!");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM