Re: Java Delemma

  • From: "Gilbert Neiva" <gneiva@xxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 30 Jun 2010 18:35:01 -0600

Guess what alex and everyone else. I finally got my application to work right. I checked the History.d2 file after my millionth atempt at making my source code just right, and the paragraphs are indented properly, there are no extra blank lines anywhere, and only the beginnings of paragraphs are indented. The bodies of paragraphs are properly formatted. My new source code is below.


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)
  {
   inputString = inFile.nextLine();
   if (inputString.length() < 1)
  {
   inputString = inFile.nextLine();
  outFile.println("\n");
  inputString = blanks+inputString;
  outFile.println(inputString);
  }
   else
   {
   outFile.println("\n");
    inputString = blanks+inputString;
    outFile.println(inputString);
   }
  }
  else
  {
   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: Tuesday, June 29, 2010 7:26 AM
Subject: Re: Java Delemma


I saw that that would happen when I read the program; I assumed that
you wanted that to happen. The problem is that you insert blanks for
every itteration of the loop that has a line whose length is greater
than 0. You must implement a checker to see if the previous line was
of length 0. I will leave the details up to you, as this is a great
exercise, but consider recording the previous line's length in an int
and checking that...

On 6/29/10, Stanzel, Susan - Kansas City, MO <susan.stanzel@xxxxxxxxxxxx> wrote:
Hi Listers,

Please don't forget about the java list at program-java@xxxxxxxxxxxxxx There
are many great folks on that list.

Susie Stanzel

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Gilbert Neiva
Sent: Tuesday, June 29, 2010 7:01 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: Java Delemma

Hey Alex. It seems I spoke to soon. It seems that when I checked the
history.d2 file a little more closely, I found that every single line was
indented, not just the beginning of paragraphs. The history.d1 was written
wwith every line written fresh, that is to say, one line was written and
then the author pushed enter and wrote another line and so on. Is there a
way to correct for this so that just the beginning of paragraphs are
indented and not everything else?

Gilbert Neiva

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


Glad it worked, or mostly worked. I am not sure why you would get two
lines right before the final paragraph...

On 6/28/10, Gilbert Neiva <gneiva@xxxxxxx> wrote:
Hi Alex. I just found out that the output file doesn't  need two lines
above
the last paragraph. As a result I made the following if and else if
statements.

if (inputString.length() < 1)
   {
    outFile.print("\n");
    }
   else if (inputString.length() > 0)
   {
    inputString = blanks+inputString;
    }

When I checked the outfile.d2 file, the last paragraph still had two
lines
above it, but at least the last paragraph was properly indented. Here is
my
new 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.print("\n");
    }
   else if (inputString.length() > 0)
   {
    inputString = blanks+inputString;
    }
   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 8:30 PM
Subject: Re: Java Delemma


And the problem is that the final paragraph does not get the indent...
What if you changed the if statement to something like this:
if (inputString.length() < 1)
{
outFile.println("\n");
inputString = blanks+inputString;
}

I say this because, and I could be wrong here, each call to a file
object's nextLine() method moves you down a line in the file, as
though you had pressed the down arrow (assuming the arrows moved you
by full lines, not just as many words as are on one line of a
monitor). When you assign inputString to inFile.nextLine(), you press
this hypothetical down arrow. Then, when you assign inputString to
blanks+inFile.nextLine(), you are pressing the arrow again, so the
line may not be correct. I am not sure if that will do it, but it is
the only thing I see. Someone on the list will likely be able to
direct you better.


On 6/28/10, Gilbert Neiva <gneiva@xxxxxxx> wrote:
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




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

__________
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: