Results 1 to 8 of 8
Thread: Need help with a loop statement
- 11-03-2008, 01:47 AM #1
Member
- Join Date
- Sep 2008
- Location
- Florida
- Posts
- 7
- Rep Power
- 0
Need help with a loop statement
Hello all,
Thank you for reading first of all.
I am trying to complete a program that reformats a line and sends it to another file in the correct format. Specifically moving the "{" from a line by itself to the end of the previous line.
My only problem is printing the lines. I need the program to only print the corrected lines. Not both the corrected and current lines.
I know where the problem lies. In the statement:
previousLine = currentLine;
However, I don't know how to fix it in order for it to return to the statement String currentLine = scanner.nextLine(); and print the previousLine.
This is the entire program and the text in different colors is the loop.
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.*;
public class Reformating
{
public static void main(String[] args) throws FileNotFoundException
{
// read number from user
String fileName = JOptionPane.showInputDialog(null,
"Enter a file name",
"User Input",
JOptionPane.QUESTION_MESSAGE);
// read file from input
Scanner scanner = new Scanner(new File(fileName));// contructing file type file
// above is the same as the two line code
// write to this file
PrintWriter printWriter = new PrintWriter(new File("testfile"));
// setup previous line
String previousLine = null;
// for loop to read next line as long as there are lines in the file
while (scanner.hasNextLine())
{
// read next line from input file
if (currentLine.trim().startsWith("{")) // if true then { found
// do something
previousLine = previousLine + " {"; // puts { in previous line
else
previousLine = currentLine;
// write previous line to output file
printWriter.println(previousLine);
// display on DOS window
System.out.println("previousLine =" + previousLine);
}
// close streams
scanner.close();
printWriter.close();
}
}
Please help me figure this out. I have tried many differnt methods but none seem to work. Thank you very much!!
Jessica
-
This doesn't compile as you've never declared the "currentLine" variable.
Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:
Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
- 11-03-2008, 01:58 AM #3
Member
- Join Date
- Sep 2008
- Location
- Florida
- Posts
- 7
- Rep Power
- 0
I have it in my program, but like you said it isn't formated properly on the forum. so I am reposting it. Thanks for the help.
Java Code:import javax.swing.JOptionPane; import java.util.Scanner; import java.io.*; public class Reformating { public static void main(String[] args) throws FileNotFoundException { // read number from user String fileName = JOptionPane.showInputDialog(null, "Enter a file name", "User Input", JOptionPane.QUESTION_MESSAGE); // read file from input Scanner scanner = new Scanner(new File(fileName));// contructing file type file // above is the same as the two line code // write to this file PrintWriter printWriter = new PrintWriter(new File("testfile")); // setup previous line String previousLine = null; // for loop to read next line as long as there are lines in the file while (scanner.hasNextLine()) { // read next line from input file String currentLine = scanner.nextLine(); if (currentLine.trim().startsWith("{")) // if true then { found // do something previousLine = previousLine + " {"; // puts { in previous line else previousLine = currentLine; // write previous line to output file printWriter.println(previousLine); // display on DOS window System.out.println(previousLine); } // close streams scanner.close(); printWriter.close(); } }
- 11-03-2008, 02:43 AM #4
Which lines are the "corrected" lines?need the program to only print the corrected lines
Comment re your coding style:
Always use {} blocks with if and else.
Never use single statements without the {}s
With {} blocks, you can put the correcting of a line and the printing of a line within the same {} block.
- 11-03-2008, 03:18 AM #5
Member
- Join Date
- Sep 2008
- Location
- Florida
- Posts
- 7
- Rep Power
- 0
-
What you really want to do is to use a StringBuilder object to build your output String. Then if you find a "{" at the start of a trimmed line, you append " {\n" to the stringbuilder object, then do a currentLine = currentLine.replace(...) to get rid of the first "{", then add the currentLine to the StringBuilder....
It would greatly simplify everything to do it this way, I think.
- 11-03-2008, 03:32 AM #7
Member
- Join Date
- Sep 2008
- Location
- Florida
- Posts
- 7
- Rep Power
- 0
Thank you for the help.
My professor just told me to do it this way. I did read on the string builder just a little while ago.
I will try it that way, but my professor told me that there is a way to figure it out the way I have it now, so only for assignment sake I would like to figure out the problem that I am having with the loop.
-
Similar Threads
-
Least To Greates[ if statement and for loop]
By kris09 in forum New To JavaReplies: 1Last Post: 08-08-2008, 06:34 PM -
Beginner's Problem on Loop/If statement
By obdi in forum New To JavaReplies: 2Last Post: 07-07-2008, 01:41 AM -
Help with if statement
By carl in forum New To JavaReplies: 1Last Post: 08-06-2007, 07:53 AM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM -
If Statement
By aDrizzle in forum New To JavaReplies: 4Last Post: 07-08-2007, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks