1

i have file which is in this format. 50 xxx/ 70 aaa/ 90 ccc/

the program saves the file to default directory, the same one i think its trying to read from. but it just doesnt scan anything... what is wrong please? im not good at programing sorry

i would like it to read the 50 to "prior" xxx to "text" and then use those two things in aPf.insert(prior, text).

File file = new File("poznamky.txt");
        Scanner scanner = null;
        int prior;
        String text;

        try {
            scanner = new Scanner(file);
            scanner.useDelimiter("/");

         while (scanner.hasNext()) {

          prior = scanner.nextInt();
          text = scanner.next();
          aPf.insert(prior, text);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}
Jonas
  • 121,568
  • 97
  • 310
  • 388

2 Answers2

2

I think the delimiter / is not escaped properly. Try to use like \/ to escape that. Please refer this Escaping a forward slash in a regular expression for more clarity.

Community
  • 1
  • 1
raddykrish
  • 1,866
  • 13
  • 15
  • Oh okay well i changed now delimiter to | and it works with that a bit.. but i still have problems.. but i ll try to work it out, you helped a bit :) – Filip Sokol May 08 '12 at 01:37
  • Glad that it helped. Please let us know the other problems, we can try helping you. – raddykrish May 08 '12 at 02:48
0

I think there are two ways you might go about this. The first is using reset() to reset the delimiter. So what I mean to say is go with the default delimiter of ' ' for nextInt, then call useDelimiter('/') for the next call. Then call scanner.reset().

Another way to go about this might be to grab a string based upon the / delimiter. Then split that string further based on the space as the next delimiter. Maybe use StringTokenizer to accomplish this.

pippin1289
  • 4,861
  • 2
  • 22
  • 37
  • thanks a lot! i did it a lot differently but i didnt know what string tokenizer was and so i learned something new and it helped :) thanks – Filip Sokol May 08 '12 at 02:32