我試圖用Java編寫一個程序,該程序將接受用戶輸入並轉換爲攝氏或華氏。因此用戶輸入一些數字,一個空格,然後是C或F.禰程序編譯罰款,但是當我嘗試測試它,我得到以下信息:MSDOS窗口中的Java程序錯誤:線程「main」中的異常java.lang.NumberFormatException
Exception in thread "main" java.lang.NumberFormatException: For input string: (whatever number, space, F/C I put in to test it0
at java.lang.Integer.parseInt<Integer.java:492>
at java.lang.Integer.parseInt<Integer.java:527>
at Temp.Conv.main<TempConv.java:14>
我猜的Java不像我試圖使用Parse搜索字符串中的整數。 有關如何完成它的任何建議?
下面是代碼:(只要你知道,我知道的支架和空間是關閉的,但這網站不會讓我來解決它)
public class TempConv
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String reply = "y";
while(reply.equalsIgnoreCase("y"))
{
System.out.println("Enter temp (ex. 70 F or 23 C): ");
String CF = input.nextLine(); // inputs string
int temp = Integer.parseInt(CF); // to get integers from string
for (int i = 0; i < CF.length(); ++i)
{
char aChar = CF.charAt(i);
if (aChar == 'F') // looking in string for F
// go to f2c()
{
f2c(temp);
}
else if (aChar == 'C') // looking for C
// go to c2f()
{
c2f(temp);
}
}
System.out.println("Would you like to covert another temp? <y/n> ");
reply = input.next();
}
}
static void f2c(int j)
{
int c = (j - 32)*(5/9);
System.out.println(j + "F = " + c + "C");
}
static void c2f(int k)
{
int f = (k*(5/9))+32;
System.out.println(k + "C = " + f + "F");
}
}