将不同类型的数据从文本文件传输到对象数组

时间:2016-03-10 01:35:59

标签: java arrays file-io io

基本上,我有一个文本文件,其行对应于对象描述(对象的变量)。据此,我的意思是,一行可能看起来像: String int int double int long。 现在,我有一个这类对象的空数组。 我的目标是将对象从文本文件传输到数组。虽然我没有找到任何东西,但我已经找到了解决方案。 这是我试图做的。我得到了java.util.InputMismatchException,虽然我不知道如何解决这个问题。非常感谢你的帮助。

     Scanner transfer = new Scanner (new FileInputStream(a));
    // we use a simple for loop to set every variable of the object to the file's order.
    for (int i = 0 ; i< Array.length; i++){
        Array[i].setLong(transfer.nextLong());
        Array[i].setString(transfer.next());
        Array[i].setInt(transfer.nextInt());
        Array[i].setString(transfer.next());
        Array[i].setDouble(transfer.nextDouble());
        Array[i].setInt(transfer.nextInt());
        }
    transfer.close();

编辑第一次转移时发生的新堆栈跟踪

    Exception in thread "main" java.lang.NullPointerException
at Driver.main(Driver.java:31)

1 个答案:

答案 0 :(得分:1)

尝试使用else子句打几个if语句,打印是否有效...

SELECT songs.title, images.path
FROM songs
LEFT JOIN images
ON images.id = songs.image_id

// ...if songs.image_id is 0, i want to just use the image_id of the corresponding album instead
等等......那样你就会知道什么是错的

LinearLayout bottomSheetViewgroup = (LinearLayout) findViewById(R.id.bottomSheet);

bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetViewgroup);

bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); //this line

这很丑陋,但是原始扫描仪可能会出现问题而不是下一行。但是,如果没有文件和代码,就无法确定。

将以下内容添加到您的代码中:

1)确认扫描仪的行数与数组中的条目数一样多

slides.children('img').each(function(){
  // here use this for the HTML element or $(this) for the jQuery wrapped one
});

2)验证每一行有6个值     //我假设你可以这样做只需使用string.split(delimeter)并计算结果数组的长度

3)确认每一行都有您指定的类型

    if(transfer.hasNextDouble()){
      Array[i].setDouble(transfer.nextDouble());
    }else{ 
    System.out.println("expecting a double, but got"+transfer.getNext());
    }

    if(transfer.hasNextLong()){
       Array[i].setLong(transfer.nextLong());
    }else{
    System.out.println("expecting a Long, but got"+transfer.getNext());
    }...

这应该是一个足够好的开始