Results 1 to 15 of 15

Thread: Output

  1. #1
    Twiggy is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Question Output

    So I'm quite new to Java, and am taking a course in computer programming now.

    One of our assignments has me kind of stumped, and I'm wondering if someone could lead me in the right direction of how to go about programming this.

    The assignment is:

    Create a new Java program and class titled, PoemData, that will allow the user to enter lines to his/her favourite poem (must be greater than four lines) and store the poem to a data file named Poem.dat. Allow the user to stop the data entry when they are finished their poem. Be sure to test your solution and view the associated data file for correctness.

    Any help would be greatly appreciated.

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

    Default

    Hello and welcome to the forum. You know of course that all of the folks here are volunteers who enjoy helping others learn Java, and we'll gladly help you with your code or with specific questions on how you can create a certain program. On the other hand, what this forum isn't is a code-producing mill that gives out code on demand.

    So on that note, let's see a decent try by you towards a solution first. After you've done this and posted it with your specific questions, I'm sure that several of us will be be glad to help you along.

    Best of luck!

  3. #3
    Manofest's Avatar
    Manofest is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    Just having a quick look around for usefull stuff this link deffinatly is

    w ww.roseindia.net/java/beginners/java-write-to-file.shtml
    if you need to do a gui to go with it there is plenty of stuff around.

  4. #4
    Twiggy is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    In response to fubarable:

    I am not asking for someone to code out a program for me or anything. I'm asking if someone could give me a suggestion on how I should actually go about making the program. I could see how I be misleading in my way of asking for help though.


    I've been doing things like trying to store lines in an array, and asking how many lines the poem is and then, letting them put in lines that many times and then it stops. But I don't know how I should do this. I tried doing it so that if the line "Done" is inputted, then it'll stop writing to the outputted file, but I couldn't figure out how to do that properly.

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

    Default

    Myself, I find it very hard to know what you know or don't know without seeing a posted attempt by you first. I stand by my first recommendations.

    Again, best of luck.

  6. #6
    Twiggy is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Okay, well here is my method which is closest to working.
    It only seems to output as many WORDS as I want lines instead though.




    Java Code:
    import java.awt.*;
    
    import java.io.*;
    import hsa.Console;
    
    public class poemData
    {
        static Console c;
    
    
        public static void main (String[] args) throws IOException
        {
            c = new Console ();
            
            String line[];
            int numberLines;
            int number = 0;
    
            
            PrintWriter output;
            
            output = new PrintWriter (new FileWriter ("Poem"));
            
            
            c.println ("How many lines is your poem?");
            numberLines = c.readInt ();
    
            
            line = new String [numberLines];
            
            
            while (number < (numberLines))
            {
            
                line [number] = c.readString ();
                
                output.println (line [number]);
                
                number++;
            }
           
            output.close ();
    
        }
    }

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

    Default

    It only seems to output as many WORDS as I want lines instead though.
    Could you please describe this a little better for me?

    Also, most of us will likely have to punt on the Console class as it is not part of standard Java. Hopefully it's not giving you any errors.

    Edit: I've run your code (all except the Console object which I couldn't use -- I substituted a Scanner object instead), and it seemed to work fine. So if your use of Console is OK, I think that your program should work. Again, if it's not, what specific problems are you having. The more details you give, the better we'll be able to help.

    Good luck.
    Last edited by Fubarable; 12-30-2008 at 02:35 AM.

  8. #8
    Twiggy is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Well, my problem is that...

    If I say my poem is going to be 3 lines long, I can put in

    word
    word
    word

    and that works. However if I still want it 3 lines long, and I put in:

    word word
    word

    or

    word word word

    It stops.




    (Sorry I'm not very good at describing my problem:o)

  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

    Quote Originally Posted by Twiggy View Post
    Okay, well here is my method which is closest to working.
    It only seems to output as many WORDS as I want lines instead though.

    First of all break down your code into simple steps. Then start to work on then. Looking at your code we cannot think what you have done there. You said that it's closest to working. But I cannot is such thing there. Where you collect user inputs, that should be the first task you have to do I guess. Is that Console class doing that?

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

    Default

    Does your Console class have a readLine() method?

  11. #11
    Twiggy is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    I really hope I have explained myself well enough in this.


    Java Code:
    import java.awt.*;
    
    [B]import[/B] java.io.*;
    [B]import[/B] [COLOR="RoyalBlue"]hsa.Console[/COLOR];
    
    [B]public class [/B][COLOR="RoyalBlue"][COLOR="RoyalBlue"]poemData[/COLOR][/COLOR]
    {
       [B] static [/B][COLOR="RoyalBlue"]Console c;[/COLOR]
    
    
        [B]public static void[/B] [COLOR="RoyalBlue"]main[/COLOR] (String[] [COLOR="RoyalBlue"]args[/COLOR]) throws IOException
        {
            [COLOR="RoyalBlue"]c[/COLOR] = [B]new[/B] [COLOR="RoyalBlue"]Console[/COLOR] ();
            
          [COLOR="YellowGreen"]  //declaring variables here.[/COLOR]
            String [COLOR="RoyalBlue"]line[/COLOR][];
            [B]int [/B][COLOR="RoyalBlue"]numberLines[/COLOR];
            [B]int [/B][COLOR="RoyalBlue"]number[/COLOR] = 0;
    
           [COLOR="YellowGreen"] //making output an object of PrintWriter class[/COLOR]
            PrintWriter [COLOR="RoyalBlue"]output[/COLOR];
            
           [COLOR="YellowGreen"] //creating a stream to the outputted file[/COLOR]
            [COLOR="RoyalBlue"]output[/COLOR] =[B] new [/B]PrintWriter ([B]new[/B] FileWriter ([COLOR="DarkRed"]"Poem"[/COLOR]));
            
            [COLOR="YellowGreen"]//ask user how many lines they'll input[/COLOR]
            [COLOR="RoyalBlue"]c.println[/COLOR] ([COLOR="DarkRed"]"How many lines is your poem?"[/COLOR]);
            [COLOR="YellowGreen"]//Here, the integer numberLines is assigned a number, which is how many lines should be allowed to be inputted and then outputted.[/COLOR]
            [COLOR="RoyalBlue"]numberLines[/COLOR] = [COLOR="RoyalBlue"]c.readInt[/COLOR] ();
    
           [COLOR="YellowGreen"] //make the array 'line' have as many values as the user inputs for the number of lines[/COLOR]
            [COLOR="RoyalBlue"]line[/COLOR] = [B]new [/B]String [[COLOR="RoyalBlue"]numberLines[/COLOR]];
            
           [COLOR="YellowGreen"] //while 'number' is less than the number that should be inputted, do the following. (Number starts at zero, because the first value in the array would be zero.[/COLOR]
            [B]while[/B] ([COLOR="RoyalBlue"]number[/COLOR] < ([COLOR="RoyalBlue"]numberLines[/COLOR]))
            {
               [COLOR="YellowGreen"]//the value of 'number' for the array will be inputted here. First it is zero. So line[0] = whatever they type in. This should be a line of text in the poem.[/COLOR]
                [COLOR="RoyalBlue"]line[/COLOR] [[COLOR="RoyalBlue"]number[/COLOR]] = [COLOR="RoyalBlue"]c.readString[/COLOR] ();
    
                [COLOR="YellowGreen"]//it should then output that line to a file[/COLOR]
                [COLOR="RoyalBlue"]output.println[/COLOR] ([COLOR="RoyalBlue"]line[/COLOR] [[COLOR="RoyalBlue"]number[/COLOR]]);
                
                [COLOR="YellowGreen"]//After doing that, the number goes up one, to move on to the next line, or the next value in the array 'line'[/COLOR]
                [COLOR="RoyalBlue"]number++[/COLOR];
            }
           
           [COLOR="YellowGreen"] //closes the output path[/COLOR]
            [COLOR="RoyalBlue"]output.close[/COLOR] ();
    
        }
    }

  12. #12
    Twiggy is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Fubarable: Thank you very much!

    readLine(); did it. We hadn't been told about that yet so I didn't know it existed. It now works :) All that messing around for such a simple solution.

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

    Default

    I understand your problem and your code. The problem (as I see it) is with this line:
    Java Code:
    line [number] = c.readString();
    It seems that readString() is reading in only tokens separated by white-space and not reading a line. Since your hsa.Console class is non-standard, most of us will not be able to help you with it, but again, I do wonder if there is a c.readLine() method that you could use here instead of c.readString(), since what you want to do is read the whole line in, not just a token. Me comprende?

    edit: I'm a bit slow on the typing. ignore this post!
    congrats on getting a solution!

  14. #14
    Lite-Creator is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default


    Hi im Newbie in this forum...
    Can u make a code for GUI using array?

  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

    Default

    Quote Originally Posted by Lite-Creator View Post
    Hi im Newbie in this forum...
    Can u make a code for GUI using array?
    Welcome to our community!

    This is a forum lol, not a code bank. You must take care of few things here.

    1. No one here ready to write codes for you. So you must put some effort first.
    2. You must explain your question more clearly.
    3. Please don't post your questions on others thread, if your question not related to it.
    4. Please read our FAQ page before posting again


    Good luck!

Similar Threads

  1. different output
    By pro85 in forum Java Applets
    Replies: 6
    Last Post: 08-05-2008, 01:42 PM
  2. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 10:14 PM
  3. Why the output is always zero
    By mehrotra.chitij in forum New To Java
    Replies: 12
    Last Post: 04-25-2008, 04:05 AM
  4. output
    By Camden in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 10:34 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
  •