Results 1 to 15 of 15
  1. #1
    bar
    bar is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default print to the next line

    I'm working on a project for school - I have a file that my program needs to read each line and it needs to indent the each line that is after a blank space and write the results into another file.

    I have no prob getting the program to read each line, determine if a line is blank and print to a new file. My issue is that I can't figure out how to tell the program to indent the line AFTER the blank line.

    This is a school project, so please DON'T post a complete program or even a bunch of code :) I really just want some direction, advice, hints, tips, etc...

    Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    how to tell the program to indent the line AFTER the blank line.
    Two issues:
    1) detect a blank line and remember it
    2) indent a line

    Which do you need help with?

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Use a variable boolean lastLineWasEmpty, before redaing the next line you set it to true or false. If it's true, indent the next line.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    bar
    bar is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    Mostly the indenting the next line.

    Here's the relevant bit
    -------------------------------------------------------------
    while (inFile.hasNextLine())
    {
    inputString = inFile.nextLine();

    if (inputString.length() < 1)
    ------------------------------------------

    I'm not sure how to tell it to write " " in front of the line next line. (But I can create a program that indents all of the blank lines lol, not terribly useful :( )

  5. #5
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    "\t" ....?
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Sno View Post
    "\t" ....?
    Yes, but the OPs question is *when* that indentation should be printed, not *what* character(s) to use.

    kind regards,

    Jos

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    how to tell it to write " " in front of the line next line
    You don't show how you are writing the lines.

    To put blanks/spaces in front of a String use the concatenation operator: +
    Java Code:
    String newString = "               " + String;

  8. #8
    bar
    bar is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    I've been printing the lines with:

    outFile.println(inputString);


    I can get it to print the line it's just read with an indent:

    outFile.println(" " + inputString);

    but I don't understand how to tell it to put that indent in front of the line after the line its just read.

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    See my previous post.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhHein View Post
    See my previous post.
    It contains no code to copy so it is worthless and so it is ignored.

    kind regards,

    Jos ;-)

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    This is a school project, so please DON'T post a complete program or even a bunch of code
    Getting old? ;)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Try using a boolean variable (declared outside the loop) indicating whether the previous line was a blank line (set as true if the line is blank, otherwise ignore it). Then, if the variable is true, indent the next line and reset the boolean (don't forget the reset or everything after the first blank line will be indented :eek:)

    Edit: Lol, PhHein said it first. Just didn't see his one-line post (^.^)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    I guess I'm too subtle.
    1) detect a blank line and remember it

  14. #14
    bar
    bar is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    I have no problem having the program detect and remember a blank line. What I'm unclear on is how to then tell it to then go to the next line and write an indent before it.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    That sounds like you need a if statement. If there was a blank line, add something to the front of the next line to output. Otherwise leave it as is.

Similar Threads

  1. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  2. Print to next Line
    By singularity in forum New To Java
    Replies: 13
    Last Post: 09-10-2009, 10:03 AM
  3. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM
  4. print line
    By kazitula in forum Java Applets
    Replies: 2
    Last Post: 01-26-2008, 03:05 PM
  5. how to print output on same line in 'while loop'?
    By acidblue in forum New To Java
    Replies: 5
    Last Post: 12-13-2007, 03:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •