how we can exit from program by hitting any key

Discussion in 'Java' started by beginner_in->, May 15, 2010.

  1. 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
     
    beginner_in->, May 15, 2010
    #1
    1. Advertising

  2. beginner_in->

    Alan Malloy Guest

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

    --
    Cheers,
    Alan (San Jose, California, USA)
     
    Alan Malloy, May 15, 2010
    #2
    1. Advertising

  3. On May 15, 12:51 pm, Alan Malloy <> wrote:
    > beginner_in-> wrote:
    > > 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.
    >
    > --
    > Cheers,
    >         Alan (San Jose, California, USA)


    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
     
    beginner_in->, May 15, 2010
    #3
  4. beginner_in->

    Lew Guest

    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
     
    Lew, May 16, 2010
    #4
  5. On May 16, 4:17 am, Lew <> 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
     
    beginner_in->, May 16, 2010
    #5
  6. beginner_in->

    Lew Guest

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

    --
    Lew
     
    Lew, May 16, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy G

    Hitting the enter key

    Andy G, Dec 27, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,561
    Andy G
    Dec 28, 2004
  2. =?Utf-8?B?UGF1bA==?=
    Replies:
    1
    Views:
    356
    =?Utf-8?B?UGF1bA==?=
    Aug 3, 2005
  3. Replies:
    3
    Views:
    360
    Maulik
    Jun 8, 2007
  4. jacob navia
    Replies:
    3
    Views:
    555
    Nick Keighley
    Feb 24, 2010
  5. Keith Thompson
    Replies:
    10
    Views:
    695
    Tim Rentsch
    Mar 3, 2010
Loading...

Share This Page