Alan Malloy wrote:
The scanner is complaining that if you type, say, "J", it can't turn
that into an integer: true enough. The simplest way to solve this is
read the user's input as a string, and check to see whether it is a
number. If so, you can convert it to a number with Java's built-in
methods and then process it; if not, you can exit the program.
Please don't quote sigs.
i [sic] have follow your suggestion ... but i [sic] wanna show you ..
What happened to the part of the code where you invoke the Scanner method? We
can't tell if you took Alan's advice not to use 'Scanner#nextInt()'.
You should provide a complete example as explained at
<
http://sscce.org/>
because these code fragments don't tell enough of the story for us to help you.
Now for some comments not related directly to your question. Disregard them
if you will, for now, but in the long run they'll help your coding.
int ch = Integer.parseInt(choice);
System.out.println("String has converted: " +ch);
if(ch == 1 || ch == 2)
{ //inner if block
You want to lighten up on that indentation a little, there, sport?
Four spaces is about the maximum per indent level in a Usenet post if you wish
to keep it readable, and thus keep those interested who might help you.
Your code would be simpler and more maintainable if you used a 'switch'
instead of these complicated 'if' blocks:
switch ( ch )
{
case 1:
whateverOne();
break;
case 2:
whateverTwo();
break;
}
There are two popular conventions for brace placement in Java. The official
one places the opening brace at the end of the conditional introducing a
block, e.g,
if ( ch == 1 ) {
The more readable one puts the opening brace at the same level as the
introductory conditional.
if ( ch == 1 )
{
Both put the closing brace at the same level as the conditional.
if ( ch == 1 ) {
...
}
or
if ( ch == 1 )
{
...
}
Magic number. What's 37.4?
The Java coding conventions call for method names (that /is/ a method call,
right?) and non-constant variable names to begin with a lower-case letter..
System.out.println("Resulted Temperature: "+res);
} // start of else-if block
else if(ch == 2)
{
td1.getVal(104.2);
Magic number. What's 104.2?
res = td1.F2C();
System.out.println("Resulted Temperature: "+res);
}
}//end of outer if and starting of else
Comments are supposed to add to understanding of the code. A closing brace on
an 'if' followed by an 'else' already informs the maintainer that you are at
the end of an 'if' and starting an 'else'.
System.out.println("<- You select to termination of program ->");
System.exit(0);
}
is that what you envisage about....
now i [sic] m not able to exit from any key input but on pressing numeric
key. Sir Should I again convert this into String to exit from the
menu.
Back to your main question:
Provide a short, self-contained compilable example (SSCCE) as instructed at
<
http://sscce.org/>
and we will find the part of the code that you have not shown here that is the
source of your trouble - or perhaps you'll find it for yourself as you
construct the example.
I suspect that you are still using the 'Scanner#nextInt()' method and that is
the problem, but I don't know.