1

I know I need to use the input.useDelimeter method, but for some reason when I try to make the two delimiters a forward slash and the newline like so, input.useDelimiter("[/\n]"); it gives me a input mismatch error. i have also tried it like ("/\n"); and ("[/\\n]");

here is the method in question.

public static void main(String[] args)
   throws FileNotFoundException {
   int month = 0;
   int day = 0;
   int year = 0;
   File file = new File("dates.txt");
   Scanner input = new Scanner(file);
   if (file.exists()){
      while (input.hasNextLine()){
         input.useDelimiter("[/]");
         month = input.nextInt();
         day = input.nextInt();
         year = input.nextInt();
         System.out.print(day + "-" + month + "-" + year + "\n");

      }
   }
}

when I use a text file that doesn't include newlines, and only forward slashes it works just fine, but for some reason I can't get it to recognize the newline character as a delimiter.

HamZa
  • 13,530
  • 11
  • 51
  • 70
  • 1
    Why do you need to recogonize new line? I would just match the forward slash and then replace the new line character. – Jorge Campos Oct 31 '13 at 16:31
  • Also, is there a reason to use `System.out.print()` and then append a `\n` rather than just `println()` ? – redFIVE Oct 31 '13 at 16:35
  • the whole print statement wont really be there, it was just for testing, and yeah i wasn't thinking. as far as @JorgeCampos 's comment, it is a homework assignment and I have to make it able to read the file my instructor will give it. – Dillon Monroe Oct 31 '13 at 16:42
  • Well, then if you have specific things in your file create an regular expression to match what you need then read the file and iterate over the matches, new line character won't matter – Jorge Campos Oct 31 '13 at 16:52

1 Answers1

0

One possibility is that the text file actually contains carriage returns rather than newlines.

My understanding is that both "[/\n]" and "[/\\n]" should work as the delimiter regexes ... if you really have newline characters to match.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096