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

Thread: File to String help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default File to String help

    I need to read a file into a string so I can scramble to words of the file. Right now I have:

    Scanner infile = new Scanner( new FileReader( args[0] ) );

    String text = infile.nextLine();

    This works properly as it reads the first sentence from the file, but it only reads the first line. How do I make it so it reads the whole paragraph in the file????

    I would appreciate any help. Thanks


  2. #2
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: File to String help

    Hope this can help you....'
    Plz do reply again if problem persists....'
    '
    ...remove by moderator
    '
    Last edited by copeg; October 18th, 2012 at 01:37 PM. Reason: remove spoonfed code

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: File to String help

    Quote Originally Posted by beginneratjava View Post
    ...it only reads the first line. How do I make it so it reads the whole paragraph in the file????
    What delimits a "paragraph"? (That is, how can you tell the end of a paragraph?) Text processing systems like TeX/LaTex use an empty line to designate the end of a paragraph. If that's the case then, here's some pseudo-code you might consider:
    Create a Scanner object with the file name. Call it infile.
    (You have already done this.)
     
    Declare a String. Make it empty.  (For example: String file_data = "";)
     
    Repeat the following loop as long as infile has a next line:
    BEGIN LOOP
     
        Declare a String called line and set it equal to infile.nextLine();
        If the length of the line String is not equal to zero
        THEN
            Set file_data equal to file_data + the line String that you just read.
        ELSE
            Break out of the Loop;
        END IF
     
    END LOOP

    Of course if you mean to read the entire file, and not a single blank-line-delimited paragraph, you would eliminate the IF/ELSE conditional block and just append each line. The loop terminates when there are no more lines to be read.

    Some people would prefer a StringBuilder object rather than a String for file_data. You would use the StringBuilder.append() method for each line you read.


    Cheers!

    Z
    Last edited by Zaphod_b; October 18th, 2012 at 03:30 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,370
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: File to String help

    Quote Originally Posted by suyog53 View Post
    Hope this can help you....'
    Plz do reply again if problem persists....

    @suyog53, Please read the forum rules and the following: The Problem with Spoon-feeding

Similar Threads

  1. Writing string to file
    By frozen java in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 10th, 2012, 09:37 AM
  2. [jsp/jsf] Send string(file name) to other class
    By barni120 in forum JavaServer Pages: JSP & JSTL
    Replies: 13
    Last Post: February 10th, 2012, 07:48 AM
  3. Text File into String Array
    By mathanv in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 05:30 AM
  4. 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
  5. finding String in a file
    By nasi in forum Java Theory & Questions
    Replies: 12
    Last Post: May 31st, 2010, 02:16 PM