954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Scanner and arraylist of objects

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 cars = new ArrayList();

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:

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

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
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:

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.

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 

Scanner line = new Scanner(line);
This looks wrong. There is no constructor for Scanner that takes a Scanner as a parameter.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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:

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.

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 =)

sneaker
Junior Poster in Training
Team Colleague
77 posts since Jul 2009
Reputation Points: 40
Solved Threads: 13
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You