2011-07-28 41 views
0

我一直在嘗試將用戶字符串輸入轉換爲int的不同方法,我可以比較並構建「if-then」語句。每次我嘗試測試它時,它都會拋出異常。任何人都可以看看我的Java代碼並幫助我找到方法嗎?我對此毫無頭緒(在編程中也是一個noob)。如果我打破任何規則,請讓我知道我是新來的。謝謝。將用戶輸入的字符串轉換爲數字(整數)值。

總之,這裏是代碼:

System.out.println("Sorry couldn't find your user profile " + userName + "."); 
System.out.println("Would you like to create a new user profile now? (Enter Y for yes), (Enter N for no and exit)."); 
try { 
    BufferedReader answer = new BufferedReader(new InputStreamReader(System.in)); 
    String addNewUser = answer.readLine(); 
    Character i = new Character(addNewUser.charAt(0)); 
    String s = i.toString(); 
    int answerInDecimal = Integer.parseInt(s); 
    System.out.println(answerInDecimal); 
} 
catch(Exception e) { 
    System.out.println("You've mistyped the answer."); 
e.getMessage(); 
} 
+1

您的輸入是什麼?什麼是異常堆棧跟蹤? – Atreys

+0

請發佈堆棧跟蹤 - – antlersoft

+0

只是一個側面的建議,您可以使用'String s = addNewUser.substring(0,1)'獲取第一個字符作爲字符串。 –

回答

1

看起來你似乎試圖將字符串(應該是單個字符,Y或N)轉換爲它的字符值,然後檢索字符的數字表示。

如果要打開Y或N到他們的十進制表示,你必須執行強制轉換成int:

BufferedReader answer = new BufferedReader(new InputStreamReader(System.in)); 
String addNewUser = answer.readLine(); 
char i = addNewUser.charAt(0); 
int integerChar = (int) i;  //The important part 
System.out.println(integerChar); 

這將返回字符的用戶輸入的整數表示。調用String.toUpperCase()方法以確保不同輸入的Y/N或y/n不會給出不同的值也可能很有用。

但是,您也可以根據字符本身執行if-else,而不是將其轉換爲整數。

BufferedReader answer = new BufferedReader(new InputStreamReader(System.in)); 
String addNewUser = answer.readLine(); 
char i = addNewUser.toUpperCase().charAt(0); 
if (i == 'Y') { 
    //Handle yes 
} else if (i == 'N') { 
    //Handle no 
} else { 
    System.out.println("You've mistyped the answer."); 
} 
+0

謝謝你,我從你的帖子中學到了很多東西。 – Yosi199

1

我想你的意思是讓他們爲是和否輸入0 1?也許?

你要求用戶鍵入Y或N,然後你試圖把它解析爲一個整數。這總是會拋出異常。

編輯 - 正如其他人所指出的那樣,如果你想繼續使用Y或N,你應該做的沿

String addNewUser = answer.readLine(); 
if (addNewUser.toLowerCase().startsWith("y")) { 
// Create new profile 
} 
+0

嗯,我試圖讓用戶直觀,並且我正在努力學習如何在未來處理這種情況。 – Yosi199

0

使用if (addNewUser.startsWith("Y") || addNewUser.startsWith("y")) {代替線的東西。 或(如Mark指出)if (addNewUser.toLowerCase().startsWith("y")) {。 順便提一下,看看Apache Commons CLI

+2

'if(addNewUser.toLowerCase()。startsWith(「y」)){'有點乾淨。 –

+0

非常感謝你們,我覺得這個答案完美無缺!我現在要調查「.toLowerCase()。startsWith」,因爲我還不知道它們。 – Yosi199

0

parseInt僅用於將文本數字轉換爲整數:其他所有內容都會得到NumberFormatException。

如果您想要一個字符的十進制ASCII值,只需將其轉換爲int。

+0

謝謝我現在知道區別。 – Yosi199

0

除非知道String包含有效整數,否則無法將String轉換爲int

首先,使用Scanner類的輸入是更好的,因爲它的速度更快 ,你不需要進入使用流的麻煩,如果你是 一個初學者。這是如何使用Scanner來輸入:

import java.util.Scanner; // this is where the Scanner class resides 

...

Scanner sc = new Scanner(System.in); // "System.in" is the stream, you could also pass a String, or a File object to take input from 
System.out.println("Would you like to ... Enter 'Y' or 'N':"); 
String input = sc.next(); 
input = input.toUpperCase(); 
char choice = sc.charAt(0); 
if(choice == 'Y') 
{ } // do something 

else if(choice == 'N') 
{ } // do something 

else 
System.err.println("Wrong choice!"); 

此代碼也可以縮短到一條線(但是你不會 能夠檢查第三個「錯誤的選擇」的情況):

if (new Scanner(System.in).next().toUpperCase().charAt(0) == 'Y') 
     { } // do something 
    else // for 'N' 
     { } // do something 

其次charint轉換隻需要一個明確的類型演員表:

char ch = 'A'; 
    int i = (int)ch; // explicit type casting, 'i' is now 65 (ascii code of 'A') 

第三,即使您從緩衝輸入流中輸入,您的 將輸入String。因此,從字符串 中提取第一個字符並進行檢查,只需要調用charAt() 函數並將作爲參數。它返回一個字符,它可以 然後進行比較,在單引號中的單個字符是這樣的:

String s = in.readLine(); 
    if(s.charAt(0) == 'Y') { } // do something 

第四,它是一個非常糟糕的主意,把整個程序在trycatchException最後。一個IOException可能是 由readline()函數拋出,parseInt()可能拋出一個 NumberFormatException,所以你將不能單獨處理異常。在這個問題中,代碼足夠小,可以忽略 ,但實際上會有很多可以拋出異常的函數,因此很容易丟失確切地說哪個函數拋出什麼異常並且適當的異常處理變爲非常困難。

+0

謝謝你的回答,我是初學者,並從你的答案中學到了東西。我感謝幫助! – Yosi199