Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33

Thread: Please Help

  1. #21
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  2. #22
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Does this code only output the last line of the poem to the datafile?
    When I run it in Ready to Program Java IDE, it doesn't run.

    Quote Originally Posted by Eranga View Post
    I don't know what you are doing on the Console class. So I've put all output to the console here.

    Java Code:
    public class NumberedPoem {
        
        static Scanner input = new Scanner( System.in);
        
        public static void main (String[] args) throws IOException {
    
            // Declare variables
            String fileName, line;
            int number;
            number = 0;
    
    
            System.out.print("What is the name of the data file? "); // Open up Poem
            fileName = input.nextLine();
    
            BufferedReader bufReader;
            bufReader = new BufferedReader (new FileReader (fileName));
    
            while ((line = bufReader.readLine ()) != null) { //Loop terminates at end of file
                number++;
                System.out.println(number + " " + line);
                writeToFile(line);
            }
        }
        
        public static void writeToFile(String textLine) throws IOException {
            PrintWriter output;
            output = new PrintWriter (new FileWriter ("C:/Poem2.txt"));
            
            output.write(textLine);
            output.write(System.getProperty("line.separator")); // New line
            
            output.close();
        }
    }

  3. #23
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you try it what happen? I think you only get the last line of the original file to the new one, is it?

    You cannot identify why it's happen?

    Look at the following two lines of code of the writeToFile() method.

    Java Code:
            PrintWriter output;
            output = new PrintWriter (new FileWriter ("C:/Poem2.txt"));
    Once read a line from the original file you call that writeToFile() method with it. Each time you initiate a new PrintWriter object, and what happen is new file is created and replace the original.

  4. #24
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    How am I suppose to edit the code to show the whole poem?

    Thanks.

    Quote Originally Posted by Eranga View Post
    Did you try it what happen? I think you only get the last line of the original file to the new one, is it?

    You cannot identify why it's happen?

    Look at the following two lines of code of the writeToFile() method.

    Java Code:
            PrintWriter output;
            output = new PrintWriter (new FileWriter ("C:/Poem2.txt"));
    Once read a line from the original file you call that writeToFile() method with it. Each time you initiate a new PrintWriter object, and what happen is new file is created and replace the original.

  5. #25
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Initiate the PrintWriter object only once. You can do it within the constructor. Define it in class level.

  6. #26
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Could you please post the code to this?

    I have very little understanding of what is going on.

    Right now I think my PrintWriter object is only initiated once?

    Quote Originally Posted by Eranga View Post
    Initiate the PrintWriter object only once. You can do it within the constructor. Define it in class level.

  7. #27
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Better to read more about those things before work out.

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class NumberedPoem {
        
        static Scanner input;
        static PrintWriter output;
        
        public NumberedPoem() throws IOException {
            input = new Scanner( System.in);
            output = new PrintWriter (new FileWriter ("C:/Poem2.txt"));
        }
        
        public static void main (String[] args) throws IOException {
    
            // Declare variables
            String fileName, line;
            int number;
            number = 0;
    
    
            System.out.print("What is the name of the data file? "); // Open up Poem
            fileName = input.nextLine();
    
            BufferedReader bufReader;
            bufReader = new BufferedReader (new FileReader (fileName));
    
            while ((line = bufReader.readLine ()) != null) { //Loop terminates at end of file
                number++;
                System.out.println(number + " " + line);
                writeToFile(line);
            }
        }
        
        public static void writeToFile(String textLine) throws IOException {
            output.write(textLine);
            output.write(System.getProperty("line.separator")); // New line
            output.flush();
        }
    
    }

  8. #28
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Thanks for your help...only problem is Ready to Program Java IDE does not support the import java.util.Scanner;

  9. #29
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I don't know why your application doesn't support default JDK packages. Did you get any error messages?

  10. #30
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Yes I did. "The import 'java.util.Scanner' is not vaild, since it does not name a type in a package".

    Quote Originally Posted by Eranga View Post
    I don't know why your application doesn't support default JDK packages. Did you get any error messages?

  11. #31
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default A Classpath problem?

    Ready to Program Java IDE does not support the import java.util.Scanner
    It simply can't be :D

    Maybe you're IDE didn't recognize your JDK installation. Check your CLASSPATH. Do you have any problem with the java.io classes?

  12. #32
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  13. #33
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Holtsoft Ready to Program Java IDE

    I have the installer, however I cannot upload it to the forum.

    If you are interested in looking at it, send me your e-mail by PM.

Page 2 of 2 FirstFirst 12

Posting Permissions

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