Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finite Loop with an Array

 
Tiffany Carra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking to repeat same word no more than 100 times. The word is gained from the command line. I understand that because it is a finite loop that the best type of loop is for(). This is where I get stuck, because all examples point to something with numbers.

I can get it to work once or infinite times, but for some reason I am getting lost trying to get it stop after 100 times.

Here what I have right now. I know it is completely wrong as it will not compile.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear to me what you are actually trying to do: repeat a word from the command line the number of times specified on the command line, but not more than 100 times?
 
Tiffany Carra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:It's not clear to me what you are actually trying to do: repeat a word from the command line the number of times specified on the command line, but not more than 100 times?


I am trying to take a word gained from the command line as such:

java Hundred Test

And repeat it 100 times, no more no less.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. If you want to take the word you passed to the command line, you have to store it in a String, not in an integer
2. To loop 100 times, we usually use loop from 0 to 100(excluded)
3. What you want to print is the word, not the loop index

Can you figure out how to correct each of these points ?
 
Tiffany Carra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:1. If you want to take the word you passed to the command line, you have to store it in a String, not in an integer
2. To loop 100 times, we usually use loop from 0 to 100(excluded)
3. What you want to print is the word, not the loop index

Can you figure out how to correct each of these points ?


Out of the three things you told me the one I could understand the first one. The other two not following you. Super Noob here.




My basic understanding is that:
Line 1: public class Hundred gives the class a name

Line 3 is the public static method "main". main has the "String[]args" which is an array of strings named "args"

Line 5 is the loop, "String i" is setting the variable i to args, then this is where I am I know I am botching it. Because I can have a variable all I want, I can say that i is less than 100 all I want, and I can say to increment i all I want, but the fact of the matter is that since i is not a string and not integer it does not work.

Line 7 is just the basic print function saying it will print whatever variable i is.
 
Tiffany Carra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also understand that if I just do this



That the first word typed after Java Hundred will be repeated in an infinite loop.

So I can get it to work, but getting it to stop after 100 is my down fall.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try searching the web for "java for loop"; there are a lot of examples.

What book are you using to learn?
 
Tiffany Carra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried doing that and they all include integers, since that is what I am having issues with that part they don't help me.

Just Java 2.0 the three examples it provides are all for integers as well.

At this point I could increment numbers in my sleep, but this isn't a number.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is repeating something exactly 100 times not an integer operation?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure you're thinking about this correctly.

You have a word (from the command line).
You want to print it.
You want to print it 100 times.
The 100 times thing, on a logical level, has absolutely nothing to do with *what* you're doing (the stuff inside the for loop), only how many times you're doing it.
 
Tiffany Carra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that helps like none.

The most common example of this that I find on the net is



According to what Christophe Verré said yesterday I have to store it in a string not an integer. If that is the case, AND what you are saying is correct then I am not sure I understand how to combine the two.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure the tone of your responses is inline with the amount of effort going in to helping you without simply giving you the answer, which isn't something we do here. If what I said doesn't help you, find a better way to indicate it.

You're not trying to print the count; you're trying to print the word from the command line. You're trying to use the count to control how many times the word from the command line is printed. You misunderstood what Christophe said: he said you need to store the word from the command line in a String, not an integer.

The integer is used to control the number of times the loop executes: it has nothing to do with what's inside the loop (in this particular case). The only thing you'll do inside the loop is print the word from the command line.

You know how to increment integers in a for loop; you said you did, and your code fragment does just that. Now what goes inside the loop body? Printing the word from the command line.
 
Tiffany Carra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't trying to get you to spoon feed me the answer. I was just trying to indicate my confusion. Sorry if my response misled you into thinking that.


I think the last response did it.
 
Tiffany Carra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks by the way.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We all understand your frustration--we've all been there, believe me, on one level or another.

For the sake of completeness, and for those that follow, it'd be cool if you posted the code you ended up with :)
 
Tiffany Carra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that is cool. I just wasn't sure if that was kosher.

Here is the code that I got

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Unless you don't drink alcoholic beverages, in which case, replace with frothy drink of your choice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic