Member Avatar for mehnihma

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);

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 mehnihma

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 mehnihma
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.