Results 1 to 3 of 3
  1. #1
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Trouble with Try Catch blocks and file reading.

    Hello there,
    I had to improve this code in order for it to compile correctly and run in a fault tolerant way. I need my code to ask the user to enter a file to read from. If the file they enter isn't found, I need to repeatedly ask the user to re-enter a file name. Once a file is found, it should read in each line of the file and store them in an array. However, if a value is not an integer, it should be ignored and not stored in the array. Finally, we must double the size of the array each time the number of integers in the file is larger than the current size of the array. I am only worried about the first part for now (repeating the question as many times as needed).

    Here is my code..

    Java Code:
    import java.util.*;
    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];
    		do {
    			try {
    				inScan = new Scanner(;
    				System.out.println("Please enter the file to read from: ");
    				fName = inScan.nextLine();
    				fScan = new Scanner(new File(fName));
    			catch (Exception bad) {
    				fName = null;
    		// not sure what to compare fName to in order to have the question
    		// above repeat as many times as needed if the file the user enters
    		// is not valid
    		} while (fName.equals());
    		while (fScan.hasNextLine())
    			nextItem = fScan.nextLine();
    			nextInt = Integer.parseInt(nextItem);
    			A[i] = nextInt;
    		System.out.println("Here are your " + i + " items:");
    		for (int j = 0; j < i; j++)
    			System.out.println(A[j] + " ");

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Destiny Islands
    Rep Power


    Why not use "while (!fName.exists());"?

    EDIT: Er, sorry. Create a File object from fName, then check if it exists using .exists().
    Last edited by Zack; 11-29-2010 at 09:06 PM.

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

Similar Threads

  1. Having trouble reading external txt file to an array
    By Metastar in forum New To Java
    Replies: 18
    Last Post: 07-22-2010, 12:29 AM
  2. App that blocks selected web pages.
    By lolaika in forum Advanced Java
    Replies: 3
    Last Post: 06-11-2010, 05:43 PM
  3. Java Blocks??
    By abimaran in forum New To Java
    Replies: 24
    Last Post: 11-07-2009, 04:22 PM
  4. Blocks Language 0.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 09-23-2007, 12:21 AM
  5. Task Blocks 0.5
    By johnt in forum Java Software
    Replies: 0
    Last Post: 08-08-2007, 09:43 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