I am writing a program in which I want to terminate the program by pressing any key (regardless of character or number), therefore I converted the ant from the string using the Integer.parseInt (variable) method and compare the option if it is not the desired option, then it should end the program, but this is an error Exception "Main" Java.lang.NumberFormatException: For Input String: "D" .
The program code is as follows: -
public class mainClass {public static zero main (string [] AR) {double res = 0; Scanner = new scanner (System.in); TDITA TD1 = new TDATA (); // Another class object system. OutprintLN ("**** Temperature Conversion ****"); System.out.println ("--------------------------"); System.out.println ("press 1-> C2F "); System.out.println (" press 2-> F2C "); Println (" & lt; - Press any key to exit -> "); String option = in. NextLine (); // ============================================ ==== ======= int ch = integer. Parasetter (preferred); system.out.println ("string has been converted:" + ch); // if confirmation is converted to an integer (Ch == 1 || ch == 2) {if (ch == 1) {td1.getVal (37.4); Res = td1.C2F (); System.out.println ("Resultant Temperature:" + res) ;} And if (f == 2) {td1.getVal (104.2); Res = td1.F2C (); System.out.println ("Results Verveup temperature: "+ res;) Else {System.out.println (" mind your input plz ");}} Else {println (" & lt; - you choose to exit -> gt; "); System.exit (0);} // ======================================== ======= ========} // main end} // end of the public class
Now I think I would do my former status ie Should change the unwanted input. String state .. This is not the right way or any other predefined method should be available in the API .
Thank you! Compare the characters instead of converting them into integers:
Code> If (Choise.isEmpty ()) {// press enter user} else if (choise.charAt (0) == '1') {// user pressed 1} and if (choise.charAt (0) == '2') {// user pressed 2} and {// user quit pressing another key (s)}
Comments
Post a Comment