views:

174

answers:

2

I'm writing a program in which I want to terminate program by pressing any key(whether character or numbers), so I did a conversion from string to int using Integer.parseInt(variable) method and compare choices if it is not desired choice it should terminate the program but it show an error Exception in thread "main" java.lang.NumberFormatException: for input string: "d".

program code is as follows:-

public class mainClass {
    public static void main( String[] ar ) {
        double res = 0;
        Scanner in = new Scanner( System.in );
        Tdata td1 = new Tdata(); // another class object

        System.out.println( "****Temperature Conversion****" );
        System.out.println( "------------------------------" );
        System.out.println( "Press 1-> C2F" );
        System.out.println( "Press 2-> F2C" );
        System.out.println( "<- Press aNY kEY TO Exit ->" );

        String choice = in.nextLine();

        // =======================================================
        int ch = Integer.parseInt( choice );
        System.out.println( "String has converted: " + ch ); 
        // verifying if converted into int
        if( ch == 1 || ch == 2 ) {
            if( ch == 1 ) {
                td1.getVal( 37.4 );
                res = td1.C2F();
                System.out.println( "Resulted Temperature: " + res );
            }
            else if( ch == 2 ) {
                td1.getVal( 104.2 );
                res = td1.F2C();
                System.out.println( "Resulted Temperature: " + res );
            }
            else {
                System.out.println( "mind your input plz" );
            }
        }
        else {

            System.out.println( "<- You select to exit ->" );
            System.exit( 0 );
        }
        // =======================================================
    }// end of main
}// end of public class

Now I think that I should convert undesired input to its previous state ie. String state.. is it right way or should Try another predefined method available in api.

-Thanks! Niks

+1  A: 

Compare characters instead of converting them to integers:

if (choise.isEmpty()) {
    //user pressed Enter
} else if (choise.charAt (0) == '1') {
    //user pressed 1
} else if (choise.charAt (0) == '2') {
    //user pressed 2
} else {
    //user pressed another key(s). exit here
}
Roman
`if (choise.isEmpty() {` - Looks like you forgot to end the parentheses of your conditional. Also, it's "choice" -- spelling and OP's variable name.
amphetamachine
Thanks! added closing bracket. about choice vs choise: I'm not a native speaker and my dictionary recognizes both words and gives the same translation. What is the correct word?
Roman
Your dictionary must be lerping the word, choice is the correct one, choise doesn't exist.
Esko
Well new code gives an error, i just change erroneous code with new one.it says:- <br>mainClass.java:43: cannot find symbol<br>symbol : method isEmpty()<br>location: class java.lang.Stringif(choice.isEmpty())<br> ^(i m not much experienced but since last few mistakes i hv learnt that this 1 occurred when method/subclass is not defined in the parent class.)however i have commented out the infected code (if-block) than program got execute.I have also checked in specification that there is no var.isEmpty() defined besides other one are defined.</pre>
NiksBestJPro
i m really i could not make my reply as readable as yours; and so many thanks to the person who have edited description of my prob above, i guess it is **tangens** :) thanks so much
NiksBestJPro
A: 

Since you're already using Scanner, you might as well use hasNextInt() and nextInt() instead of doing Integer.parseInt().

Try this snippet: enter numbers, non-numbers, etc. Enter 0 when you're done.

    Scanner sc = new Scanner(System.in);
    do {
        System.out.println("Enter code: ");
        while (!sc.hasNextInt()) {
            System.out.println("int, please!");
            sc.next();
        }
        int num = sc.nextInt();
        if (num == 0) break;
        System.out.println("Got " + num);
    } while (true);
    System.out.println("DONE!");
polygenelubricants
excellent code.!! so many thanks all of you!
NiksBestJPro