943,649 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1812
  • Java RSS
Jul 29th, 2009
0

Scanner and arraylist of objects

Expand Post »
Hello,

I have a text file that is formatted as such:
Volkswagen, 547, 9.78, 2
Mercedes, 985, 45.77, 35
...

I am trying to figure out how use the Scanner to read from the text file and store the information into an ArrayList of objects.

ArrayList<Car> cars = new ArrayList<Car>();

I would then like to be able to use each element of the arraylist individually. For example:


cars.getName();
cars.getSerial();
...

Here is what I have so far:

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.Scanner;
  5.  
  6. public class ScannerTest
  7. {
  8. public static void main(String[] args) {
  9.  
  10. ArrayList<Car> cars = new ArrayList<Car>();
  11.  
  12. File file = new File("car.csv");
  13.  
  14. try {
  15.  
  16. Scanner scanner = new Scanner(file).useDelimiter(",");
  17.  
  18. while (scanner.hasNextLine())
  19. {
  20. String line = scanner.nextLine();
  21. String name = scanner.next();
  22. int serial = scanner.nextInt();
  23. double itemCost = scanner.nextDouble();
  24. int itemCode = scanner.nextInt();
  25.  
  26. System.out.println(name);
  27.  
  28.  
  29. }//while
  30. } //try
  31. catch (FileNotFoundException e)
  32. {
  33. e.printStackTrace();
  34. }
  35.  
  36. }
  37. }

For some reason, I am getting a Mismatch input error and the items are not being added to an arraylist for proper usage. Is there a better method to using the file elements individually?

Any assitance will appreciated.
Thank you
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Jul 30th, 2009
0

Re: Scanner and arraylist of objects

I really hope that someone can provide some suggestions or help to guide me in the right direction.
I am trying to use a scanner to parse a string and use its elements with methods. It is formatted as so:

String line = "Mercedes, 124 124.11, 45";

When I parse the string, I get a java.util.InputMismatchException

here is a snippet:

Java Syntax (Toggle Plain Text)
  1. Scanner line = new Scanner(line);
  2. line.useDelimiter(",");
  3.  
  4. while(line.hasNext())
  5. {
  6. String car = line.next();
  7. int id = line.nextInt();
  8. double serial = line.nextDouble();
  9. int code = line.nextInt();
  10. }

Any suggestions will be a hugely appreciated.
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Jul 30th, 2009
0

Re: Scanner and arraylist of objects

Scanner line = new Scanner(line);
This looks wrong. There is no constructor for Scanner that takes a Scanner as a parameter.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is online now Online
5,764 posts
since Apr 2008
Jul 31st, 2009
0

Re: Scanner and arraylist of objects

Click to Expand / Collapse  Quote originally posted by KimJack ...
I really hope that someone can provide some suggestions or help to guide me in the right direction.
I am trying to use a scanner to parse a string and use its elements with methods. It is formatted as so:

String line = "Mercedes, 124 124.11, 45";

When I parse the string, I get a java.util.InputMismatchException

here is a snippet:

Java Syntax (Toggle Plain Text)
  1. Scanner line = new Scanner(line);
  2. line.useDelimiter(",");
  3.  
  4. while(line.hasNext())
  5. {
  6. String car = line.next();
  7. int id = line.nextInt();
  8. double serial = line.nextDouble();
  9. int code = line.nextInt();
  10. }

Any suggestions will be a hugely appreciated.
Hi
In the example above you have named your scanner as line and your string as line. Thats is confusing and makes it difficult for you to search for faults. In the string there is no "," after the id number. Maybe you just missed it writing it down in your post here? If you also missed it in your code you will get an error.

I could be that your .useDelimiter(",*"); works better if you tell it there is a space after the "," like this .useDelimiter(",\\s*");

You probably will get some error trying to catch the serial like this:
double serial = line.nextDouble();
Try like this instead:
String s = new String();
double serial = Double.valueOf(s = line.next());
This way you catch the serial as a string and makes a double out of the value from the string.

I am not really sure of why you have put in inside a while loop but my advise is to take it out of the loop if you have trouble until you catch does results you want and first then put your stuff inside any kind of loop.

Use System.out.println(); to test what you get and how it looks.
I can not see that you are closing your scanner?
You do that in the same way you are closing streams, whit a simple .close();

Hope this will give you some help.
Good luck =)
Sponsor
Reputation Points: 40
Solved Threads: 13
Junior Poster in Training
sneaker is offline Offline
76 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: get all open windows references
Next Thread in Java Forum Timeline: ... java is not as hard as we think ...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC