nadya carissa
nadya carissa

Reputation: 11

How to add check for string in do while

Scanner in = new Scanner(System.in); 
int  menuItem; 
do{ 
  System.out.println("Choose menu item 1,2,3,4,5: "); 
   menuItem = in.nextInt();
  }while(menuItem >5);
  //i tried to use this
  //while(menuItem >5 || !in.hasNextInt());---> but doesnt work

It shows

Exception in thread "main" java.util.InputMismatchException

In this code I want to validate the menu item not string type and not more than 5 and repeat the choose item menu if the input is not string type and not more than 5

But I don't know how to validate the input if its string.

Upvotes: 0

Views: 53

Answers (3)

PVR
PVR

Reputation: 915

As answer given by ΦXoce 웃 Пepeúpa, outer while loop will run infinitely if user enters no greater than 5.
Please try this:
1. which will validate number if it is string and asked user to enter valid number
2.And repeat the chhose menu item if it is correct.

package Sample;

import java.util.ArrayList;
import java.util.Scanner;

public class tets 
{

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int menuItem = 0;
        do {
            System.out.println("Choose menu item 1,2,3,4,5: ");
            try 
            {
                menuItem = Integer.parseInt(in.nextLine());
            } 
            catch (NumberFormatException e) 
            {
                System.out.println("Wrong input, Please enter again");
                menuItem=0;
            }
        } while (menuItem <= 5 && menuItem >=0);
        System.out.println("You have entered no > 5 OR no < 0");
        System.out.println("EXIT");
        }   

}

OUTPUT:
Choose menu item 1,2,3,4,5: 
2
Choose menu item 1,2,3,4,5: 
4
Choose menu item 1,2,3,4,5: 
A
Wrong input, Please enter again
Choose menu item 1,2,3,4,5: 
-1
Choose menu item 1,2,3,4,5: 
6
You have entered no > 5 OR no < 0
EXIT

Upvotes: 0

bhansa
bhansa

Reputation: 7504

Try this:

import java.util.*;

public class input_mismatch{
    public static void main(String[] args) {
Scanner in = new Scanner(System.in); 
int  menuItem; 
do{ 
  System.out.println("Choose menu item 1,2,3,4,5: "); 
   menuItem = in.nextInt();
  }while(menuItem >5 || in.hasNextInt());

 }
}

Upvotes: 0

You need to validate the input before you try to work with it, there is no warranty that the user gives as input an integer, not even a number, and if somethig like that happens then this here:

menuItem = in.nextInt();

will try to get the integer from something that is not "parsable as int" then you will get an exception

Try validating the input until the user gives something valid for you to work...

Example:

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int menuItem = -1;
do {
    System.out.println("Choose menu item 1,2,3,4,5: ");
    while (menuItem == -1) {
    try {
        menuItem = Integer.parseInt(in.nextLine());
    } catch (NumberFormatException e) {
        System.out.println("Wrong input");
    }
    }
} while (menuItem > 5);
}

Upvotes: 2

Related Questions