2016-05-13 29 views
0

我想知道如何使用掃描器將用戶輸入轉換爲我已經聲明的整數。我已經創建了person1對象,並在'Person'類中聲明瞭變量firstName,lastName,age和gender。我知道我應該使用解析,但我不確定如何在我的代碼中實現它。將字符串轉換爲整數或對象

Scanner user_input = new Scanner(System.in); 

     System.out.println("Please input your details."); 
     System.out.println("First Name:"); 
     person1.setFirstName(user_input.nextLine()); 
     System.out.println("Last Name:"); 
     person1.lastName = user_input.nextLine(); 
     System.out.println("Age:"); 
     person1.age = user_input.nextLine(); 
     System.out.println("Gender:"); 
     person1.gender = user_input.nextLine(); 

第9行和第11行需要修改。

+0

不想在你的代碼段數行... – Guillaume

+0

您還可以使用persont1.age =的Integer.parseInt(user_input.nextLine());考慮到在輸入時你只輸入數字值 –

回答

0

的年齡,用途:

person1.age = user_input.nextInt();