我在這個項目上,允許用戶輸入名稱到輸出窗口工作,但他們並不想能夠輸入數字。我想不出正確的代碼行,使其向他們無法輸入號碼(如果他們這樣做是否會給他們一個錯誤消息)。使的ArrayList()不允許號的編輯輸入
import java.util.*;
public class UserInput
{
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
Scanner stdin = new Scanner(System.in);
do
{
Collections.sort(list);
System.out.println("Current list is " + list);
System.out.println("Add more? (y/n)");
if (stdin.next().startsWith("y"))
{
System.out.println("Enter: ");
list.add(stdin.next());
}
else
{
break;
}
}
while (true);
System.out.println("List is " + list);
String[] arr = list.toArray(new String[0]);
System.out.println("Array is " + Arrays.toString(arr));
}
}
public void add(String string) {
try {
double d = Double.parseDouble(string);
} catch(NumberFormatException e) {
// throw a exception or something.
return;
}
super.add(string);
return;
}
這裏是我的代碼!
非常感謝您的幫助!
我只是想這一點,是給我的錯誤「UserInput.java:31:分析到達文件的末尾」。有什麼建議? – Boom 2013-03-25 03:29:32
什麼文件 – 2013-03-25 03:31:03
是行號31定了!太謝謝你了 – Boom 2013-03-25 03:34:07