• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help Creating A Loop!

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
christian loran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if yes and no are bad names for the variables, what names should I choose? Thank you!
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 16
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend you to read basics, it will be really helpful.

So, you can try this pattern for interactive loop:
reply
    Bookmark Topic Watch Topic
  • New Topic