0
我有一個類項目,我需要創建一個需要用戶輸入的程序,我想創建一個獲取用戶輸入並返回值爲main的方法,出於某種原因我使用下面的代碼時出現以下錯誤,掃描儀以不同的方式讀取字符串
Names.java:40:錯誤:不兼容的類型 userInput = kb.next(); ^ 要求:整數 發現:字符串
public static void main(String[] arg)
throws FileNotFoundException{
Scanner kb = new Scanner(System.in);
Scanner fileInput = new Scanner(new File("names.txt"));
System.out.println("This program allows you to search through the");
System.out.println("data from the Social Security Administration");
System.out.println("name popularity since 1900.");
int userInput;
do{
userInput = getInput(kb);
if(userInput == 1)
namePopularity();
if(userInput == 2)
nameCompare();
if(userInput == 3)
nameRank();
}while(userInput == 4);
}
//Gets the user input
public static int getInput(Scanner kb){
int userInput;
do{
System.out.println("Choose a task number from the following:");
System.out.println("1-See histogram of a name's popularity");
System.out.println("2-Compare two names in a specific decade");
System.out.println("3-Show what name had a specific rank for a certain decade");
System.out.println("4-Exit Program");
userInput = kb.next();
}while(!userInput.hasNextInt());
return userInput;
}
我試圖打破它歸結爲一個簡單的版本,下面,但我不斷收到該錯誤信息,我不知道爲什麼它的讀它的字符串,而不是掃描儀,有人可以幫助我,它的工作原理,直到我嘗試以不同的方法使用掃描儀。
public static void main(String[] arg)
throws FileNotFoundException{
Scanner kb = new Scanner(System.in);
Scanner fileInput = new Scanner(new File("names.txt"));
int userInput;
System.out.println(getInput(kb));
}
public static int getInput(Scanner kb){
int userInput;
System.out.println("write something");
userInput = kb.next();
return userInput;
}
非常感謝你 –