Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Loop Question  RSS feed

 
Ching Ming
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a block of Java code, containing a do/while loop, that will read in a person’s year of birth. The year of birth must be a value larger than 1900, but smaller than 2016, else the user will receive an error message. The loop must continue to prompt the user until a valid value is entered.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds doable. What have you tried so far?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

It is actually quite an easy task, and I suggest you create a separate method or methods to do that reading. Don't try to do it inside a method to do something different, otherwise you will write a method which does several things. General design principle: make each method do one thing.
 
Ching Ming
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I thought off.

 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the code work the way the instructions say? If not, what is your question? (I notice at least two typo errors regarding the value of the year being checked).

Also, unless this is to be part of a larger GUI program, you should consider using a Scanner instance instead of a JOptionPane to get the user's input.
Even better would to make a separate method to get the user's input, since that would allow you to change from GUI input to command-line input without modifying the code that checks for the correct input.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you post code, please UseCodeTags (that's a link).  It makes the code much easier to read.  I've edited your post and added them for you this time.

There are two numbers in your code that I don't understand: 1990 and 1190.  Are these typos?

Why is the method saveYears() static?  I suspect it's because you're calling it from main(), which isn't the best idea.  Usually, main() will launch the program and that's all.  See MainIsAPain (that's a link).
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ching Ming wrote:The year of birth must be a value larger than 1900

Ching Ming wrote:

What has happened?
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strip the method down to as little as you need right now, remove the try-catch and anything to do with GUI's; a Scanner object will suffice to read input.

As an added note, you don't need to use the double type to store the birth year. A year can be represented as an integral value.

L.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using option panes for keyboard input? That is very old‑fashioned coding practice. I would use this. If you search my posts, you may find an example with a loop similar to what you want.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Ching Ming
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This was the question.


Write a block of Java code, containing a do/while loop, that will read in a person’s year of birth. The year of birth must be a value larger than 1900, but smaller than 2016, else the user will receive an error message. The loop must continue to prompt the user until a valid value is entered.





Would that be a correct answer?
 
Paul Clapham
Sheriff
Posts: 22496
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

If you test it with various inputs, you'll see that it doesn't work correctly. Actually running code to see if it works right is far preferable to looking at code and trying to figure it out, so I strongly advise the the "actually running code" option.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already asked a question about such code. Don't start a new thread. I am going to try to merge the two threads.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!