Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Cannot get rid of File Input error

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Cannot get rid of File Input error

    import java.util.Scanner;
    import java.io.FileNotFoundException; 
    import java.io.FileReader;
    import java.io.IOException;
     import java.io.PrintWriter;
     
    public class CountryPopulationReverse{
     
       public static void main(String args[])throws FileNotFoundException {
     
          Scanner in = new Scanner(System.in);
          Scanner FileIn;
          FileReader inputFile;
          String population, country;
          int checkNum;
          PrintWriter output;
          boolean good = false;
     
         //Try catch for valid file goes here
         while(!good)
         {
          try
          {
          System.out.print("Input file: ");
          String inputName = in.nextLine();
          inputFile = new FileReader(inputName);
          FileIn = new  Scanner(inputFile);
          good=true;
          }catch ( FileNotFoundException e){ System.out.println("File not found."); }
         }
     
          System.out.print("Output file: ");
          String outputName = in.nextLine();
     
     
    		output = new PrintWriter(outputName);
     
          try
          {
             while(FileIn.hasNextLine())
                {
                   country = FileIn.next();
                   population = FileIn.next();
     
                   checkNum = Integer.parseInt(population);
                   if(checkNum<=5)
                       throw new BadDataException("Population (" + checkNum + ") is too low.");
                   if(checkNum>2000000000)
                      throw new BadDataException("Population (" + checkNum + ") is too high.");
     
                }
     
          } catch (BadDataException e) { System.out.println("Bad Data: " + e.getMessage());}
            catch (NumberFormatException e) { System.out.println(e.getMessage());}
     
     
       output.close();
     
       }
    }

    CountryPopulationReverse.java:40: error: variable FileIn might not have been initialized
             while(FileIn.hasNextLine())
                   ^
    1 error

    Everytime I move some code somewhere else to get rid of the error then another one pops up it is very frustrating.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Cannot get rid of File Input error

    Please post the exact error, including the stack trace, copied and pasted from exactly as it appears at your end. Be sure it corresponds to the version of the code you've posted, or provide updated code.

Similar Threads

  1. Replies: 19
    Last Post: March 15th, 2013, 03:11 PM
  2. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  3. How to get rid of this?
    By tai8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 12th, 2012, 07:54 PM
  4. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 12:05 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM