Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how we can exit from program by hitting any key

Reply
Thread Tools

how we can exit from program by hitting any key

 
 
beginner_in->
Guest
Posts: n/a
 
      05-15-2010
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
 
Reply With Quote
 
 
 
 
Alan Malloy
Guest
Posts: n/a
 
      05-15-2010
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)
 
Reply With Quote
 
 
 
 
beginner_in->
Guest
Posts: n/a
 
      05-15-2010
On May 15, 12:51*pm, Alan Malloy <alan.NO.S...@malloys.org> 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-15-2010
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
 
Reply With Quote
 
beginner_in->
Guest
Posts: n/a
 
      05-16-2010
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

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-16-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: Any exit status without explicitely using return /exit Keith Thompson C Programming 10 03-03-2010 12:41 AM
Re: Any exit status without explicitely using return /exit jacob navia C Programming 3 02-24-2010 02:39 PM
How can I exit from "getline()" without pressing any key? caoliangbj@gmail.com C++ 3 06-08-2007 12:35 PM
Can I get the exit code "n" passed to sys.exit(n) ? Yujo Python 2 04-10-2007 08:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57