Skip to content
Advertisement

How do I read strings from a file that already contain double quotes?

I have a list of names in a .txt file which are in the format:

“Tim”, “Dave”, “Simon”

The input will always be single value names in quotes, comma separated and on a single line.

I want to read these into String[] names.

I have the following code, but the output puts each of them in double quotes, meaning it looks like:

“”Tim””, “”Dave””, “”Simon””

I’m also not able to use any third party libs.

How do I get it so that each element in the String array only has one set of double quotes?

String[] names = {};

// arraylist to store strings
List<String> listOfStrings = new ArrayList<String>();

// load content of file based on specific delimiter
Scanner sc = new Scanner(new FileReader("names.txt")).useDelimiter(",");
String str;

while (sc.hasNext()) {
    str = sc.next();
    listOfStrings.add(str);
}

Advertisement

Answer

Sorry. Actually this is better

add(s.replace(""", ""));
Advertisement