Welcome to Dream.In.Code
Become a Java Expert!

Join 150,163 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,361 people online right now. Registration is fast and FREE... Join Now!




booleans

 
Reply to this topicStart new topic

booleans, need boolean to check for blank lines

j_delong
27 Aug, 2008 - 02:10 PM
Post #1

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 8

Working on another project. I have a program set up to read from a file, I then need to insert indents into the paragraphs. The problem is I need the program to check for blank lines because my first paragraph is not indenting nor my last paragraph (because there were 2 blank lines before it.) From what I am told from others the easiest way to do this is to set a boolean to true if it finds a blank line and then have it print the blank spaces for indents. How do I set a boolean expression and where does it go in the program? I have printed out a lot of material on booleans and it still isn't making any sense to me.

[code]
import java.io.*;
public class History
{
public static BufferedReader inFile;
public static PrintWriter outFile;

public static void main(String [] args) throws IOException
{
String line;
int index;

inFile = new BufferedReader(new FileReader("history.dat"));
outFile = new PrintWriter(new FileWriter("historyd2.out"));
line = inFile.readLine();


while (line !=null)
{

index = line.length();
{
if (index<1)
{
line = inFile.readLine();
outFile.println(" " + line);


}
else

outFile.println(line);
line = inFile.readLine();
}
}


outFile.close();
inFile.close();
}
}
[code]
User is offlineProfile CardPM
+Quote Post

pbl
RE: Booleans
27 Aug, 2008 - 02:32 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(j_delong @ 27 Aug, 2008 - 03:10 PM) *

Working on another project. I have a program set up to read from a file, I then need to insert indents into the paragraphs. The problem is I need the program to check for blank lines because my first paragraph is not indenting nor my last paragraph (because there were 2 blank lines before it.) From what I am told from others the easiest way to do this is to set a boolean to true if it finds a blank line and then have it print the blank spaces for indents. How do I set a boolean expression and where does it go in the program? I have printed out a lot of material on booleans and it still isn't making any sense to me.

Don't really need a boolean. Just print (not println) when you have a blank line
CODE

import java.io.*;
public class History
{
public static BufferedReader inFile;
public static PrintWriter outFile;

public static void main(String [] args) throws IOException
{
   String line;

   inFile = new BufferedReader(new FileReader("history.dat"));
   outFile = new PrintWriter(new FileWriter("historyd2.out"));
   line = inFile.readLine();    

   outFile.print("   ");             // beginning
   while (line !=null)
   {
      if(line.trim().length() == 0)    // trim to remove blanks
     {
            // emptyline
            outFile.println();          // print empty line
            outFile.print("   ");       // beginning of following one
      }
      else
      {
             outFile.println(line);     // regular line
       }
       line = inFile.readLine();
   }
}


outFile.close();
inFile.close();
}
}



This post has been edited by pbl: 27 Aug, 2008 - 02:34 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:51AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month