开发者

Should convert String to Int in java @ 1.5 or use other method?

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 = In开发者_如何学Cteger.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


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
}


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!");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜