The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: How to limit users to only enter integer into a String variable.

You can't control what a user enters, but you can make the program not accept what the user enters.
To do that use a loop that asks the user for input, tests what is input and exit the loop if the input is acceptable. If the input is not acceptable, the loop goes back and asks the user for input again.

One way to test the user's input to be a number is to use the Integer class's parse method inside of a try catch block.

Re: How to limit users to only enter integer into a String variable.

you need to get the users input before you start the validation so you have something to check it against

you are using a do while loop wrong

do { <--start loop
stuff you want to do at least one time <-- ask for input and receive the input
}while(test here); <---test if the input is good and if its not go back to do and start all over

really you could just use a while loop after you get the users input and if the while loop tests incorrect to ask for input again..then if the users input is correct it wouldn't even execute the body of the while loop