Results 1 to 4 of 4
  1. #1
    ChicagoAve is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Catch exception, return to while loop

    I have a class which reads pairs of integers from a file and stores them into an array of fractions. I have an inputmismatch exception to catch anything that is not an integer. The problem is that once the exception is caught, my while loop terminates and the rest of the data is not read. How do I get back inside my while loop from my exception?

    Java Code:
    private void readArray()
        {
            
            String line;
            Scanner scanner;
            int numerator,denominator;
    
           try
           {
            
            scanner = new Scanner(file);
            A = new Fraction[MAX];
            int data = 0;
            n = 0;
            
               try
               {
                   
                   while(scanner.hasNext())
                    {
                    
                    data = 0;    
                    numerator = scanner.nextInt();
                    data = 1; 
                    denominator = scanner.nextInt();
                    A[n] = new Fraction(numerator, denominator);
                    n++;
                          
             
                   }
                scanner.close();
                }
                catch(InputMismatchException e)
                {
                    String str = scanner.next();
                    System.out.println("the input " + str+ "is bad");
                    
                    if (data == 0)
                    {
                        str = scanner.next();
                        System.out.println("ignoring this data " +str);
                    }
                    
                }
                
               
            }
            catch(FileNotFoundException exception)
            {
                System.out.println("The file " + file.getAbsolutePath() + "was not found.");
                System.exit(0);
            }
        
        
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    ChicagoAve is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Of course! Long morning. Thanks for the help.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. try and catch inside a for loop fails!!!
    By Kingmaha64 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 03:40 AM
  2. Try catch loop problems :'(
    By Romally in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 09:15 PM
  3. try-catch InputMismatchException in a while loop
    By themulator in forum New To Java
    Replies: 17
    Last Post: 10-12-2010, 05:49 AM
  4. Why can't I catch an exception?
    By glhansen in forum New To Java
    Replies: 5
    Last Post: 03-17-2009, 07:48 PM
  5. Try- catch block + return + IO package
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •