On May 16, 4:17*am, Lew <no...@lewscanon.com> wrote:
> 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.
> beginner_in-> wrote:
> >> --
> >> Cheers,
> >> * * * * *Alan (San Jose, California, USA)
>
> 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;
> * *}
>
> > * * * * * * * *if(ch == 1)
> > * * * * * * * * * *{
>
> 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 )
> * *{
> * * *...
> * *}
>
> > * * * * * * * * * *td1.getVal(37.4);
>
> Magic number. *What's 37.4?
>
> > * * * * * * * * * * * * * res = td1.C2F();
>
> 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..
>
> <http://java.sun.com/docs/codeconv/index.html>
>
> > * * * * * * * * * * * * * *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'.
>
> > * * * * * * * * * * * * * *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.
>
> --
> Lew
Respected Sir, thanks for your suggestion to go through
http://sscce.org/
i have got the concept of sscce.
Sir, i want to tell you that i am accessing this Usenet group from
google group service... so i could not able to guess the out look of
my given code snippet at usenet group platform.
I would like to draw your attention the word [sic] that you mostly use
before each 'i' in quoted words. I still don't know the meaning of
[sic] as I m possibly not the countryman of your place, however, your
each useful advise I would like to follow to remove objections and
errors to learn the concepts effectively. This is the thing that
matters to me and all other things come on second priority so please
avoid those words which are of no business, here.
About the erroneous code, i have solved the problem using hasNextInt()
and nextInt().
I also want to know why you are not in favour to use Scanner#nextInt()
method.
-Niks