Re: Java Delemma

  • From: "Gilbert Neiva" <gneiva@xxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 28 Jun 2010 19:33:09 -0600

Here is the entire class.

import java.io.*; // importing input.output classes
import java.util.Scanner;

public class History
{
static Scanner inFile; // Object that will read from file
static PrintWriter outFile; // object that will print to file

static void printHistory() throws IOException
// Start of void method
{
 String inputString; // String for line of text in file
 final String blanks = "     ";
 // Five blanks inserted at beginning of paragraphs
 inFile = new Scanner(new FileReader("history.d1"));
 // Instantiating new scanner and new filereader object with
 // history.d1 as argument
 outFile = new PrintWriter(new FileWriter("history.d2"));
 // Instantiating new printWriter object
 // It will be used to write to a file
 inputString = blanks+inFile.nextLine();
 outFile.println(inputString);
 // Printing 5 spaces plus line
 // to file

 while (inFile.hasNextLine())
 {
  inputString = inFile.nextLine();

  if (inputString.length() < 1)
  {
  outFile.println("\n");
  inputString = blanks+inFile.nextLine();
  }
  outFile.println(inputString);
  // Printing line of text to file
 }
 inFile.close();
 outFile.close();
 // Closing files
}

public static void main(String[] args) throws IOException
{
 printHistory();
 // Calling method printHistory()
}
} // End of class

Gilbert Neiva

----- Original Message ----- From: "Alex Hall" <mehgcap@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Monday, June 28, 2010 6:58 PM
Subject: Re: Java Delemma


I don't follow. Could you post the entire loop so we can see what is
going on? Also, comments are welcomed! Remember that in Java, two
slashes mean that the rest of the line is a comment, so you could put
this in your file:
if(1a==0){ //check that a is really 0
System.out.print("var a is 0!"); //tell user
}//end if

On 6/28/10, Gilbert Neiva <gneiva@xxxxxxx> wrote:
I succeeded in making my application that inputs a file, prints 5 spaces at the beginning of a paragraph and prints to a file with the new changes. Now I have another problem. The last paragraph of the input file has two blank
lines above it. When my application runs and creates the output file, the
last paragraph of the output file has two lines above it, but the paragraph
is not indented. The blank line just above the paragraph has 5 space
characters in it. The rest of the output file is fine. The paragraphs in the output file that have no indent issues have one blank line above them which
is what I want. I made an if statement inside a while statement to detect
the blank lines and print an extra blank line to the output file as well as
store the new paragraph indented 5 spaces in a string varriable. The if
statement is as follows.

if inputString.length() < 1
{
outFile.println("\n");
inputString = inFile.nextLine();
}

What do I do to correct my last paragraph so it is indented properly, but I
still have two blank lines above it?

Gilbert Neiva

----- Original Message -----
From: "Alex Hall" <mehgcap@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Monday, June 28, 2010 1:36 PM
Subject: Re: Java Delemma


You want to use getNext(), not hasNext(). HasNext is a test to see if
any lines remain; you might say something like
while(file.hasNextLine()){
txt+=file.getNextLine();
}
Please note that it has been some time since I did Java, so the method
may not be called getNext, but it is something similar which you can
find in the docs.
The error is because hasNext returns true or false (a boolean) where
getNext returns an actual string, which is what you want.


On 6/28/10, Gilbert Neiva <gneiva@xxxxxxx> wrote:
I'm trying to write an application which takes an input file, and adds 5 blank spaces at the beginning of each paragraph. Then the application is
to
write to an output file with the new changes. The application will detect the beginning of a new paragraph because there is a blank line before it.

I
test for the blank using the following if statement.

if inputString.length() < 1
{
inputString = blanks+inFile.hasNextLine();
}

When I tried to compile the application using the javac program, I get
the
following error.

History.java:23: incompatible types
found   : boolean
required: java.lang.String
inputString = inFile.hasNextLine();
^
1 error

The source code for the application is below.

import java.io.*; // importing input.output classes

import java.util.Scanner;


public class History

{

public static Scanner inFile;

public static PrintWriter outFile;


public static void main(String[] args)

{

String inputString; // Input string from a line in file

final String blanks = " ";

// Five blanks inserted at beginning of each paragraph

inFile = new Scanner(new FileReader("history.d1"));

// Instantiating new scanner and new filereader object with

// history.d1 as argument

outFile = new PrintWriter(new FileWriter("history.d2"));

// Instantiating new printWriter object

// It will be used to write to a file

inputString = blanks+inFile.hasNextLine();

outFile.println(inputString);


while (inFile.hasNextLine())

{

inputString = inFile.hasNextLine();


if (inputString.length() < 1)

{

inputString = blanks+inFile.hasNextLine();

}

outFile.println(inputString);

}

inFile.close();

outFile.close();

}

}



What am I doing wrong?



Gilbert Neiva





--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind




--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: