What does your code do when it gets the invalid input? It should loop back and ask the user to the number to process.
Look at your code and see why it is not looping back. How does it fall out of the loop when there is invalid input?

Can you write a small program that will compile and execute with only this problem in it and post it here?

What does your code do when it gets the invalid input? It should loop back and ask the user to the number to process.
Look at your code and see why it is not looping back. How does it fall out of the loop when there is invalid input?

Can you write a small program that will compile and execute with only this problem in it and post it here?

sir here is the code, sir it will create infinite loop how to get rid of this.

import java.util.*;
 
  
   class Keyword
    
     {
       
        public static Scanner console =new Scanner(System.in);
       
        public static void main(String []args)throws Exception
        	
        	
        {
           		
        	String duplicate;
        	String search,names,keyword;
        	int i;
        	int n=0;
        	boolean found,trap;
         
        	int counter;
        	String c;
        
  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:" );
       	 
       	
       	}
      	 
    } 	  
         
    while(trap);  	    
         
       
           	System.out.println("Names to be process" + " " +  n);
           String [] list = new String [n];   
              
        	for(i=0;i<list.length;i++)
        	{
        
        		list[i]= console.next();
        	
        		
        	}	
       
             
         		
             	 				
           found = false;
        	 System.out.println("Search a name");
        	 search=console.next();

   }
}

Has your problem changed? Now you say you are getting an infinite loop.
What did you change to get the infinite loop?

You need to experiment with using the Scanner class so that you understand how to use it. There are other methods you might need to use such as hasNextInt to test if there is an int available to be read and nextLine to remove any bad data from the input buffer so that you can read an int.

Has your problem changed? Now you say you are getting an infinite loop.
What did you change to get the infinite loop?

You need to experiment with using the Scanner class so that you understand how to use it. There are other methods you might need to use such as hasNextInt to test if there is an int available to be read and nextLine to remove any bad data from the input buffer so that you can read an int.

sir thank you for the reply,can you please help me sir where i am going to put the hasNextInt or the nextLine....hoping for your positive responds...

Use the hasNextInt() to test if the next data to be input will be an int. Your code doesn't test first, it tries to read an int and can fail if the data is not an int.
If the next data is NOT an int, use nextLine() to read the bad data and allow the program to ask the user again for good data.

Here's a modified version of you code that shows one usage:

import java.util.*;
 
  
   class TestingKeyword2       {
      // Get input from an array vs having to use a console 
      public static Scanner console =new Scanner("x 2 name1 Name2 xxx"); //System.in);
       
      public static void main(String []args) {
           		
        	String search,names,keyword;
        	int n=0;
        	boolean trap = true;   // controls looping
         
         // Loop until we get a good number
         do    {     
           	  
             try          {
                 	System.out.println("Please Input number to be processed");
            
            	    n=console.nextInt();
            	    trap=false;   // got good number signal we can leave loop
            	
             } catch(InputMismatchException e)       	{
          		System.out.println("Invalid Input,Please Input number:" );
          	   System.out.println("skipping next=" + console.next()); // eat invalid input
          	 }
         	 
         }while(trap);  	    
         
         System.out.println("Names to be processed=" + " " +  n);

         String [] list = new String [n];   
              
        	for(int i=0;i<list.length;i++)        	{
        		list[i]= console.next();
        	}
	
         System.out.println("list=" + Arrays.toString(list)); // show list
   }
}/*  Output:
Running: java.exe -classpath D:\JavaDevelopment;.  -Xmx512M TestingKeyword2

Please Input number to be processed
Invalid Input,Please Input number:
skipping next=x
Please Input number to be processed
Names to be processed= 2
list=[name1, Name2]

0 error(s)
*/

Use the hasNextInt() to test if the next data to be input will be an int. Your code doesn't test first, it tries to read an int and can fail if the data is not an int.
If the next data is NOT an int, use nextLine() to read the bad data and allow the program to ask the user again for good data.

Here's a modified version of you code that shows one usage:

import java.util.*;
 
  
   class TestingKeyword2       {
      // Get input from an array vs having to use a console 
      public static Scanner console =new Scanner("x 2 name1 Name2 xxx"); //System.in);
       
      public static void main(String []args) {
           		
        	String search,names,keyword;
        	int n=0;
        	boolean trap = true;   // controls looping
         
         // Loop until we get a good number
         do    {     
           	  
             try          {
                 	System.out.println("Please Input number to be processed");
            
            	    n=console.nextInt();
            	    trap=false;   // got good number signal we can leave loop
            	
             } catch(InputMismatchException e)       	{
          		System.out.println("Invalid Input,Please Input number:" );
          	   System.out.println("skipping next=" + console.next()); // eat invalid input
          	 }
         	 
         }while(trap);  	    
         
         System.out.println("Names to be processed=" + " " +  n);

         String [] list = new String [n];   
              
        	for(int i=0;i<list.length;i++)        	{
        		list[i]= console.next();
        	}
	
         System.out.println("list=" + Arrays.toString(list)); // show list
   }
}/*  Output:
Running: java.exe -classpath D:\JavaDevelopment;.  -Xmx512M TestingKeyword2

Please Input number to be processed
Invalid Input,Please Input number:
skipping next=x
Please Input number to be processed
Names to be processed= 2
list=[name1, Name2]

0 error(s)
*/

sir, thank you for sharing this your idea and showing little some code that can enlighten my mind...I also get idea about your Scanner,and also for the Arrays.toString for displaying list...thank you also for putting some comments in the code, this is what i want it is easy for me to understand...thank you and more power to you sir...

I put console.nextLine() in the catch..to get rid of the bad data...and it works...
please correct me if i am wrong...

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:" );
       	  
       	        console.nextLine();
       	}
      	 
        } 	  
         
    while(trap);  	    
         
       
           	System.out.println("Names to be process" + " " +  n);
           String [] list = new String [n];   
              
        	for(i=0;i<list.length;i++)
        	{
        
        		list[i]= console.next();
        	
        		
        	}	
       
             System.out.println("The names are" + Arrays.toString(list));
         		
             	 				
           found = false;
        	 System.out.println("Search a name");
        	 search=console.next();
  }
}
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.