-2

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

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  
Anonymous
  • 23
  • 4
  • 1
    Is your file always the same? If you want to do this your file has to have the same format every time. Also why are you using `Scanner`? There is much better alternatives for this type of task – cheshire Nov 19 '20 at 19:28
  • 1
    Can you add the code for the objects you are putting these into? IE Item, ClothingItem, Computer, FoodItem, Beverages? Also, is each line in the file representative of it's own object? Are you trying to read each line of the file, process it as an object, then read the next? I'm confused what you're actually trying to do.. – Dan Hessler Nov 19 '20 at 19:29
  • You should be reading the file line by line, splitting each line into tokens and converting the line into object. From the first glance, the file contains 6 lines but the code tries to create 7 objects, there's only 1 computer entry. The other huge problem is that you do not check for the end of the file (only one `hasNext` for a ton of `next/nextInt/nextDouble`) – Alex Rudenko Nov 19 '20 at 20:01
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) This is another source of multiple errors in the presented code snippet. – Alex Rudenko Nov 19 '20 at 20:04
  • @cheshire, I can create the txt file in a different however, not all classes have the same attributes, for example computer has 9 attributes & clothing item has 7 and without saying both have completely different types – Anonymous Nov 20 '20 at 06:10
  • @DanHessler Yes, each line in the file representative of an object, am trying to read each line of the file, and store it into an arraylist of multiple objects – Anonymous Nov 20 '20 at 06:39

1 Answers1

0

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.

You made 2 mistakes.

1)The first mistake is that you have 1 line that does not correspond to your data.

        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. You use the the method: 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. 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. So when you call nextInt we get a string as our next() value is a string.

change hasNext() to next()

Also there is no need for a while loop if you reference every value manually.

Also your while loop that prints the item values is just gonna print the object string which isn't helpful.

CodinGuy
  • 35
  • 1
  • 5
  • Hello there, appreciate the answer, I fixed pretty much everything you pointed out however the error still exists, it runs pretty fine when I have exactly one line in the txt file, let's say for example a file that has the following `1003 LenovoThinkpad 1050 2 Samsung i7-10610U SM123 8 512 ` and only & int main only one object of computer. However when you start to have multiple lines, it just simply doesn't work – Anonymous Nov 20 '20 at 06:28
  • Edited the question to match the code that I currently have – Anonymous Nov 20 '20 at 07:31
  • Your code works with me, are still getting the same error. – CodinGuy Nov 20 '20 at 12:01
  • it gives the following error Exception in thread "main" java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at java.util.ArrayList.toArray(ArrayList.java:408) at assignment.Assignment.main(Assignment.java:34) – Anonymous Nov 20 '20 at 12:33
  • Could you point out which line is line 34, in your code. – CodinGuy Nov 20 '20 at 12:53
  • it pretty much gives the same error for any line of the array object, I even tried to remove the line that gives the error and it just ends up giving the same error for the line that comes after it – Anonymous Nov 20 '20 at 13:20
  • The error is caused by your parameter being a array and you list datatype is not an array. – CodinGuy Nov 20 '20 at 13:44
  • Do you have any suggestions – Anonymous Nov 20 '20 at 13:55
  • It depends on what you gonna do with the arrays or do you need the array. – CodinGuy Nov 20 '20 at 14:19
  • It's basically a store program where I need to store the items from the file to the array then the user will have a cart where he can add any of those items to his cart – Anonymous Nov 20 '20 at 14:26