Results 1 to 8 of 8
  1. #1
    sunshine39 is offline Member
    Join Date
    Sep 2008
    Location
    Florida
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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]

  3. #3
    sunshine39 is offline Member
    Join Date
    Sep 2008
    Location
    Florida
    Posts
    7
    Rep Power
    0

    Default

    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();
    
    
    
    
      }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    need the program to only print the corrected lines
    Which lines are 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.

  5. #5
    sunshine39 is offline Member
    Join Date
    Sep 2008
    Location
    Florida
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Which lines are 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.

    The entire program is in my second post on this thread.

    So do you mean seperating the if and else with {} blocks

    example:
    if (adlkfjad) {
    // do something }
    else {
    // do something }

    ?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    sunshine39 is offline Member
    Join Date
    Sep 2008
    Location
    Florida
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    well, you can do most anything with a String that can be done with a StringBuilder, and the general concepts needed are the same.

Similar Threads

  1. Least To Greates[ if statement and for loop]
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 06:34 PM
  2. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 01:41 AM
  3. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM
  5. If Statement
    By aDrizzle in forum New To Java
    Replies: 4
    Last Post: 07-08-2007, 08:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •