Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help writing star pattern to file

 
Rose Deanne
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instructions:
1. Program will ask the user for a number input (integer).
2. Program will then produce the stars ("*") as a square. However while it is printing a line of stars("*"), it will also write each set (line) of stars on a text file vice displaying on screen.
3. The program will then open the same text file for reading, read a line at a time and display on screen.
4. The program will keep reading lines until no more data (end of file).


This is what I've got so far. Keep getting "cannot find symbol variable outputFile" and "manystars is already defined in main(java.lang.String[])" errors. Help :/




My first post. Aww...
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 29, you declare/define "outfile" but then subsequently call it "outputfile". Choose a name and use it consistently.

Lines 9 and 51 both declare the local variable, String manystars. You only need (are allowed to) declare it once. Remove the "String " from line 51.
 
Rose Deanne
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:Line 29, you declare/define "outfile" but then subsequently call it "outputfile". Choose a name and use it consistently.

Lines 9 and 51 both declare the local variable, String manystars. You only need (are allowed to) declare it once. Remove the "String " from line 51.



I changed the "outfile" to match the rest, and they seem good to go now. Thanks.

But if I delete line 51 it tells me "variable manystars might not have been initialized" on line 36 and line 52. And if I delete line 9 instead it says "cannot find symbol variable manystars" on line 36.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You were not told to delete the line, but to get rid of the double declaration. Either:
  • Delete the word "String".
  • Or delete lines 9 and 36.
  • You have done nothing with manystars before line 36, so it won't have a value to display.

    You have some confusion. I don't think you will get
    ****
    ****
    ****
    ****
    anywhere.
    Go back to your for loop, and notice what it says here about {}. Add {} to your for loop, so you can see what is "inside" the loop and what "outside" it. Work out what that will print in your file.

    Now write down with pencil and paper (and eraser, preferably a large one :wink exactly how you are going to persuade the computer to write
    ****
    either to a file or on screen. You will probably know how to do that already. You will now have to convert that to this:
    ****
    ****
    ****
    ****
    Now you need to get that into a file, which I think you have got worked out already.

    Use spaces for indentation, not tabs, and definitely not a mixture. Find out about the %n tag and why it is better than \n. Or you could use println for each separate line in 15-17.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12123
    30
    Chrome Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    IMHO, you should never have gotten to this point. it would appear that you wrote close to 60 lines of code before you compiled. That is a mistake bigger than the Titanic.

    I never NEVER NEVER write more than 2-3 lines of code before I compile. I am CONSTANTLY recompiling and retesting what I wrote. Without exception, each time I start a new project, I write exactly this much before I compile the first time:

    Once that works, I'll start writing another 2-3 lines, then re-compile and test. I am always writing throw-away lines. I stick System.out.println statements in all the time, comment them out, delete them, put them back... I would estimate that for every 10 lines of code i write and keep, I write 20 lines that do nothing but test is, verify it works, show me execution paths, etc.

    If you follow this paradigm of coding, you'll find mistakes sooner, locate them faster, and have fewer.

    You may think it would take longer to write your code, but it ultimately ends up saving you 3-4 times the cost by making your debugging easier and faster.
     
    Rose Deanne
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fred Rosenberger:
    Yeah, wise words. That's something I'm gonna have to get used to.

    Campbell Ritchie:
    Well you were right about it not giving me a box, but I don't really know why. Earlier in the semester I made a program that just displayed the box on the screen, which worked just fine:



    So I just assumed that I could change "System.out.print("*");" to "outputFile.println("*");" and it would know to go into the text file instead.

    ...I was obviously wrong.



    Here's the most current version of my code so I can make sure I'm still on the same page:



    Thanks so much for all your help btw!
     
    Rose Deanne
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OH! I got it! Haha. Just had to get rid of the ln part of the "outputFile.println("*");"

    Success!
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12123
    30
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Congrats. That is a terrific feeling when you figure something out like that.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 48943
    60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    fred rosenberger wrote:Congrats. . . ..
    Agree. Well done.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic