我的文本是
1
central
1
2
final
2
这样的
我的   ArrayList<Station>  allStationNames = new ArrayList<Station>();是这样定义的
文本文件中读取的数据来填充ArrayList构成一个一个Station,Station是由(int,string,int)这样构造的
救救萌新吧 萌新都懵了
private void setUpStationData(String fname) throws IOException{

Scanner inputFile = new Scanner (
    new File ( fname ) 
);
String firstLine = inputFile.nextLine();
inputFile.close(); }

解决方案 »

  1.   


    有对应的set方法
      

  2.   

    Scanner sc = new Scanner(new File("C:\\Users\\lenovo\\Desktop\\abc.txt"),"gbk"); // 第二个参数为文本对应的编码格式,格式不对应时,读不出数据
    int lineIndex = 1;
    Station s = null; 
    List<Station> list = new ArrayList<Station>();

    while (sc.hasNextLine()) {
    switch (lineIndex % 3) {
    case 1 :
    s = new Station();
    list.add(s);
    s.setStationNo(Integer.valueOf(sc.nextLine()));
    break;
    case 2 :
    s.setName(sc.nextLine());
    break;
    case 0 :
    s.setZone(Integer.valueOf(sc.nextLine()));

    break;
    default :
    break;
    }
    lineIndex++;
    }
    System.out.println(list);
    sc.close();