Results 1 to 12 of 12
  1. #1
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default My Try Catch block isn't catching the right value from a file.

    I have my code using a NumberFormatException for reading in a file of integers. When it reads in from the given file, it is supposed to catch all the non-integers and display a message saying which values are not integers. When I run my program, it says that 200 and 150 (the integers right before the words) are the non-integers... any idea why? Here is the file to read from with my code following it.

    File lab10data.txt

    Java Code:
    100
    200
    bogus
    300
    150
    weasel
    400
    600
    250
    3.5
    800
    50
    500
    450
    600
    550
    lab10.java

    Java Code:
    import java.util.*;
    import java.io.*;
    public class lab10
    {
    	public static void main(String [] args)
    	{
    		Scanner inScan, fScan = null;
    		String fName;
    		String nextItem;
    		int nextInt = 0;
    		int i = 0;
    		int [] A = new int[5];
    		File file;
    		
    		try {
    			inScan = new Scanner(System.in);
    			System.out.println("Please enter the file to read from: ");
    			fName = inScan.nextLine();
    			file = new File(fName);
    			
    			while (!file.exists()) {
    				System.out.println("Please enter the file to read from: ");
    				fName = inScan.nextLine();
    				file = new File(fName);
    			}
    			
    			fScan = new Scanner(file);
    			
    			while (fScan.hasNextLine())
    			{
    				try {
    					nextItem = fScan.nextLine();
    					nextInt = Integer.parseInt(nextItem);
    					A[i] = nextInt;
    					i++;
    				}
    				catch (NumberFormatException nonInt) {
    					System.out.println(nextInt + " is not an integer -- ignored.");
    				}	
    			}
    
    			System.out.println("Here are your " + i + " items:");
    			for (int j = 0; j < i; j++)
    			{
    				System.out.println(A[j] + " ");
    			}
    		}
    		catch (FileNotFoundException bad) {
    			fName = null;
    		}
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Java Code:
    			while (fScan.hasNextLine())
    			{
    				try {
    					nextItem = fScan.nextLine();
    					nextInt = Integer.parseInt(nextItem);
    					A[i] = nextInt;
    					i++;
    				}
    				catch (NumberFormatException nonInt) {
    					System.out.println(nextInt + " is not an integer -- ignored.");
    				}
    nextItem is the offending input; not nextInt (it contains the previous valid input) so you should print out nextItem in your catch clause.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    Okay when I tried using nextItem like this...

    Java Code:
    while (fScan.hasNextLine())
    			{
    				try {
    					nextItem = fScan.nextLine();
    					nextInt = Integer.parseInt(nextItem);
    					A[i] = nextInt;
    					i++;
    				}
    				catch (NumberFormatException nonInt) {
    					System.out.println(nextItem + " is not an integer -- ignored.");
    				}	
    			}
    It will not compile correctly, saying that nextItem may not have been initialized.?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by theBurgh22 View Post
    Okay when I tried using nextItem like this...

    Java Code:
    while (fScan.hasNextLine())
    			{
    				try {
    					nextItem = fScan.nextLine();
    					nextInt = Integer.parseInt(nextItem);
    					A[i] = nextInt;
    					i++;
    				}
    				catch (NumberFormatException nonInt) {
    					System.out.println(nextItem + " is not an integer -- ignored.");
    				}	
    			}
    It will not compile correctly, saying that nextItem may not have been initialized.?
    The compiler is extremely stupid (but very consistent at that). Suppose your readLine() method throws an Exception for some reason; that would leave the nextItem variable uninitialized. So, initialize it somewhere (preferably when it is declared).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    Great, thank you! I got that part to work. I now need to add the integer values to an array, while ignoring the non-integers and not incrementing the count variable. I also need to double the array size when needed. I'm not getting the output that I should be getting with my code. I should have 13 items to print out, which are all the integers in the file. However, I am only getting 11, and the first 10 are zeros, and the last is 550. Here is the updated code.

    Java Code:
    import java.util.*;
    import java.io.*;
    public class lab10
    {
    	public static void main(String [] args)
    	{
    		Scanner inScan, fScan = null;
    		String fName;
    		//String nextItem;
    		int nextInt = 0;
    		int i = 0;
    		int [] A = new int[5];
    		File file;
    		
    		try {
    			inScan = new Scanner(System.in);
    			System.out.println("Please enter the file to read from: ");
    			fName = inScan.nextLine();
    			file = new File(fName);
    			
    			while (!file.exists()) {
    				System.out.println("Please enter the file to read from: ");
    				fName = inScan.nextLine();
    				file = new File(fName);
    			}
    			
    			fScan = new Scanner(file);
    			
    			while (fScan.hasNextLine())
    			{
    				String nextItem = fScan.nextLine();
    				try {
    					nextInt = Integer.parseInt(nextItem);
    					A[i] = nextInt;
    					i++;
    				}
    				catch (NumberFormatException nonInt) {
    					System.out.println(nextItem + " is not an integer -- ignored.");
    				}	
    				catch (ArrayIndexOutOfBoundsException dbl) {
    					System.out.println("Resizing the array from " + A.length + " to " + A.length * 2);
    					A = new int[A.length * 2];
    				}
    			}
    			
    			System.out.println("Here are your " + i + " items:");
    			for (int j = 0; j < i; j++)
    			{
    				System.out.println(A[j] + " ");
    			}	
    		}
    		catch (FileNotFoundException bad) {
    			fName = null;
    		}
    	}
    }
    the file remained the same.

  6. #6
    anubhavranjan is offline Member
    Join Date
    Sep 2009
    Location
    Bangalore, India
    Posts
    10
    Rep Power
    0

    Post

    Java Code:
    catch (ArrayIndexOutOfBoundsException dbl) 
    {
    [INDENT]System.out.println("Resizing the array from " + A.length + " to " + A.length * 2);[/INDENT]
    [INDENT]A = new int[A.length* 2];[/INDENT]
    }
    In the code, as son as the exception is caught, your array re-initializes and the previous data is gone. So before re-initializing you need to hold your array with some temp array. Once your array is re-initialized with the new size copy the data from temp array to your Array A.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by theBurgh22 View Post
    Great, thank you! I got that part to work. I now need to add the integer values to an array, while ignoring the non-integers and not incrementing the count variable. I also need to double the array size when needed. I'm not getting the output that I should be getting with my code. I should have 13 items to print out, which are all the integers in the file. However, I am only getting 11, and the first 10 are zeros, and the last is 550.
    Why don't you use an ArrayList<Integer> and add a new number when everything is ok? The ArrayList takes care of all those nasty details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    So I got the numbers to print out, but for some reason it won't count both of the "600" values in the file and it won't print them out either and I have no idea why. I change my code to this...

    Java Code:
    				catch (ArrayIndexOutOfBoundsException dbl) {
    					System.out.println("Resizing the array from " + A.length + " to " + A.length * 2);
    					int [] temp = new int[A.length];
    					for (int index = 0; index < A.length; index++)
    						temp[index] = A[index];
    					A = new int[temp.length * 2];
    					for (int j = 0; j < temp.length; j++)
    						A[j] = temp[j];
    				}

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by theBurgh22 View Post
    So I got the numbers to print out, but for some reason it won't count both of the "600" values in the file and it won't print them out either and I have no idea why. I change my code to this...

    Java Code:
    				catch (ArrayIndexOutOfBoundsException dbl) {
    					System.out.println("Resizing the array from " + A.length + " to " + A.length * 2);
    					int [] temp = new int[A.length];
    					for (int index = 0; index < A.length; index++)
    						temp[index] = A[index];
    					A = new int[temp.length * 2];
    					for (int j = 0; j < temp.length; j++)
    						A[j] = temp[j];
    				}
    You're resizing the array alright, but what happened to the array store that caused this exception? It still needs to be stored in the (new and longer) array.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    anubhavranjan is offline Member
    Join Date
    Sep 2009
    Location
    Bangalore, India
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    catch (ArrayIndexOutOfBoundsException dbl) 
    {
    	System.out.println("Resizing the array from " + A.length + " to " + A.length * 2);
    	int [] temp = new int[A.length];
    	for (int index = 0; index < A.length; index++)
    		temp[index] = A[index];
    	A = new int[temp.length * 2];
    	for (int j = 0; j < temp.length; j++)
    		A[j] = temp[j];
    }
    Try changing your code by saving the value of the last item in the above code after copying all the values...
    Java Code:
    A[i]=nextInt;
    i++;
    Cos whenever it throws an exception, the storing of that value gets skipped and the next value comes in.

  11. #11
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    Okay, awesome! It's working perfectly now. Thank you both for your help.

  12. #12
    anubhavranjan is offline Member
    Join Date
    Sep 2009
    Location
    Bangalore, India
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Try/catch block
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 02:32 PM
  2. Try- catch block + return + IO package
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 10:09 AM
  3. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:56 PM
  4. Try Catch block issues
    By kewlgeye in forum New To Java
    Replies: 11
    Last Post: 04-29-2008, 07:10 AM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 07:53 PM

Posting Permissions

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