0
public void readToooooolData(String fileName) throws FileNotFoundException
    {
         File dataFile = new File(fileName);
        Scanner scanner = new Scanner(dataFile);
        scanner.useDelimiter("( *, *)|(\\s*,\\s*)|(\r\n)|(\n)");

         while(scanner.hasNextLine()) {
             String line = scanner.nextLine();
        if(!line.startsWith("//") || !(scanner.nextLine().startsWith(" ") )) {
          String toolName = scanner.next();
         String itemCode = scanner.next();
         int timesBorrowed = scanner.nextInt();
         boolean onLoan = scanner.nextBoolean();
         int cost = scanner.nextInt();
         int weight = scanner.nextInt();
         storeTool( new Tool(toolName, itemCode, timesBorrowed, onLoan, cost, weight) );
         scanner.nextLine();
        } 
        scanner.close();

    }

}

txt file:

// this is a comment, any lines that start with //
// (and blank lines) should be ignored

// data is toolName, toolCode, timesBorrowed, onLoan, cost, weight  
Makita BHP452RFWX,RD2001, 12 ,false,14995,1800  
Flex Impact Screwdriver FIS439,RD2834,14,true,13499,1200  
DeWalt D23650-GB Circular Saw, RD6582,54,true,14997,5400  
Milwaukee DD2-160XE Diamond Core Drill,RD4734,50,false,38894,9000  
Bosch GSR10.8-Li Drill Driver,RD3021,25, true,9995,820  
 Bosch GSB19-2REA Percussion Drill,RD8654,85,false,19999,4567  
Flex Impact Screwdriver FIS439, RD2835,14,false,13499,1200  
DeWalt DW936 Circular Saw,RD4352,18,false,19999,3300  
Sparky FK652 Wall Chaser,RD7625,15,false,29994,8400  
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
azzy
  • 21
  • 2
  • Hello and welcome. Please include your expected results, your attempted solutions, and **why they didn't work**. Please see the [Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922) for more details. – Federico klez Culloca Feb 24 '20 at 15:52

1 Answers1

3

There are some problems with your code.

  • It's better to use try with resources to open the Scanner

  • Some of the delimiters which you specified are duplicated, and some are unnecessary. Space is included into \s, so (\s*,\s*) is a duplicate of ( *, *), and (\r\n)|(\n) is not needed.

  • You read a line from the file and test if for being a comment or an empty line - ok. Then you need to extract tokens from the already read line, but you cannot use scanner.next() for this, because it will retrieve you the next token after the line which you have just read. So what your code is actually doing is trying to parse the information on the line after the not-comment/not-empty line.

  • There is also another scanner.nextLine() at the end of the loop, so you skip one more line from the file.

public void readToooooolData(String fileName) throws FileNotFoundException {
    File dataFile = new File(fileName);
    try (Scanner scanner = new Scanner(dataFile)) {
      while (scanner.hasNextLine()) {
        String line = scanner.nextLine().trim();
        if (line.startsWith("//") || line.isEmpty()) {
          continue;
        }
        String tokens[] = line.split("\\s*,\\s*");
        if (tokens.length != 6) {
          throw new RuntimeException("Wrong data file format");
        }
        String toolName = tokens[0];
        String itemCode = tokens[1];
        int timesBorrowed = Integer.parseInt(tokens[2]);
        boolean onLoan = Boolean.parseBoolean(tokens[3]);
        int cost = Integer.parseInt(tokens[4]);
        int weight = Integer.parseInt(tokens[5]);
        storeTool( new Tool(toolName, itemCode, timesBorrowed, onLoan, cost, weight) );
      }
    }
  }
Alex Sveshnikov
  • 4,214
  • 1
  • 10
  • 26
  • Hello and welcome. While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Federico klez Culloca Feb 25 '20 at 07:51
  • I added some explanations to the answer. – Alex Sveshnikov Feb 25 '20 at 08:15