Re: Java Delemma

  • From: Alex Hall <mehgcap@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Mon, 28 Jun 2010 15:36:10 -0400

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

Other related posts: