Forums Register Login

Do While Loop and If Statements

+Pie Number of slices to send: Send
HI, Fellas

I am practicing the Do while loop and I'm having some difficulties in adding some feature like if I type character on the number field it will give me an error message that says that I should type a number instead of Strings, and same way in the Character field.

Thank you in advance,


+Pie Number of slices to send: Send
So what does the code do now, exactly, and at which point does it start to deviate from what it should actually do?
+Pie Number of slices to send: Send
So Once you run that code it will ask your name and your age. and once you type your name it will increment it into 5. Below is the Output of the above code.

Hello. What Is Your Name?
John
How Old Are You?
25
Hi John How Are You?
Did You Know That In Five Years You will Be 30 Years Old.
And Five Years Ago You Are 25 Years Old.

So What I want is if I type number on the Character field when it says "How Are you" a message error will display like "Please Enter Character" or if I press enter without typing any character it will also display some message says that "Please type a character" and it will switch back to the question "What is Your name" and this should be the same on the age field. I've done some coding but I'm still having some problems it doesn't give me what I expect. Please help me please
+Pie Number of slices to send: Send
 

Haider Mezta wrote:So Once you run that code it will ask your name and your age. and once you type your name it will increment it into 5. Below is the Output of the above code.

Hello. What Is Your Name?
John
How Old Are You?
25
Hi John How Are You?
Did You Know That In Five Years You will Be 30 Years Old.
And Five Years Ago You Are 25 Years Old.

So What I want is if I type number on the Character field when it says "How Are you" a message error will display like "Please Enter Character" or if I press enter without typing any character it will also display some message says that "Please type a character" and it will switch back to the question "What is Your name" and this should be the same on the age field. I've done some coding but I'm still having some problems it doesn't give me what I expect. Please help me please



I can provide you some points to move ahead in making some progress, may not be the best solution at this time.

1. You need to check on the input provided by the user for any digits. You can use regular expression here but as you are just starting ( I think so) you can check for individual digits from 0-9. You can also create array of integers 0-9 and check input string for presence of any element in array. String class provides contains() method to check any substring/char in given String.
2. If you see any digit you can call the main method again, that will start asking questions from user again ( starting from the name).

I think if you work with above points you should be able to have working code that can be modified further to have better structure.
Bring out your dead! Or a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 415 times.
Similar Threads
Wait for user input
Problem with Scanner input
Confused by hasNext()
Looping problem with ints
Programming formulas
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:56:59.