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

    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.*;
    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];
    		do {
    			try {
    				inScan = new Scanner(System.in);
    				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;
    			i++;
    		}
    
    		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
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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 08:06 PM.

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

Similar Threads

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