This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How do you print a character a random number of times? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do you print a character a random number of times?" Watch "How do you print a character a random number of times?" New topic
Author

How do you print a character a random number of times?

Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
Hi, I'm trying to write a method that will print a string of a random number of X's (from 5 to 20) on a line, then another number of X's on the next line, etc. until the random number = 16.
I have to use while loops and probably fencepost algorithms. I'm confused with how to print the number of x's the random number of times over and over.
Right now, I'm pretty sure my for loop is wrong or unnecessary.
Any help is greatly appreciated, thanks!

So far, this is my code:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
And what happens when you run that code? I can only see one error.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
Make that two errors.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
three. I ought to learn to read.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
Four. Most of them standard beginners’ errors. Look at the end of the line beginning for
Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
Well, it's only printing one line of x's. I need it to continue to print lines of random numbers of x's until the random number is 16.
I'm thinking I went wrong with the for loop?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
Earlier, I wrote:. . . Look at the end of the line beginning for
If I drop any heavier hints, they will go through the floor.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Ashley Kin wrote:Well, it's only printing one line of x's.
I'm thinking I went wrong with the for loop?

Yes, there's a small problem with big consequences at the end of line 7.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
Have you really not read that line, AK? Look very carefully. You have even had hints which part of the line to look at first.

By the way: Insert this line before the line saying print("x")If you are lucky, that will bring the other three errors to your notice. If you are unlucky, only two of them. One of those three errors is related to an error I pointed out yesterday in a different thread of yours about a different program.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
No, no, no, no, no. You, Harsha Smith, are going to deprive AK of any chance of learning if you simply post code like that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you print a character a random number of times?
 
Similar Threads
Random Number Chooser programming exercise
Array question
Multi Dimensional Arrays and nextBoolean
Array problem
Duplicate problem