Results 1 to 11 of 11
  1. #1
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Reading from file problem

    I am working with the following code to read from an existing .txt file. The highlighted code below is my error check to see if the file exists and proceed with the following code. The file exists and

    Java Code:
    public class ReadIt {
    
    					
    	public static void main(String[] args) throws IOException {
    		// Going to calculate how long it will take me to earn the lazy's their gold
    		myIntro.PrintIntro();
    				
    		File inFile = new File ("totalHours.txt");
    			
    		if(inFile.exists())
    	[highlight]System.out.println("It exists");[/highlight]
    		
    		Scanner meh = new Scanner(inFile);
    		String line;
    		
    				while(meh.hasNext())
    					{
    	//********the next line of code does not process from text file*****			
                                                    line = input.nextLine();
    						System.out.printLine(line);
    						}
    			
    		meh.close();

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    line = input.nextLine();
    This does not compile.

  3. #3
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default A follow-up question

    Thanks Junky. I have a textbook with code to read from a text file and I have followed it exactly. I am using eclipse to run and test the code and I do not get any compile errors. So, I am at a loss. Any suggestions would be appreciated.

    Thanks again.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There is something very wrong about the code Junky showed you.

  5. #5
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default My error

    You are right that that line does not compile. Even corrected, the line "line = input.next(); does not process. If I type in information in the console window, it accepts input. Why isn't it reading from the text file if the program acknowledges that it exists?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What did you change that line to?

  7. #7
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default My change

    I am sorry for the confusion. I saw the System.out.printLine() should have been System.out.println() and that is what I changed. I still have the same problem

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    The variable "input" does not exist. You called your Scanner "meh".

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Look at junky's post #2, have you changed that line? If so, to what?

  10. #10
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Got it

    I changed input to meh and it works. Thank you both for your patience and help.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No problem, I am glad you solved it, you need to be meticulous about checking for matching variable names, and punctuation.

    If you are done, please mark your thread solved with the thread tools at the top.

Similar Threads

  1. problem reading file
    By jmoutia in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 03:19 AM
  2. Problem reading from a file
    By sarapeace in forum New To Java
    Replies: 13
    Last Post: 10-03-2010, 02:08 PM
  3. Replies: 26
    Last Post: 05-31-2010, 09:05 PM
  4. Reg: File Reading Problem
    By balaji csc in forum New To Java
    Replies: 0
    Last Post: 11-06-2009, 04:22 PM
  5. Problem with reading text from a .txt file
    By Gigi in forum New To Java
    Replies: 40
    Last Post: 01-22-2009, 03:22 AM

Tags for this Thread

Posting Permissions

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