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

Do While Loop and If Statements  RSS feed

 
Haider Mezta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,


 
Tim Moores
Saloon Keeper
Posts: 3885
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what does the code do now, exactly, and at which point does it start to deviate from what it should actually do?
 
Haider Mezta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!