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

Thread: reading from text file produces extra s p a c e s, even on numbers 1 0 1

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default reading from text file produces extra s p a c e s, even on numbers 1 0 1

    I have a test.txt file with the following in it:
    MapTile,1,256,false
    MapTile,9,291,false

    no matter what way I try to read this file in java, the result is:
    M a p T i l e  ,  1  ,  2 5 6  ,  f a l s e
    M a p T i l e  ,  9  ,  2 9 1  ,  f a l s e


    String tokens[] = line.split("[,]"); //results in the following:
    " M a p T i l e ", " 1 ", " 2 5 6 ", " f a l s e "

    when the following is expected:
    "MapTile", "1", "256", "false"

    treating it to any method of removing all the white spaces fail, what exactly are these characters?

    there is no way i can use the numbers or booleans in this (read as strings)
    console says:

    NumberFormatException: for input String: "2 5 6"

    and goes on to specify the line where I try to
    int num = Integer.parseInt(tokens[2]);

    I have searched the net for hours on this.

    Please help.
    Last edited by Spidey1980; September 3rd, 2011 at 07:25 PM.


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

    Default Re: reading from text file produces extra s p a c e s, even on numbers 1 0 1

    Can you show the code you are using to read the file.
    Add a println to print out the String that was read immediately after it is read to show what was read from the file.
    How did you create the txt file? Can it contain unicode characters vs ASCII?
    Do a properties on the file and see how many bytes the file contains. Does the byte count match the character count?

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: reading from text file produces extra s p a c e s, even on numbers 1 0 1

    never mind I rebuilt my loader and my parser works perfect now!

    unicode, specified it in the writer but not the reader...

    using IOOperations.java sub class from Java programming for the absolute beginner, it did not have Unicode Specified on both the writer and the reader. I fixed that, and it now works.
    Last edited by Spidey1980; September 3rd, 2011 at 09:21 PM.

Similar Threads

  1. How to generate 13 digit numbers to text file
    By duanedtp in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 11:30 AM
  2. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM
  3. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  4. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  5. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 06:16 PM