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 11 of 11

Thread: Trouble reading a Text File

  1. #1
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trouble reading a Text File

    Hello All.

    I am having trouble reading data from a text file. I want to read a bit of text, store it (to manipulate later), then do this with each block of text.

    Once all the data has been read, (each block seperately), I would like to manipulate the data from each block that has been read. I don't know how to do this and I have spent quite a few days trying to work it out.

    Any ideas?

    Thanks for your help, any questions please ask.

    PS: I am new to this, so be nice.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble reading a Text File

    Can you post the code you are using to read the file and explain what the problems are?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble reading a Text File

    My code isn't right. I am not doing it right. I do not know what I am doing. I know how to read lets say 4 different numbers from a text file (5 6 30 500), but what I don't know how to do it read Blocks from a text File. Like the example I gave above.

    I need to read the text from each block, and do calculations.
    I want the program to know there is 2 lines, because the number at the top is how many lines below there is. Then I will be doing simple calculations, 36 * 7 + (how ever many lines there are in that certain block), so for Block 3, 36 * 7 + 36 * 7. I want to save this information, then compare it to another number that will be manually input by a user.

    I know how to read the file, I know how to do the calculation, I know how to give the user the option to input the number, and I know how to print it all out.

    I have this, but I don't know if it is right:

    		for (int i = 0; i < 53; i++)             
     
    ([B]I have the number 53 there because there is 53 lines of text in the whole file (I do not know if this is right [/B]))
    		{
     
    			number_of_lines = inFile.nextInt();
    			number_left = inFile.nextDouble();
    			number_right = inFile.nextDouble();
    			number_left = number_left + inFile.nextDouble();
    			number_right = number_right + inFile.nextDouble();
     
    			if (inFile.next() == null)
    			{
    		} 
     
    		// Calculations
    		wage = number_left * number_right;

    Sorry about my confusing way of explaining things.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble reading a Text File

    The code you've posted does not read the text from the file line by line. The methods you are using read tokens from the file ignoring the end of line character. The data could be like this:
    Block 1 4 32 8 38 6 38 6 16 7 Block 2 0

    If you want to know what lines the data is on, use the nextLine() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble reading a Text File

    I really don't understand what you are saying. Argh, this is so frustrating.

    EDIT: OK I understand now. You are saying my code is reading it as it comes, not line by line. Why have you said use the nextLine() method.

    I want it to read from Block 1 until the empty line, or the start of Block 2, and so on.

    I am looking online but I can't find anything that makes sense to me.

    Sorry if I don't make sense. Please be patient,

    EDIT #2: I was thinking if it read all of block 1, do all of the calculations in block 1, then save it as a String. Then do the same for block 2, until the last block. Then maybe I could compare these strings to the "inputted number".
    -Does that make sense?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble reading a Text File

    my code is reading it as it comes, not line by line
    Yes, that is what I meant.

    Try writing a test program that reads from the file using nextLine() and prints out every line as it is read so you can see what the method does.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble reading a Text File

    Ok. Thank you. I will try this now and get back to you. Thanks again!

    --- Update ---

    OK. I sort of understand the nextLine() method.

    I need to know how to break a line into two so we can store them as separate variables or strings.

    What do you think? Is that possible? Thanks.

    --- Update ---

    OK. I sort of understand the nextLine() method.

    I need to know how to break a line into two so we can store them as separate variables or strings.

    What do you think? Is that possible? Thanks.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble reading a Text File

    how to break a line into
    The String class's split() method can be used to break a line into parts.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble reading a Text File

    Thank you Sir. I do appreciate all your help. I will let you know how I get on. Thanks again.

  10. #10
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble reading a Text File

    When you mention the split() method, would that mean that I am using array's? Is there anyway of reading the data and assigning each value to a variable to do a calculation for each line considering these variables vary in terms of int's and double's?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble reading a Text File

    Is there anyway of reading the data and assigning each value to a variable
    The split() method will separate the tokens on a String into separate Strings that can be converted to numeric values.

    Another way to separate the tokens in a String into numbers is by putting the String into a Scanner class constructor and then calling the various Scanner class methods to retrieve the data as numbers.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading into an ArrayList from a text file
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 04:32 PM
  2. Replies: 6
    Last Post: June 18th, 2012, 04:54 AM
  3. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  4. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  5. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM