Home > Mobile >  Keep getting NoSuchElementException with Scanner in Java
Keep getting NoSuchElementException with Scanner in Java

Time:09-02

I have been learning Java for about three weeks and I'm attempting to write a little vending machine program. For some reason I keep running into a NoSuchElementException through my Scanner, and I've found it is coming from the Item class. I also have my try{} catch{} blocks commented out but I do want to have the ability to catch an InputMismatchException as well. Any help is very much appreciated, I have the code for my Item class below.

``

    import java.util.*;
    public class Item {
      public Item() {  // class constructor
        System.out.println("Choose an item from the list below!");
        System.out.println("(Enter only the item's number): ");
      }
    
      public void Out() {
        Scanner sc = new Scanner(System.in);
    
        ArrayList<String> optionList = new ArrayList<>();  // collection
        String x = "#1 Coffee Creamer";
        String y = "#2 Black Cold Brew";
        String z = "#3 An Actual Tree";
        optionList.add(x);  // add String x to optionList
        optionList.add(y);  // add String y to optionList
        optionList.add(z);  // add String z to optionList
        System.out.println();
    
        for (String str : optionList) {  // print optionList items
          System.out.println(str);
        }
    
        System.out.println();
        System.out.print("Enter a number: ");  // prompt
    
        int itemNumber = sc.nextInt();  // user's input field
    
        // while (true) {  // catch possible InputMismatchException
        //   try {
        //     itemNumber = sc.nextInt();
        //     break;
        //   } catch (Exception e) {
        //     System.out.println("Enter a valid choice.");
        //     System.out.println("Enter a number: ");
        //   }
        //   sc.nextInt();
        // }
    
        while (itemNumber != 1 && itemNumber != 2 && itemNumber != 3) {
          System.out.println("Enter a valid choice.");
          itemNumber = sc.nextInt();
        } if (itemNumber == 1 || itemNumber == 2 || itemNumber == 3) {
            switch (itemNumber) {
              case 1:
                System.out.println("You chose "   optionList.get(0)   "!");
                break;
              case 2:
                System.out.println("You chose "   optionList.get(1)   "!");
                break;
              case 3:
                System.out.println("You chose "   optionList.get(2)   "!");
                break;
            }
        }
    
        System.out.println();
        sc.close();
      }
    }

CodePudding user response:

try:

        Scanner sc = new Scanner(System.in);


        int itemNumber = 0;
        while (true) {
            try {
                itemNumber = sc.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("Enter a valid choice.");
                System.out.println("Enter a number: ");
                sc.next();
            }
        }

CodePudding user response:

sorry, actually not sure what you want to do but if you want to keep asking for correct number you can define function like

    private static int nextInt(Scanner sc) {
        while (true) {
            try {
                return sc.nextInt();
            } catch (Exception e) {
                System.out.println("Enter a number: ");
                sc.next();
            }
        }
    }

and replace all your calls:

itemNumber = sc.nextInt();

with

itemNumber = nextInt(sc)

  • Related