• 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

How to write a suitably loop?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Please, help me. A can't understand

I need to make a loop.
If the input can not be interpreted as an int value, then ask to enter the value again.
And ask to enter a value until the input can be interpreted as an int value.

Full terms and conditions:
Ask enter two values ​​.
Check that the input can be interpreted as an int value. If not , ask to enter again.
Calculate the amount.

This my code:
 
Bartender
Posts: 3323
86
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you find yourself writing the same basic chunk of code again, think I need to putting that code in a method which you can then repeatedly call whenever you need it.

In this case putting the code to get the next input into a method will also make it easier for you to work out how to repeatedly execute the code until you get a valid input.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember you want to repeat the input while you do not have a next int.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Alena,

First of all, in order to make a loop, you need to use a loop statement and condition in which circumstances loop iterates.
Have a look at the mainly used loop statements and their descriptions:

for - Count controlled loop statement (when you know exact number of iterations can happen). Repeating some kind of steps, until condition is met.

while - Used for iteration based on a condition. In different words, iterating through a loop body statements and executing them, until something happens (meets your specified condition).

do while - Very similar to while loop, but, the body of the do while statement is executed at least once. (For example you print message and waiting user to respond. If response doesn't meet your condition, the same message is being printed again).

Read extra articles about these loops if never used it before, and then write over here, what you think, which loop would be most suitable in your case.

Good luck.
 
Alena Net
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Whenever you find yourself writing the same basic chunk of code again, think I need to putting that code in a method which you can then repeatedly call whenever you need it.

In this case putting the code to get the next input into a method will also make it easier for you to work out how to repeatedly execute the code until you get a valid input.



Thank you so much!
It's so obvious and simple, but I completely forgot about it. Thanks!

I still can not do it.
Please can you show the direction ?
This is what I have now, and it works quite wrong.



And I need to remember the values ​​and summarize them at the end. Now I'm stuck.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You appear simply to have repeated your earlier code. You need a loop, so try a loop.

Hint: search my posts for similar posts about Scanners.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alena Net wrote:I still can not do it.
Please can you show the direction ?
This is what I have now, and it works quite wrong.


Ok, you need to take one step at a time. Don't try and solve the whole problem in one go, take one part and write the code for taht and test it works before going onto the next part. In this case getting a valid input is the first stage so get that working before trying to get two values and do the calculation.
You have written a method getNumber that asks for a number and then calls another method to get the number. If an invalid value is entered it then calls getNumber to repeat the process but getNumber() doesn't return any value. Two issues:
1. You are using recursion to solve a problem that could be more easily done with a do-while loop. Also recursion is difficult to understand, especially for beginners. I suggest you merge these two methods and add a do-while loop to repeat the code while the input value is not valid.
2. getNumber doesn't return the input value. HINT: any method starting with 'get' should always return a value.
 
Alena Net
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't find how to get input again from do-while. I can't find, how go back and ask get input again.
I read much about this, but i didn't see the way.
So i use if-else. I think it's works.
But now i have a new problem.
When i give the wrong input, the right after that don't save in variable.
I guess the rizen is "return 0" in else block. But if i put right input after rong, than it should not go in else block.

Please, give me new hint.
My code:

P.s. Tony , you're really great explains.
Thank you so much .
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't use a do loop myself. I gave you a hint on Saturday where you can find a much simpler technique than what you wrote.

I would use a loop, however, not the recursive technique you showed.
 
Ranch Hand
Posts: 349
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alena Net wrote:

My code:

P.s. Tony , you're really great explains.
Thank you so much .



Hi Alena,
I'd like to do a little change on your code :




The reason is :
If I input other than integer , the execution will go to the else part and here is the trick .The recursive call has been made .Get the data from the call and return that vale .I guess you understand .

Thanks ,
Satya



 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Majumder wrote: . . .
Hi Alena,
I'd like to do a little change on your code :
. . .

That only improves it slightly. There is a far better and simpler way to ensure one reads an int with a Scanner.
 
Alena Net
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Majumder wrote:
The reason is :
If I input other than integer , the execution will go to the else part and here is the trick .The recursive call has been made .Get the data from the call and return that vale .I guess you understand .



Thank you, Satya!
It explains everything.

Campbell Ritchie, i agree. I find that topic, and find ready solution. This working code:

Now it looks simple. Thank you!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is Rob Spoor you should thank because he taught me that sort of code
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic