hello can you help me how i can catch the exception...

I want to input integer for my array and i want to catch the exception if i will input a string this will display Invalid input how can i use this in try catch because i am confuse please help me hoping for your positive responds...please correct me if i am wrong...


here is my code

System.out.println("Please Input number in array");
       try{
        	
      	    n=console.nextInt();
       }
          
       	catch(InputMismatchException e)
       	{
       		throw new Exception("invalid input");
       	}
      
        	System.out.println("FamilyNames to be process" + " " +  n);
       	     String [] list = new String [n];
       		
       	
       
        	for(i=0;i<n;i++)
        	{
        
        		list[i]= console.next();  
        			 			      
        	}

Please explain what you want to happen when you catch an exception. Your code throws another exception which will go to the next exception handler.

One way to handle this is to have a while loop surrounding the try/catch block that uses a boolean to keep it looping.
If you get a good number you set the boolean so the code will exit the loop. If you get an invalid number, you leave the boolean alone, stay in the loop and ask the user again for a number. The catch block would only println() a message saying the input was invalid.

Please explain what you want to happen when you catch an exception. Your code throws another exception which will go to the next exception handler.

One way to handle this is to have a while loop surrounding the try/catch block that uses a boolean to keep it looping.
If you get a good number you set the boolean so the code will exit the loop. If you get an invalid number, you leave the boolean alone, stay in the loop and ask the user again for a number. The catch block would only println() a message saying the input was invalid.

hello sir thank you for the reply...i want to happen that if i will input string this will display invalid input and display again the "please input number to array" and if i get the correct input shall i say integer my program will continue down to my forloop...

Please explain what you want to happen when you catch an exception. Your code throws another exception which will go to the next exception handler.

One way to handle this is to have a while loop surrounding the try/catch block that uses a boolean to keep it looping.
If you get a good number you set the boolean so the code will exit the loop. If you get an invalid number, you leave the boolean alone, stay in the loop and ask the user again for a number. The catch block would only println() a message saying the input was invalid.

sir i follow your suggestion but sir i have an error saying n might not have been initialized...please help me sir ..hoping for your positive responds...and please correct me if i am wrong with my codes...

boolean trap;
int i,n;
 do
    {
    	  
       try
       {
          
             	
            System.out.println("Please Input number in array");
      	    n=console.nextInt();
      	    
      	    trap=false;
      	
       }	
       	
       	catch(InputMismatchException e)
       	{
       		System.out.println("invalid input");
       	}
      	    
      	    
    } 	    
    while(!trap);  	    
           System.out.println("Family Names to be process" + " " +  n);
           String [] list = new String [n];
              
              
        	for(i=0;i<n;i++)
        	{
        
        		list[i]= console.next();  
        			 			      
        	}

i have an error saying n might not have been initialized

It means you have to initialize the variable N.
Like: int n = 5

Also where do you set the value of trap to true. In your code it will always be false!!!

It means you have to initialize the variable N.
Like: int n = 5

hello sir thank you for the reply,

sir i will not give initial value to the variable n because it is in the user
choice if how many array of names he want...example if the user will input 10 so this will create an array of 10 family names...correct me if i am wrong instead of 5 I just do like this int n=0;...

Because n is given a value in a statement that throws an exception, it is possible that n will not be given a value if the exception is thrown.
With an exception in the nextInt() method call, n will NOT be given a value before it is used in line 26.

Also where do you set the value of trap to true. In your code it will always be false!!!

hello sir thank you for the reply, please correct me if i am wrong...sir i have also problem if i input String this will print invalid but it will continue to the next statement S.O.P("family name to be process");

I want only if the user input String this will display Invalid then this will loop to the "Please Input number in array"...hoping for your positive responds...

int n=0;
  boolean trap;


do
    {
      trap=true;
      
      try
      {
           	System.out.println("Please Input number in array");	
             	
        
      	    n=console.nextInt();
      	    trap=false;
    
      }
      catch(InputMismatchException e)
      {
      	 
      	 System.out.println("invalid");
      	  
      }
        
    }   
  while(!trap);
  	
  	     
  	    
        	System.out.println("Family Names to be process" + " " +  n);
      	
        	String [] list = new String [n];

What happens when you execute this code?
Does it do what you want it to?

What happens when you execute this code?
Does it do what you want it to?

hello sir, the problem is that it will execute all my statements..if i input String then

this will display invalid and then it goes down to other statement displaying Family names to be process and so on...i did not post my whole code here...

What happens when you execute this code?
Does it do what you want it to?

sir here's the output if I input String,i want only to loop in the "Please Input number to Array" if input String.sir, why is it will execute Family names etc...did i have wrong in my do while loop..please help me sir hoping for your positive responds...

Please Input number to array"
sfsf
invalid
Family Names to be process 0
Search a name
sfsf Not Found
Search keyword:

Look at the while(!trap); statement
The boolean trap is true until there is good data and then its false.
What value of the trap variable will let the loop exit and what value will cause the loop to continue?
If you are in doubt add two copies of the following statement, one before and one after the while(!trap):
System.out.println("trap=" + trap); // show trap value

Look at the while(!trap); statement
The boolean trap is true until there is good data and then its false.
What value of the trap variable will let the loop exit and what value will cause the loop to continue?
If you are in doubt add two copies of the following statement, one before and one after the while(!trap):
System.out.println("trap=" + trap); // show trap value

sir thank you for the reply, i am confuse sir what should i do sir i mean all i want only to display the Input number in array if i inputed wrong...not to display until to the search keyword if i inputed the wrong one...sir help me i am confuse...hoping for your positive responds...

Did you add the two println() statements I asked you to add to your program?
What did they print out on the screen?

Would using the Google Translator help you to write a better response. Use it to translate your language to English
I'm having a problem understanding your English.

Did you add the two println() statements I asked you to add to your program?
What did they print out on the screen?

Would using the Google Translator help you to write a better response. Use it to translate your language to English
I'm having a problem understanding your English.

yes sir i added to println()...sir my problem is that if String is inputed example

Enter number to array
SSSSS--->this is the string

it will print "Invalid"...i want my loop go back to
"Enter number to array"

but why is it the ouput is this
SSSS
invalid
Family Names to be process 0
Search a name
sfsf Not Found
Search keyword:

please help me sir hoping for your positive responds...

Did you add the two println() statements I asked you to add to your program?
What did they print out on the screen?

Would using the Google Translator help you to write a better response. Use it to translate your language to English
I'm having a problem understanding your English.

hello sir, sir i already tried to put println(),sir can you please correct my code and put some comments so that it can enlighten my mind because i can't understand, im still beginner in this java and i want to learn please help me...hoping for your positive responds....

The program will execute your code in do.. while loop if the boolean value in the while clause is true. What your code do is the opposite. You should write the trap = false in the catch block, not in the try block.

i added to println()

Are you sure you added the println()s where I suggested?
Your posted output from the program does NOT show the output from those println()s.

}
    System.out.println("trap1=" + trap);    
    }   
  while(!trap);
    System.out.println("trap2=" + trap);   	

  	     
  	    
        	System.out.println("Family Names to be process" + " " +  n);

Are you sure you added the println()s where I suggested?
Your posted output from the program does NOT show the output from those println()s.

}
    System.out.println("trap1=" + trap);    
    }   
  while(!trap);
    System.out.println("trap2=" + trap);   	

  	     
  	    
        	System.out.println("Family Names to be process" + " " +  n);

sir thank you for the reply i modify my code please correct me if i am wrong..but i have problem sir it still print the "search keyword" if i inputed a string i want only to be print "input number in array"...hoping for your positive responds...

do
    {     
    
      	
       trap=true;
      	  
       try
       {
          
             	
           	System.out.println("Please Input number to be process");
      
      	    n=console.nextInt();
      	     trap=false;
      	    
      	
       }	
       	
       	catch(InputMismatchException e)
       	{
       		System.out.println("Invalid Input,Please Input number:" + n);
       		
       	
       	}
      	 
         System.out.println("trap2=" + trap); 
        } 	  
         
    while(trap); 
         System.out.println("trap2=" + trap);

it still print the "search keyword"

Sorry, I do NOT see where in your code it prints "search keyword"

I have no idea what you are asking. The code you posted is only a little piece of your program.


The code you posted looks like it will ask you for a number, (you enter a number) and if the number is OK, trap is set to false and you exit the loop.
If the number is not OK, the program will ask you again for the number.

What do you want the program to do differently?


Can you post all of the console messages that are printed when you execute the program and add comments to the messages showing where the problem is.

Sorry, I do NOT see where in your code it prints "search keyword"

I have no idea what you are asking. The code you posted is only a little piece of your program.


The code you posted looks like it will ask you for a number, (you enter a number) and if the number is OK, trap is set to false and you exit the loop.
If the number is not OK, the program will ask you again for the number.

What do you want the program to do differently?


Can you post all of the console messages that are printed when you execute the program and add comments to the messages showing where the problem is.

sir here's is the code...hoping for your positive responds...

do
    {     
    
      	
       trap=true;
      	  
       try
       {
          
             	
           	System.out.println("Please Input number to be process");
      
      	    input=console.nextInt();
      	     trap=false;
      	    
      	
       }	
       	
       	catch(InputMismatchException e)
       	{
       		System.out.println("Invalid Input,Please Input number:" + input);
       	   
       	
       	}
      	 
        } 	  
         
    while(trap);  	    
         
       
           	System.out.println("Family Names to be process" + " " +  input);
                String [] list = new String [input];   
              
        	for(i=0;i<list.length;i++)
        	{
        
        		list[i]= console.next();
        		
        		
        	}	


                 System.out.print("Search keyword: "  );
        	 keyword=console.next();
        	 
        	 for(i=0;i<list.length;i++)

Is there a problem now with the code you have posted? You did enter any description of why you posted this code.

I think some of your problem is how you are using the Scanner class. If nextInt() fails you need to clear what's in Scanner's buffer. Also you could be using the hasNext methods to test for valid input before reading it. Do some experimenting with using different Scanner methods to test for int and to clear out bad data.

Is there a problem now with the code you have posted? You did enter any description of why you posted this code.

I think some of your problem is how you are using the Scanner class. If nextInt() fails you need to clear what's in Scanner's buffer. Also you could be using the hasNext methods to test for valid input before reading it. Do some experimenting with using different Scanner methods to test for int and to clear out bad data.

sir can you show me how sir i am confuse about you said..hoping for your positive responds..

Is there a problem now with the code you have posted?

Is there a problem now with the code you have posted?

yes sir,i want only this part sir to be displaye if i entered wrong data

System.out.println("Please Input number to be process");

then i will enter again until i get the right data...

Some pseudo code:
loop until a good number
Ask user for number
Get number from user
if number is good, exit loop
else tell user number is bad
end loop

Some pseudo code:
loop until a good number
Ask user for number
Get number from user
if number is good, exit loop
else tell user number is bad
end loop

sir, you mean i will change my do while loop?...

this is only my problem sir if i enter string it will print

invalid
Search name:
Search keyword"

but my point sir i want only to print this sir if i inputed a string

invalid
Please input number to be process
then here sir i will input again with the right one
so i will input 5

so this will continue then enter array of names of five
then it will dislplay search names

and then search keyword...

but the code that i posted sir is when i input string
this will print

invalid
search name
search keyword...

sir, i have no problem if i input the right one...only if i input string..

hoping for your positive responds...

when i input string
this will print

invalid
search name
search keyword...

Does it always say "invalid" when you input a String? Even if the String is valid?
Why doesn't your code see the difference between a valid and an invalid String?

i have no problem if i input the right one...only if i input string..

What do you mean here? Everything that you input is a String. Some Strings are right and some are invalid.

Does it always say "invalid" when you input a String? Even if the String is valid?
Why doesn't your code see the difference between a valid and an invalid String?


What do you mean here? Everything that you input is a String. Some Strings are right and some are invalid.

hello sir, in the first part of my code sir it will ask to the user how many element should be put in array...that's why it will print "please input number to be process"

the user should input integers if it's not this will print invalid...

in my porgram sir if i directyly input integers like 5, i have no problem.
but when i input a string, "invalid" and it will print "enter number to be process" this is my expected output if i input a STring in the first part.... but why is it sir the out put like this

invalid,input...
names to be process
search name
search keyword

...it should be like this
invalid input..
Please enter number to be process...

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.