aspose file tools*
The moose likes Beginning Java and the fly likes Need Help Creating A Loop! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help Creating A Loop!" Watch "Need Help Creating A Loop!" New topic
Author

Need Help Creating A Loop!

christian loran
Greenhorn

Joined: May 12, 2011
Posts: 7
my program compiles with no errors though I need help actually getting the program to loop. I want it to loop if yes is entered , and stop if no is entered. How would I go about doing this? can someone please provide the proper code needed? I am very new to Java and I am trying my best to learn. this program is a project from a book I am reading.

here is my code:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11326
    
  16

There are three kinds of loop:

1) a for-loop. This is most commonly used when you know exactly how many times you will need to loop

2) a while-loop. this is used when you may need to loop, or you may not. It is possible the body will NEVER be executed.

3) a do-while loop. This loop body will ALWAYS execute at least once.

There are different philosophies on whether the do-while should ever be used, but it sounds like that would meed your needs.

Basically, you need to surround all the code you want to run over and over again in this:



it looks to me like you'd want to put everything from line 33 to 69 inside the body. Then, you need to establish your condition for terminating the loop. You probably want to test to see if some input is equal to 'yes' or 'no', depending on how you word the question.

Why don't you give it a try, and show us what you come up with?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
christian loran
Greenhorn

Joined: May 12, 2011
Posts: 7
This is my valient effort at trying to get it to actually work, though it was a failure unfortunately. Here is my code



and this was the error message!

C:\Users\christian\Desktop\document2.java:35: yes is already defined in main(java.lang.String[])
int yes;
^
1 error

Tool completed with exit code 1
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

christian loran wrote:...this was the error message!

C:\Users\christian\Desktop\document2.java:35: yes is already defined in main(java.lang.String[])
int yes;
^
...

This is telling you that the variable "yes" you tried to define on line 35 has already been defined within your main method. Indeed, you have a boolean "yes" defined on line 27 and an int "yes" defined on line 35. You can't declare 2 variables by the same name (in the same scope).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11326
    
  16

That should be easy for you to fix...don't define it twice. You have a boolean variable 'yes' on line 27, and an int yes variable defined on 35. You can't have two variables with the same name (well, technically you can, but for now let's pretend we can't).

although, I have to say that 'yes' and 'no' are not very good names for variables.
christian loran
Greenhorn

Joined: May 12, 2011
Posts: 7
thanks for the quick response, well now I am at a point were I do not know what to do... which variable yes should I remove? the one from the boolean or int?

here is the code:


compile message:
C:\Users\christian\Desktop\document2.java:35: yes is already defined in main(java.lang.String[])
int yes;
^
1 error

Tool completed with exit code 1
christian loran
Greenhorn

Joined: May 12, 2011
Posts: 7
if yes and no are bad names for the variables, what names should I choose? Thank you!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11326
    
  16

christian loran wrote:if yes and no are bad names for the variables, what names should I choose? Thank you!

well...what do they represent? Generally, you name a variable so that when you come across it in your code, you'd know what it was. so "loopAgain" might be a good name for the variable that holds whether you want to loop through your code another time.

I don't see where you actually ever USE them, so why not just get rid of them?
christian loran
Greenhorn

Joined: May 12, 2011
Posts: 7
here are the new changes I made, still no luck. what changes should I make so that the program will run correctly. I want the user to be able to type yes or no to tell the program to loop or end. So should I add a value to yes and no? or just go with what you said before, int loopagain.



this was the compile message:

C:\Users\christian\Desktop\document2.java:74: variable no might not have been initialized
} while (no);
^
1 error

Tool completed with exit code 1
christian loran
Greenhorn

Joined: May 12, 2011
Posts: 7
If anyone can modify my code to make it properly loop that would be a huge help! Its hard trying to learn from a book, and I dont have an example of a working loop for my situation. feel free to add in any code to make it work, and the names of values do not matter, as long as it loops when the user types yes and ends when they type no. I realise the program is fairly simple though I am completely new to java and I am used to learning visusally and having a professor that includes samples. Thank you so much for all the help!

here is the code!



here is the compile message

C:\Users\christian\Desktop\document2.java:74: variable no might not have been initialized
} while (no);
^
1 error

Tool completed with exit code 1
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11326
    
  16

christian loran wrote:If anyone can modify my code to make it properly loop that would be a huge help!

We don't do that here. This is your homework, so you need to do the work. If someone where to post the solution here, I would delete it.

You know how to get input from a user - you've done that. You need to do it again. On line 70, you ASK the user if they want to go again, but you haven't gotten an answer from them.

How did you get the input from the user for the length? You have to get their answer and store it somewhere. Once you have it, then you need to see if what they gave you requires you to loop again.

As to your current error, the compiler tells you exactly what the problem is. You have a (still poorly named) variable called "no" that has never been initialized. Local variable - variables declared inside a method - MUST be given a default, initial value. Your "no" variable is a boolean, so you need to set it initially to either 'true' or 'false' when you declare it back on line 27.
christian loran
Greenhorn

Joined: May 12, 2011
Posts: 7
I completely understand, I just made some more changes and got it to work, though I am having problems with the loop, how should I go about getting the users answers yes and no to have effects on weather the program loops?
Mykhailo Kozik
Greenhorn

Joined: May 12, 2011
Posts: 16

I recommend you to read basics, it will be really helpful.

So, you can try this pattern for interactive loop:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help Creating A Loop!