对于以下代码,我得到一个错误InputMismatch异常,即使我提供正确的输入

时间:2015-08-08 15:48:37

标签: java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Person {
    public enum Sex {
        Male, Female
    }
    String Name;
    int Age;
    Sex Gender;
    String EmailAddress;
    public int getAge() {
        return Age;
    }
    static public Person getInstance() {
        return new Person();
    }
    public String getPerson() {
        return Name;
    }
}
public class TestPerson {
    public static void main(String...args) {
        List list = new ArrayList();
        list.add(Person.getInstance());
        list.add(Person.getInstance());
        Scanner s = new Scanner(System. in );
        for (int i = 0; i < 1; i++) {
            System.out.println(list.get(i).Name = s.nextLine());
            System.out.println(list.get(i).Age = s.nextInt());
        }
        s.close();
    }
}

2 个答案:

答案 0 :(得分:1)

在声明列表时,您需要使用泛型。

> str(mibid)
'data.frame':   4263 obs. of  6 variables:
 $ Days: int  1 2 3 4 5 6 7 8 9 10 ...
 $ Date: Date, format: "2000-01-03" "2000-01-04" "2000-01-05" "2000-01-06" ...
 $ BID : num  8.82 8.82 8.88 8.79 8.78 8.8 8.81 8.82 8.86 8.78 ...
 $ I.S : num  0.092 0.0819 0.0779 0.0801 0.074 0.0766 0.0628 0.0887 0.0759 0.073 ...
 $ BOR : num  9.46 9.5 9.52 9.36 9.33 9.37 9.42 9.39 9.4 9.33 ...
 $ R.S : num  0.0822 0.0817 0.0828 0.0732 0.084 0.0919 0.0757 0.0725 0.0719 0.0564 ...
> head(mibid)
  Days       Date  BID    I.S  BOR    R.S
1    1 2000-01-03 8.82 0.0920 9.46 0.0822
2    2 2000-01-04 8.82 0.0819 9.50 0.0817
3    3 2000-01-05 8.88 0.0779 9.52 0.0828
4    4 2000-01-06 8.79 0.0801 9.36 0.0732
5    5 2000-01-07 8.78 0.0740 9.33 0.0840
6    6 2000-01-08 8.80 0.0766 9.37 0.0919
> 

现在,编译器知道您打算使用List<Person> = new ArrayList<Person>(); 对象填充此列表,而不是常规Person。如果没有泛型,它会假定列表将填充对象,而对象没有ObjectsName作为变量,因此编译器会抱怨。

答案 1 :(得分:1)

使用仿制药,埃里克爵士说:

List<Person> = new ArrayList<Person>();

另外:在nextLine();之后使用nextInt();方法时,

nextLine(); "\n" 作为下一次迭代的输入,因为nextInt();只接受下一个整数令牌而不是Enter Button ("\n"),然后由nextLine();案例中的iteration code Integer.parseInt(nextLine());拍摄。

使用

  • nextInt();代替"\n"

OR

  • 只需使用nextLine();跳过 public static void main(String...args) { List<Person> = new ArrayList<Person>(); list.add(Person.getInstance()); list.add(Person.getInstance()); Scanner s = new Scanner(System. in ); for (int i = 0; i < 1; i++) { System.out.println(list.get(i).Name = s.nextLine()); System.out.println(list.get(i).Age = s.nextInt()); s.nextLine(); //skips the "\n" } s.close(); } ,如下所示:

    //removed the method to make it more readable.
    public class Game implements Parcelable {
        private int _id;
        private ArrayList<Quest> _questList;
        private int _numberOfGames;
        private String _name;
        private Date _startTime;
    
        public Game(String name, ArrayList<Quest> quests, int id){
            _name = name;
            _questList = quests;
            _numberOfGames = quests.size();
            _id = id;
        }
    }