I am reading in a CSV file if Vice Presidents with their names and ages. The issue I am having is trying to split the string on a space and a period.
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class VicePresidents {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String filename = "VicePresidentAges.csv";
File file = new File(filename);
Scanner infile = new Scanner(file);
ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> ages = new ArrayList<Integer>();
while (infile.hasNext())
{
String line = infile.nextLine();
String[] tokens = line.split("[" ".]");
System.out.println(tokens[0]);
//put the tokens into their correct ArrayList
}
infile.close();
I am getting an error message on the split line. What is weird is that if I split with a comma, the output I get is correct except the first name comes out like this: John Adams. The part that confuses me is the file doesn't have any commas, which is why I am trying to split on a space and a period (middle initial). Not understanding how using a comma with no comma in the file works. My book has using two delimiters as ("[@.]"). But when I try doing line.split("[" ".]"); I get an error "Syntax error on token "".]"", delete this token" *This has been edited to include the error message as requested. Can this please be re-opened?