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

Thread: getting the proper input from file

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default getting the proper input from file

    guys, suppose I have a text file with the following format

    name lastname gpa major advisor

    my problem here is with major which is sometimes compose of two words, but in my loop the second word of major becomes the first word of advisor... how could I get around that?
    don't tell me

    major = in.next();
    major += in.next();

    b/c some major have only one word..


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: getting the proper input from file

    You can use some character to separate each of the categories
    Such as:

    Smith#John#3.7#Computer Science#Mr. Jones

    Then you can just get the whole line and split it up at each # character

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: getting the proper input from file

    Quote Originally Posted by Parranoia View Post
    You can use some character to separate each of the categories
    Such as:

    Smith#John#3.7#Computer Science#Mr. Jones

    Then you can just get the whole line and split it up at each # character
    my file is split with "tab"... can I just do the same using "tab" instead of some other character like #?

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: getting the proper input from file

    Quote Originally Posted by mia_tech View Post
    my file is split with "tab"... can I just do the same using "tab" instead of some other character like #?
    Yes, you can. You can use the String's split method with "tab" as a delimiter.

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: getting the proper input from file

    for some reason "tab" does not space every column equally in the same line

    for example

    String data1 = "john doe 2.5 computer science programming";

    believe it or not every column is separated by a "tab" and as you can see they are not equally separated.. meaning not all columns have equal amount of blank spaces in between. Therefore when using data1.split(" ") with tab as a separator, it messes up the the input, and why is it that data1.split("\t") does not work?

  6. #6
    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: getting the proper input from file

    why is it that data1.split("\t") does not work?
    The \ character has special meaning in regular expressions. To use a \ you have to escape it.
    And that is tricky. Try this: "\\\t"

    EDIT: I just tried your code and it worked.
           String aStr = "csw\tdswws\tew322\tf";
           System.out.println(Arrays.toString(aStr.split("\t"))); // [csw, dswws, ew322, f]
    Last edited by Norm; June 17th, 2012 at 11:28 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: getting the proper input from file

    Quote Originally Posted by Norm View Post
    The \ character has special meaning in regular expressions. To use a \ you have to escape it.
    And that is tricky. Try this: "\\\t"

    EDIT: I just tried your code and it worked.
           String aStr = "csw\tdswws\tew322\tf";
           System.out.println(Arrays.toString(aStr.split("\t"))); // [csw, dswws, ew322, f]
    well, your code is not equal than mine... you actually inserted asci \t into the string.... I'm using keyboard tab. I tried escaping it but didn't work either
    Last edited by mia_tech; June 17th, 2012 at 04:33 PM.

  8. #8
    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: getting the proper input from file

    What characters are in a line in your text file?

    Print out the bytes in the String you read from the file using the following technique:
           byte[] bytes = aStr.getBytes();
           System.out.println(Arrays.toString(bytes));
    Replace aStr with the String variable that you read a line into from the file.
    Post the output here.


    I'm using keyboard tab
    If you are in an editor, the editor could replace the tab with spaces.
    Last edited by Norm; June 17th, 2012 at 04:50 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: getting the proper input from file

    Quote Originally Posted by Norm View Post
    If you are in an editor, the editor could replace the tab with spaces.
    yeah, that was the problem, I just used notepad, which is a more cleaner editor, and it all worked (first time used notepad++)

    by the way this is the output you ask for. How do you convert this back to asci characters?

    [74, 111, 104, 110, 9, 83, 109, 105, 116, 104, 9, 51, 46, 52, 9, 67, 111, 109, 112, 117, 116, 101, 114, 32, 83, 99, 105, 101, 110, 99, 101, 9, 112, 114, 111, 103, 114, 97, 109, 109, 105, 110, 103]

  10. #10
    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: getting the proper input from file

    Is your code working now?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: getting the proper input from file

    Quote Originally Posted by Norm View Post
    Is your code working now?
    yes, it is...

  12. #12
    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: getting the proper input from file

    How do you convert this back to asci characters?
    That output was so we could see what was in the file. Its source was the String of ASCII characters.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Proper way to get rid of breaks.
    By Massaslayer in forum Loops & Control Statements
    Replies: 6
    Last Post: December 23rd, 2011, 07:53 PM
  2. 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
  3. Need proper Java Lingo
    By Deceptiron in forum Java Theory & Questions
    Replies: 4
    Last Post: November 22nd, 2010, 09:39 PM
  4. How to insert a proper child in the XML Doc
    By Sarakh in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 31st, 2010, 12:36 PM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM