Member Avatar for Member #921280

I need to check whether the entered numbers are valid, if not output a message. this needs to be done with using exception handles. i have tried the following but does not work. i get the error : cannot invoke hasNextInt() on the primitive type int

input = in.nextInt();
        
     
        if (input > 0 || input > 8 || input.hasNextInt())
        {
           
        	System.out.print("Enter Option");
        	  
                 }
           
        
        else 
        { 
        	System.out.println();
            System.out.println("ERROR Bad data entry");
            System.out.println();
            
   
        } // end else
     
     } while (input !=7);

Dani AI

Generated

The compile error happens because hasNextInt() is a Scanner method — not a method on an int. In the posted code input is an int, so input.hasNextInt() triggers "cannot invoke hasNextInt() on the primitive type int." Also the logic using || is wrong for a numeric range: input > 0 || input > 8 will almost always be true. For the menu shown (options 1–7) the correct range test is input >= 1 && input <= 7.

Follow this safe pattern (no exceptions) when reading a menu choice from Scanner:

  • Check in.hasNextInt() before calling in.nextInt() so invalid tokens are detected and not fed to nextInt().
  • If hasNextInt() is false, consume the bad token (in.next()) or the rest of the line (in.nextLine()) to avoid an infinite loop.
  • After reading the int, check that it lies in the valid range with &&.
  • Only then dispatch to the switch/handler. Repeat until the quit option is chosen.

Example of the core loop:

Scanner in = new Scanner(System.in);
int input = 0;
do {
  System.out.print("Enter Option: ");
  if (!in.hasNextInt()) {
    System.out.println("ERROR Bad data entry");
    in.next(); // discard invalid token
    continue;
  }
  input = in.nextInt();
  if (input < 1 || input > 7) {
    System.out.println("ERROR Bad data entry");
    continue;
  }
  // valid input: handle option (switch ...)
} while (input != 7);

Notes: was correct to ask what in is (it must be a Scanner). was right that the comparison should be < 8 (or <= 7) and that hasNextInt() belongs to the Scanner. This approach satisfies the "no exception handling" requirement while preventing InputMismatchException and keeping the menu responsive.

Recommended Answers

All 5 Replies

what exactly is your 'in'?
if it's not a Scanner object, there's indeed something wrong with your code.
also, I don't see any exception handling there. no doubt you're supposed to throw and catch an exception.

Member Avatar for Member #921280

it is up in the code...
No I cannot use exceptions that is the problem

propably, but it is not in the code which you shared, so how do you expect us to be able to help you correct it?

as for exception handling: it's about like every other part of the basics, no place like the official tutorials to learn about them

Member Avatar for Member #921280
import java.util.Scanner;

public class AccountTest { public static void main(String args[])
{
    
  	// input Scanner
     Scanner in = new Scanner(System.in);
     int input = 0; // local variable
  	
     int counter = 0; 
  	
     do {
     
     // Options
     
        System.out.println("What do you want to do?");
        System.out.println("        1  Create a new account");
        System.out.println("        2  Deposite funds");
        System.out.println("        3  SWithdraw funds");
        System.out.println("        4  Show Balance");
        System.out.println("        5  Show Interest");
        System.out.println("        6  Show Summary");
        System.out.println("        7  Quit");
        System.out.print("Enter Option");
     
     
        input = in.nextInt();
        
     
        if (input > 0 || input > 8 || input.hasNextInt())
        {
           
        	System.out.print("Enter Option");
        	/**   switch (input) 
           {
           
              case 1:
                 {
                    input1();
                    break;
                 }
              case 2:
                 {
                    input2();
                    break;
                 }
              case 3:
                 {
                    input3();
                    break;
                 }
              case 4:
                 {
                    input4();
                    break;
                 }
              case 5:
                 {
                    input5();
                    break;
                 }
           }*/
        }	
        else 
        { 
        	System.out.println();
            System.out.println("ERROR Bad data entry");
            System.out.println();
            
   
        } // end else
     
     } while (input !=7);
  
     System.exit(0); // exit VM
  }
 

} // end AcountTest class

This lab asks not to use exception handeling so how can I do it without it?

if (input > 0 || input > 8 || input.hasNextInt())

Like what stultuske said hasNextInt() can only be used in a scanner object
("in" in your code is a scanner object )
Also maybe you meant input < 8
But this method won't work for non numeric input

How about using regex like in this thread
Re: Input Check (Int or not)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.