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.

Page 2 of 2 FirstFirst 12
Results 26 to 32 of 32

Thread: Read Text File Into Strings

  1. #26
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Read Text File Into Strings

    Hi, if i print out System.out.println("<"+s.trim()+">");
    i get an output
    <NameAttrib>
    <YearAttrib>
    <Year2Attrib>
    then the next lines
    <NameAttrib>
    <YearAttrib>
    <Year2Attrib>
    which is how i want it (all 3 parts are printing seperetly so have been split correctly)

    But now i need each of the 3 parts that are in s.trim() to be put into seperate strings like String name, String year, String year2;

    I tried:
                while (scanner.hasNextLine()) {
     
     
                		String theNextLine = scanner.nextLine();	// set the next line to a string
     
                        String regex = "(\\(|\\)|\\/)";
                        String fields[] = theNextLine.split(regex);
                		for(String s: fields){
     
                			String name = s.trim();
                			String year = s.trim();
                			String year2 = s.trim();
     
                		System.out.println("TITLE IS" + name);
                		System.out.println("\nYEAR IS" + year);
                		System.out.println("YEAR2 IS" + year2);
    }
    which outputs
    TITLE IS NameAttrib
    YEAR IS NameAttrib
    YEAR2 IS NameAttrib
    then goes onto:
    TITLE IS YearAttrib
    YEAR IS YearAttrib
    YEAR2 is YearAttrib
    then
    TITLE IS Year2Attrib
    YEAR IS Year2Attrib
    YEAR2 is Year2Attrib
    It is assigning the same data part to each String and then next time it goes round it assigns the next data part to each string...

    I also tried another way
                while (scanner.hasNextLine()) {
     
                		String theNextLine = scanner.nextLine();	// set the next line to a string
     
                        String regex = "(\\(|\\)|\\/)";
                        String fields[] = theNextLine.split(regex);
                		for(String s: fields){
     
                			String name = fields[0];
                			String year = fields[1];
                			String year2 = fields[2];
     
                		System.out.println("TITLE IS" + name);
                		System.out.println("\nYEAR IS" + year);
                		System.out.println("YEAR2 IS" + year2);
    }
    this is a little better and assigns the correct data part to each string
    but for example if the datafile has example data:
    aname (2012) 2012
    it will print out aname, (2012) 2012 twice, so assiging the same data to the Strings twice.

    I need it to simply do like the example directly above what i am saying now, but do each data only once as it is just repeated data.

    Thanks
    Last edited by Khadafi; January 12th, 2012 at 03:48 PM.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Read Text File Into Strings

    You do not need a loop. You never use the variable s, you explicitly access the three elements of the array.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Khadafi (January 12th, 2012)

  4. #28
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Read Text File Into Strings

    Oo thanks =), thats great. Such a silly mistake on my side, I dont know why I didnt realise.

    Thank you very much.

  5. #29
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Read Text File Into Strings

    Hi, I have got it working where I can read in data from a file and store it into strings.
    e.g. the format Name year year2

    but the file that i need to import has data before the actual data like:

    "
    File: something.list Date: Fri Oct 23 17:00:00 2009

    Copyright Something All rights reserved.

    Something (Some)

    something.list

    2006-11-18

    -----------------------------------------------------------------------------

    SOMETHING LIST
    ===========

    aname (2005) 2005
    anothername (2006) 2006
    "

    is there a way I can make my program skip all the stuff and start reading at aname....

    Thanks

  6. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Read Text File Into Strings

    make my program skip all the stuff and start reading at aname
    No. You have to read and ignore/skip over everything you do not want to use.
    Just like if you had a pile of magazines and only wanted to read the one about fishing. You would have to look at each one in turn until you got to the one you wanted.

  7. #31
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Read Text File Into Strings

    Thanks, that makes sense.

    But what is the way to skip over some parts, like maybe the first 10 lines etc.

    Thanks.

  8. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Read Text File Into Strings

    How do you skip the first 10 magazines in a pile of magazines? Pick each one up and discard it.
    For lines in a file, read 10 lines and ignore them. You could use a loop

Page 2 of 2 FirstFirst 12

Similar Threads

  1. What is the best way to read from a text file?
    By Kerr in forum File I/O & Other I/O Streams
    Replies: 20
    Last Post: January 4th, 2012, 05:41 PM
  2. Read text file
    By cardamis in forum Java IDEs
    Replies: 2
    Last Post: November 5th, 2011, 12:59 AM
  3. Replies: 8
    Last Post: March 25th, 2011, 03:34 PM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. reading content of the text file, splitting it into strings
    By Dodo in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 6th, 2011, 07:57 PM