简体   繁体   English

将文件读入多个对象的arraylist java

[英]Reading a file into an arraylist of multiple objects java

So this is my main class, am basically trying to read from a txt file a list of items and am trying to store them in an arraylist of multiple objects but for some reason I keep getting an inputmissmatch error if I tried to have multiple lines in the txt file and multiple objects, however let's say I have a txt that has one line & I add one object to the list, it works perfectly fine所以这是我的主类,我基本上试图从一个 txt 文件中读取一个项目列表,并试图将它们存储在一个包含多个对象的数组列表中,但是由于某种原因,如果我尝试在其中输入多行,我会不断收到 inputmissmatch 错误txt 文件和多个对象,但是假设我有一个包含一行的 txt & 我将一个对象添加到列表中,它工作得很好

public static void main(String[] args) throws FileNotFoundException, IOException {
        
                //    FileReader fr = new FileReader("Item2.txt");
                Scanner file = new Scanner(new File("item.txt"));
                ArrayList<Item> List = new ArrayList<Item>();
             
                   List.add(new ClothingItem(file.next().charAt(0), file.next(), file.next(), file.nextInt(), file.next(), file.nextDouble(), file.nextInt()));
                    List.add(new ClothingItem(file.next().charAt(0), file.next(), file.next(), file.nextInt(), file.next(), file.nextDouble(), file.nextInt()));
                    List.add(new Computer(file.nextInt(), file.next(), file.nextDouble(), file.nextInt(), file.next(), file.next(), file.next(), file.nextDouble(), file.nextDouble()));
                    List.add(new FoodItem(file.nextDouble(), file.next(), file.nextInt(), file.next(), file.nextDouble(), file.nextInt()));
                    List.add(new FoodItem(file.nextDouble(), file.next(), file.nextInt(), file.next(), file.nextDouble(), file.nextInt()));
                    List.add(new Beverages(file.nextInt(), file.next(), file.nextInt(), file.next(), file.nextDouble(), file.nextInt()));
           
        
                ClothingItem[] list1 = List.toArray(new ClothingItem[List.size()]);
                ClothingItem[] list2 = List.toArray(new ClothingItem[List.size()]);
                Computer[] list3 = List.toArray(new Computer[List.size()]);
                FoodItem[] list4 = List.toArray(new FoodItem[List.size()]);
                FoodItem[] list5 = List.toArray(new FoodItem[List.size()]);
                Beverages[] list6 = List.toArray(new Beverages[List.size()]);
        
                for (Item A : List) {
                    System.out.println(A);
         }
                
            }
        }  

File that am trying to read正在尝试读取的文件

M RED MALE 1001 Shirt 19.0 100
L BLACK FEMALE 1002 Shoes 49.0 25   
1003 Thinkpad 1050.0 2 Samsung i710610U SMEF 8.0 512.0
5.0 NotFresh 1004 GreenPeas 3.0 117 
5.0 Fresh 1005 Lettuce 2.0 90 
1006 NotAlcholic 1981 Cola 3.0 50  

This error occur when you use使用时出现此错误

file.nextInt() or file.nextDouble()

on a next value which is not an integer nor double.下一个既不是整数也不是双精度值的值。 In the lines of your code a string ends up with nextInt() or nextDouble() which causes the error.在您的代码行中,字符串以导致错误的 nextInt() 或 nextDouble() 结尾。

You made 2 mistakes.你犯了2个错误。

1)The first mistake is that you have 1 line that does not correspond to your data. 1)第一个错误是您有1行与您的数据不符。

        List.add(new Computer(file.nextInt(), file.next(), file.nextDouble(), file.nextInt(), file.next(), file.next(), file.next(), file.nextDouble(), file.nextDouble()));

This is the fourth line in your loop.这是循环中的第四行。 This line causes conflictions as it was design to run with the third line from your text file, but at this line of code we using it on the fourth line from your text file.此行会导致冲突,因为它设计为与文本文件中的第三行一起运行,但在这行代码中,我们在文本文件的第四行中使用它。 Just comment it out or remove it.只需将其注释掉或删除即可。

2)The last mistake is in the last 3 lines of your code. 2)最后一个错误出现在代码的最后 3 行。 You use the the method: file.hasNext()你使用的方法是:file.hasNext()

hasNext returns a boolean which tells us whether there is more data after our current position, but it does not go to the next value. hasNext 返回一个布尔值,它告诉我们在当前位置之后是否还有更多数据,但它不会转到下一个值。 The cause of this mistake is for example when we call next() again we should get a string, but you called hasNext(), it does not move to the next value.这个错误的原因是例如当我们再次调用 next() 时我们应该得到一个字符串,但是你调用了 hasNext() ,它没有移动到下一个值。 So when you call nextInt we get a string as our next() value is a string.因此,当您调用 nextInt 时,我们会得到一个字符串,因为我们的 next() 值是一个字符串。

change hasNext() to next()

Also there is no need for a while loop if you reference every value manually.如果您手动引用每个值,也不需要 while 循环。

Also your while loop that prints the item values is just gonna print the object string which isn't helpful.此外,您打印项目值的 while 循环只会打印无用的对象字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM