| | |
Scanner and arraylist of objects
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2006
Posts: 114
Reputation:
Solved Threads: 0
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:
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
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)
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { ArrayList<Car> cars = new ArrayList<Car>(); File file = new File("car.csv"); try { Scanner scanner = new Scanner(file).useDelimiter(","); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String name = scanner.next(); int serial = scanner.nextInt(); double itemCost = scanner.nextDouble(); int itemCode = scanner.nextInt(); System.out.println(name); }//while } //try catch (FileNotFoundException e) { e.printStackTrace(); } } }
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
•
•
Join Date: Apr 2006
Posts: 114
Reputation:
Solved Threads: 0
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:
Any suggestions will be a hugely appreciated.
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)
Scanner line = new Scanner(line); line.useDelimiter(","); while(line.hasNext()) { String car = line.next(); int id = line.nextInt(); double serial = line.nextDouble(); int code = line.nextInt(); }
Any suggestions will be a hugely appreciated.
•
•
Join Date: Jul 2009
Posts: 48
Reputation:
Solved Threads: 7
•
•
•
•
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)
Scanner line = new Scanner(line); line.useDelimiter(","); while(line.hasNext()) { String car = line.next(); int id = line.nextInt(); double serial = line.nextDouble(); int code = line.nextInt(); }
Any suggestions will be a hugely appreciated.
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 =)
![]() |
Similar Threads
- (Urgent) Problem with Populating 3rd DropDown list in JSP page (JSP)
- Multiple file read, then write (Java)
- Java Arraylist Help Please (Java)
- Java - enter string (Java)
- what is the difference between list and arraylist in c#? (C#)
- Using operator[] with an ArrayList (Java)
- initialising objects help (Java)
- help with pcOrion spyware scanner (Viruses, Spyware and other Nasties)
- Storing Point2D.Double objects in an ArrayList. (Java)
Other Threads in the Java Forum
- Previous Thread: get all open windows references
- Next Thread: ... java is not as hard as we think ...
Views: 344 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






