Can anyone help debug my java program?

Discussion in 'OT Technology' started by an0nymous, Nov 2, 2005.

  1. an0nymous

    an0nymous Anon E. Mouse

    Joined:
    Jan 7, 2005
    Messages:
    710
    Likes Received:
    0
    Well, I know what's wrong with it. I just don't know how to fix it.

    the assignment:

    my code:

    Code:
    import java.util.Scanner;
    import java.io.*;
    
    
    public class Copy
    {
        public static void main(String[] args)
        {
            // scan: user input reader
            Scanner scan = new Scanner(System.in);
            // fScan: read file streams
            Scanner fScan = new Scanner(System.in);
            
            // get file name
            System.out.print("Enter file name: ");
            String fName = scan.nextLine();
            
            
            try
            {
                fScan = new Scanner(new File(fName));
            }
            catch (FileNotFoundException e)
            {
                System.out.println("File Not Found: " + fName);
            }
        
            while (fScan.hasNext())
            {
                System.out.println(fScan.nextLine());
            }
    
        }
    }
    Now, the only way I could get the fScan to not give a "not initialized" error was to make it a new Scanner(System.in). Anyone know another way to do that?

    It reads it fine if the file exists, and if it doesn't exist it just keeps it as a System.in reader, and echos what you enter.

    I've tried putting loops around every possible code block combination there is, but I can't figure it out.

    Any ideas? :hsd:
     
  2. an0nymous

    an0nymous Anon E. Mouse

    Joined:
    Jan 7, 2005
    Messages:
    710
    Likes Received:
    0
    Anyone? :x: :x:
     
  3. colosodian

    colosodian Next Gen

    Joined:
    Oct 16, 2004
    Messages:
    1,110
    Likes Received:
    0
    Location:
    SF Bay
    I havent done any java in months, but it looks like your initializing fscan twice.

    what functionality is left to implement? The asking for a new file?
     
  4. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Put the while loop inside the try {} block. If no file is found, then the stuff in the while loop gets executed with the original fScan (declared on System.in). This is also why it gives you a not initalized error.
     
  5. an0nymous

    an0nymous Anon E. Mouse

    Joined:
    Jan 7, 2005
    Messages:
    710
    Likes Received:
    0
    Thanks so much :bowdown:

    I knew it was something simple. I put the hasNext() while inside the try, wrapped that all in a do/while based on a boolean and in the catch I asked for input again :hsd::hsd:
     
  6. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    I would recommend doing this the other way. Put the try block inside a while or a do-while loop. First, initialize your scanner for the text input. Then start the loop and read the line the user entered. Then start your try block and put ALL of the file access in this block. If a FileNotFoundException is thrown, just return to the start of the loop and do it all over again. It will ask for the file name and try to read it. If you successfully complete the file access and print out all the data, break out of the loop.
     
  7. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    That's actually how I would do it to, but for the simplicity of using the code he already has, I suggested the opposite.
     

Share This Page