Results 1 to 6 of 6
  1. #1
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default try, catch, exception error

    I am working on this assignment where I have to import data from a text file, then search the array for certain numbers

    Here is what I have so far:

    Java Code:
    import java.io.*;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class ArrayNum
    {
    	public static void main(String[] args)
    	{
    		int search1 = 513;
    		int search2 = 123;
    		int search3 = 162;
    		int search4 = 803;
    
    		try
    		{
    
    			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("DATA9.txt")));
    
    			String line = null;
    
    			while(line == br.read() && line != null)
    			{
    
    				ArrayList NUM = new ArrayList(12);
    
    				NUM.add(new Integer(br));
    
    				System.out.println("myArrayList contains the following values: " + br);
    				System.out.println("The number 513 is found at the position(s): " + NUM.indexOf(search1));
    				System.out.println("The number 123 is found at the position(s): " + NUM.indexOf(search2));
    				System.out.println("The number 162 is found at the poisition(s): " + NUM.indexOf(search3));
    				System.out.println("The number 803 is found at the position(s): " + NUM.indexOf(search4));
    
    				for(int a = 0; a < br.length; a++)
    				{
    
    					if(NUM.contains(new Integer(a)))
    					{
    						System.out.println("The value was found at position: " + NUM[a]);
    					}
    
    					else
    					{
    						System.out.println("The value " + "was not found in the array");
    					}
    		}
    
    			catch(Exception e)
    			{
    				System.err.println("There was an error " + e.getMessage());
    			}
    		}
    	 }
       }
    }
    It gives me the following errors:

    Java Code:
    ArrayNum.java:51: 'catch' without 'try'
                                      catch(Exception e)
    ArrayNum.java:51: '}' expected
                                     catch(Exception e)
    ArrayNum.java:51: not a statement
                                      catch(Exception e)
    ArrayNum.java:51: ';' expected
                                    catch(Exception e)
    ArrayNum.java:17: 'try' without 'catch' or 'finally'
                            try
    5 errors
    Press any key to continue . . .
    Please help!

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    How is your text file setup? Is it one number per row, like:


    32424
    234
    9344
    5133
    205
    3434

    Also, do you have to find all the occurences of a particular set of numbers

  3. #3
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Your catch block is inside the try block. Look at your brackets. You need to move the catch block two close brackets down.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  4. #4
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Quote Originally Posted by sehudson View Post
    How is your text file setup? Is it one number per row, like:


    32424
    234
    9344
    5133
    205
    3434

    Also, do you have to find all the occurences of a particular set of numbers
    It's one number per column.. for ex:

    1 2 3 4 5 1 3 4 2 6 7 8

    Yeah.. I have to find all occurences.. say if it is 1, then it should print = "The number 1 was found at index " + index

  5. #5
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Quote Originally Posted by FlyNn View Post
    Your catch block is inside the try block. Look at your brackets. You need to move the catch block two close brackets down.
    Oh what a bonehead mistake.. thanks for pointing that out! :D

  6. #6
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Quote Originally Posted by Asvin View Post
    Oh what a bonehead mistake.. thanks for pointing that out! :D
    Glad to help you out.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

Similar Threads

  1. try catch exception
    By soxfan714 in forum New To Java
    Replies: 3
    Last Post: 05-05-2011, 11:11 AM
  2. Catch exception, return to while loop
    By ChicagoAve in forum New To Java
    Replies: 3
    Last Post: 02-20-2011, 06:44 PM
  3. Replies: 7
    Last Post: 12-30-2010, 01:42 PM
  4. Why can't I catch an exception?
    By glhansen in forum New To Java
    Replies: 5
    Last Post: 03-17-2009, 06:48 PM
  5. Error with my Try Catch
    By kewlgeye in forum New To Java
    Replies: 9
    Last Post: 05-03-2008, 03:38 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
  •