Okay so basically my problem is that I have to prompt the user for a set of floating point values once . If they do not enter the right data type then I have to prompt them again. If they dont enter the right type the second time I quit reading input and use exception handling to detect improper input. If they do enter the right data on either of the two chances i have to find and print the sum. So again, If it is the second prompt/input i use exception handling to detect improper inputs. So my issue is being able to prompt the user once without exceptions. I understand how to use exceptions and print the sum if they were only prompted once. At this point my code is all over the place and posting it would be useless. So any feedback as to the right methods or loop/ decision combinatioin would be appreciated. Let me also say that yes this is for school. I am in my second programming class but this is not a graded assignment it is just a problem that I decided to attempt and has stumped me and my CS tutor. So thanks again if you guys can just point me in the right direction.

Recommended Answers

All 2 Replies

Your requirements are not entirely clear, but it sounds like you want to create a method like

private Set<Float> promptForFloats() throws NumberFormatException

And then you can simply do something like this:

Set<Float> result;
try {
    result = promptForFloats();
} catch(NumberFormatException e) {
    result = promptForFloats();

Thanks bguild. I thought I had a decent idea of what exceptions were and how to use them.Although I didnt even think to use the Throws clause- which was the one part that I kind of understood what it did but not when or why you would use it. I'm going to test your method or some form of it and If it works great if not I will still have learned somehing. So thanks again. Also since I was unclear here is the exact wording of he problem:

Write a program that asks the user to input a set of floating-point values. When the
user enters a value that is not a number, give the user a second chance to enter the
value. After two chances, quit reading input. Add all correctly specified values and
print the sum when the user is done entering data. Use exception handling to detect
improper inputs.

Horstmann, Cay S. "Java For Everyone:"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.