how we can exit from program by hitting any key

B

beginner_in->

hello there,

I want to exit from main class by pressing any key.... the code is in
this way:-


public class mainClass
{
public static void main(String[]ar)
{
//...some code here...

Scanner in = new Scanner(System.in);
int choice = in.nextInt(); //collecting user
input here
if(choice == 1)
{
td1.C2F();
}
else if (choice == 2)
{
td2.F2C();
}
else //if any key press program should be
terminate.
{

System.out.println("<- You select to exit -
//results in error java.util.InputMismatch exception
//due to Strongly Type characteristic
}
}//end of main
}//end of public class


so please, suggest me what should I do to exit from the class by
pressing any key. i m using jdk1.5.0_22

-Thanks
 
A

Alan Malloy

beginner_in- said:
hello there,

I want to exit from main class by pressing any key.... the code is in
this way:-


public class mainClass
{
public static void main(String[]ar)
{
//...some code here...

Scanner in = new Scanner(System.in);
int choice = in.nextInt(); //collecting user
input here
if(choice == 1)
{
td1.C2F();
}
else if (choice == 2)
{
td2.F2C();
}
else //if any key press program should be
terminate.
{

System.out.println("<- You select to exit -
//results in error java.util.InputMismatch exception
//due to Strongly Type characteristic
}
}//end of main
}//end of public class


so please, suggest me what should I do to exit from the class by
pressing any key. i m using jdk1.5.0_22

-Thanks

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.
 
B

beginner_in->

beginner_in- said:
hello there,
I want to exit from main class by pressing any key....  the code is in
this way:-
public class mainClass
  {
          public static void main(String[]ar)
            {
                       //...some code here...
Scanner in = new Scanner(System.in);
                    int choice = in.nextInt();   //collecting user
input here
              if(choice == 1)
                       {
                         td1.C2F();
                       }
                      else if (choice == 2)
                              {
                             td2.F2C();
                             }
                         else    //if any key press program should be
terminate.
                          {
System.out.println("<- You select to exit -
//results in error java.util.InputMismatch exception
//due to Strongly Type characteristic
                           }
     }//end of main
}//end of public class
so please,  suggest me what should I do to exit from the class by
pressing any key. i m using jdk1.5.0_22

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.

i have follow your suggestion ... but i wanna show you ..

int ch = Integer.parseInt(choice);
System.out.println("String has converted: " +ch);
if(ch == 1 || ch == 2)
{ //inner if block
if(ch == 1)
{
td1.getVal(37.4);
res = td1.C2F();
System.out.println("Resulted Temperature: "+res);
} // start of else-if block
else if(ch == 2)
{
td1.getVal(104.2);
res = td1.F2C();
System.out.println("Resulted Temperature: "+res);
}

}//end of outer if and starting of else
else
{

System.out.println("<- You select to termination of program ->");
System.exit(0);
}

is that what you envisage about....

now i 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.

Thanks!
-Niks
 
L

Lew

Alan said:
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;
}
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.

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.
 
B

beginner_in->

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;
   }
               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..

                           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.

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
 
L

Lew

beginner_in- said:
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.

Spaces are spaces. Google Groups has no relevance; you know how many spaces
you're typing.
I would like to draw your attention the word [sic] that you mostly use

"sic" means that the person quoting the passage (me) has not changed the
quoted content, so therefore the error in spelling is in the original, not a
transcription error. The word "I", first person singular pronoun, in English
is always capitalized.
before [actually, after] 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

The meaning is available from any standard dictionary.
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.

This is a free discussion forum. A professinal ought to be able to get the
case right of fundamental words like "I" or "Java". This helps one get used
to the case-sensitive nature of Java. No one faults a person for not having
English as a first language, but names and such are not dependent on the
language one learned first, and spelling "I" correctly is such a simple matter
that there's really no excuse for getting it wrong.
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.

Alan explained that in his post, as you may recall.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top