-2

I am given a file that will read the following:

"String",int,int
"String",int,int
"String",int,int
...

Given an unknown number of variables, a while (scanner.hasNextLine()) can solve to the number of entries. My goal is to take these three pieces of data and store them into a Node. I am using the method BinaryTree.addNode(String, int, int) for this. My issue comes to when I am trying to read in the data. I am trying to remove the commas within the document and then attempting to re-read the data using the following:

Scanner firstpass = new Scanner(file);
String input = firstpass.nextLine().replaceAll(",", "");
Scanner secondpass = new Scanner(input);
String variable1 = secondpass.next();
int variable2 = secondpass.nextInt();
int variable3 = secondpass.nextInt();

This however is a very innefective way of going about this.

UPDATED The compiling errors can be fixed with the following:

try {
            Scanner scanner1 = new Scanner(file);
            while (scanner1.hasNextLine()) {
                String inventory = scanner1.nextLine().replaceAll(",", " ");
                Scanner scanner2 = new Scanner(inventory);
                while (scanner2.hasNext()){
                    String i = scanner2.next();
                    System.out.print(i);
                }
                scanner2.close();
            }
            scanner1.close();
        } 
        catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }

which gives me the output: "String"intint"String"intint"String"intint... So I know I am on the right track. However any (spaces) within the "String" variable are removed. So they would output "SomeString" instead of "Some String". Also I still don't know how to remove the "" from the strings.

2 Answers2

0

I would instead of using

String input = firstpass.nextLine().replaceAll(",", "");
Scanner secondpass = new Scanner(input);
String variable1 = secondpass.next();
int variable2 = secondpass.nextInt();
int variable3 = secondpass.nextInt();

Use the following approach

String line = firstpass.nextLine();
String[] temp = line.split(","); 
String variable1 = temp[0];
int variable2 = Integer.parseInt(temp[1]);
int variable3 = Integer.parseInt(temp[2]);
Templum
  • 183
  • 1
  • 12
0

The format you've shown matches the CSV (Comma-Separated Values) format, so your best option is to use a CSV parser, e.g. Apache Commons CSV ™.

If you don't want to add a third-party library, you could use Regular Expression to parse the line.

Reading lines from a file should not be done with a Scanner. Use a BufferedReader instead. See Scanner vs. BufferedReader.

try (BufferedReader in = new BufferedReader(new FileReader(file))) {
    Pattern p = Pattern.compile("\"(.*?)\",(-?\\d+),(-?\\d+)");
    for (String line; (line = in.readLine()) != null; ) {
        Matcher m = p.matcher(line);
        if (! m.matches())
            throw new IOException("Invalid line: " + line);
        String value1 = m.group(1);
        int value2 = Integer.parseInt(m.group(2));
        int value3 = Integer.parseInt(m.group(3));
        // use values here
    }
} catch (IOException | NumberFormatException ex) {
    ex.printStackTrace();
}

Note that this will not work if the string contains escaped characters, e.g. if it contains embedded double-quotes. For that, you should use a parser library.

The code above will correctly handle embedded spaces and commas.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thank you for introducing me to this CSV format. I had never heard of it before. Or maybe I have and just couldn't remember. I have also pondered the use of Scanner vs BufferedReader/Writer so I appreciate that reference as well. – Aaron T Maynard Apr 09 '17 at 23:06