Page 1 of 2 12 LastLast
Results 1 to 20 of 33

Thread: Please Help

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

    Question Please Help

    Hello Everyone,

    Here's my assignment that I'm kind of stuck on:

    Create a program and new class named "NumberedPoem" that will read the data from the poem.dat file (created in Activity 4).

    // The "PoemData" class.
    import java.awt.*;
    import java.io.*;
    import hsa.Console;

    public class PoemData
    {
    static Console c; // The output console

    public static void main (String[] args) throws IOException
    {
    c = new Console ();

    // Declare variables
    String line[];
    int numberLines;
    int number = 0;

    // Will produce a data file called Poem with "X" number of lines in the poem
    PrintWriter output;
    output = new PrintWriter (new FileWriter ("Poem"));

    c.print ("How many lines is your poem? ");
    numberLines = c.readInt ();
    c.println();

    c.println ("Enter in the " + numberLines + " lines of your poem.");
    line = new String [numberLines];
    while (number < numberLines)
    {
    line [number] = c.readLine ();
    output.println (line [number]);
    number++;
    }
    output.close (); // Close the output file
    } // Main method
    } // PoemData class

    Once the data has been read, you are to output the data both to the screen and to a new data file (call this data file Poem2.dat), such that an associated line number appears before each line. For example:
    1 The rain in Spain

    2 Falls mainly


    Here is what I have done so far:

    import java.io.*;
    import hsa.Console;

    public class NumberedPoem
    {
    static Console c; // The output console

    public static void main (String[] args) throws IOException
    {
    c = new Console ();

    // Declare variables
    String fileName, line;

    c.print ("What is the name of the data file? "); // Open up Poem
    fileName = c.readLine ();
    c.println ();

    BufferedReader input;
    input = new BufferedReader (new FileReader (fileName));
    line = input.readLine (); //Read a line of characters

    for (int number = 1 ; number < 5 ; number += 1)
    {
    c.println (number + " " + line);

    line = input.readLine (); //Read next line
    }
    } // main method
    } // NumberedPoem class


    for (int number = 1 ; number < 5 ; number += 1) - How am I suppose to change 5 to the number of lines in the poem?

    Finally, how am I suppose to intergrate this into my program to output the file:

    PrintWriter output;
    output = new PrintWriter (new FileWriter ("Poem"));

    Thanks for your help.

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

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

    Default

    Thanks.

    // The "NumberedPoem" class.
    import java.awt.*;
    import java.io.*;
    import hsa.Console;

    public class NumberedPoem
    {
    static Console c; // The output console

    public static void main (String[] args) throws IOException
    {
    c = new Console ();

    // Declare variables
    String fileName, line;
    int number;
    number = 0;

    c.print ("What is the name of the data file? "); // Open up Poem
    fileName = c.readLine ();
    c.println ();

    BufferedReader input;
    input = new BufferedReader (new FileReader (fileName));
    line = input.readLine (); //Read a line of characters


    while (line != null) //Loop terminates at end of file
    {
    number = number + 1;
    c.println (number + " " + line);
    line = input.readLine (); //Read next line

    }
    } // main method
    } // NumberedPoem class

    Now I have to figure out how to output the file...

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

    Default

    This is not a good practice at all. You have done the same thing twice without any special requirement.

    Java Code:
    line = input.readLine (); //Read a line of characters
    
    
    while (line != null) //Loop terminates at end of file
    {
    number = number + 1;
    c.println (number + " " + line);
    line = input.readLine (); //Read next line
    
    }
    You can do the same in this way.

    Java Code:
    while ((line = input.readLine ()) != null) //Loop terminates at end of file
    {
    number++;
    c.println (number + " " + line);
    }

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

    Default

    Quote Originally Posted by Starr29 View Post
    Now I have to figure out how to output the file...
    What you mean?

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

    Default

    Thanks so much for your help. This forum is very helpful.

    I meant: "Once the data has been read, you are to output the data both to the screen and to a new data file (call this data file Poem2.dat), such that an associated line number appears before each line. For example:
    1 The rain in Spain

    2 Falls mainly"

    I know I am suppose to put this in the code somewhere??:

    PrintWriter output;
    output = new PrintWriter (new FileWriter ("Poem2"));
    Last edited by Starr29; 01-02-2009 at 05:17 AM.

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

    Default

    First of all, write a simple code separately to write some text into a text file. Use some hard coded text lines there.

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

    Default

    Since I'm a newbie, I have no idea what you mean. This is an online course that I'm taking which is very poorly explained.

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

    Default

    Ok, you requirement is read a text file line by line, and write back them to the console as well as to another text file. Is that right?

    In first part what you have done is read the line by line in the text file. Is that right?

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

    Default

    Yes. Now I have to output this into a new data file called Poem2:

    while ((line = input.readLine ()) != null) //Loop terminates at end of file
    {
    number++;
    c.println (number + " " + line);
    }


    Using:

    PrintWriter output;
    output = new PrintWriter (new FileWriter ("Poem2"));

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

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

    Default

    I have tried this, but it is not outputting:

    // The "NumberedPoem" class.
    import java.awt.*;
    import java.io.*;
    import hsa.Console;

    public class NumberedPoem
    {
    static Console c; // The output console

    public static void main (String[] args) throws IOException
    {
    c = new Console ();

    // Declare variables
    String fileName, line;
    int number;
    number = 0;

    c.print ("What is the name of the data file? "); // Open up Poem
    fileName = c.readLine ();
    c.println ();

    BufferedReader input;
    input = new BufferedReader (new FileReader (fileName));

    while ((line = input.readLine ()) != null) //Loop terminates at end of file
    {
    number++;
    c.println (number + " " + line);
    }

    PrintWriter output;
    output = new PrintWriter (new FileWriter ("Poem2"));

    while (line != null) //Loop terminates at end of file
    {
    number++;
    output.println (number + " " + line);
    }
    output.close (); // Close the output file
    } // main method
    } // NumberedPoem class

    When I open Poem2.dat all I get is a blank page.

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

    Default

    See this simple code segment.

    Java Code:
        public static void main(String[] args) throws IOException {
            PrintWriter output;
            output = new PrintWriter (new FileWriter ("C:/Poem2.txt"));
    
            String[] text = {"One", "Two", "Three", "Four", "Five", "Six", "Seven"};
    
            for(int index = 0; index < text.length; index++) {
                output.write(text[index]);
                output.write(System.getProperty("line.separator")); // New line 
            }
    
            output.close ();
        }

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

    Default

    Thanks, but where am I suppose to put this?

    Quote Originally Posted by Eranga View Post
    See this simple code segment.

    Java Code:
        public static void main(String[] args) throws IOException {
            PrintWriter output;
            output = new PrintWriter (new FileWriter ("C:/Poem2.txt"));
    
            String[] text = {"One", "Two", "Three", "Four", "Five", "Six", "Seven"};
    
            for(int index = 0; index < text.length; index++) {
                output.write(text[index]);
                output.write(System.getProperty("line.separator")); // New line 
            }
    
            output.close ();
        }

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

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

    Default

    How would I use this in my application? Could you integrate your code into mine?

    Quote Originally Posted by Eranga View Post
    This is main method. Put this content in another method and use it in your application.

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

    Default

    Still need help with my post/question above. If you could help that would be greatly appreciated. Thanks.

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

    Default

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

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

    Default It's must

    Seems you don't have more clear idea about Java basis. So the best thing you must do is get more familiar with those first. You can work on with the Suns' tutorial. While working on examples like this, try to work out on with that as well.

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

Page 1 of 2 12 LastLast

Posting Permissions

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