0

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);
3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by zeroliken
0

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.

0

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

0

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

0
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?

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

Edited by zeroliken: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.