0

hi this is my file contents

#nadal\r\n#federer\r\n*#djokovic\r\n#murray\r\n

i want to use \n as delimiter i want my output as

#nadal\r
#federer\r
*#djokovic\r
#murray\r

i googled about it but i could not find answer

here is my code written in java

    Scanner scan=null;

    scan = new Scanner(new FileReader("//Users//Rakesh//Desktop//input.txt"));

    // initialize the string delimiter
    scan.useDelimiter(Pattern.compile("[\\r\\n;]+"));



    // Printing the delimiter used
    System.out.println("The delimiter use is "+scan.delimiter());

    // Printing the tokenized Stringhashtable
    while(scan.hasNext()){
        System.out.println(scan.next());
    }

    // closing the scanner stream
    scan.close();

i am getting output as whole line instead of in new line.Can any one help on this TIA

Rajesh M
  • 634
  • 11
  • 31

1 Answers1

1

Your program works fine for me, producing one output line for each input line. Except for the detail that, to be the output lines ended in \r, the delimitters must be like this:

    scan.useDelimiter(Pattern.compile("[\\n]+"));

(The semicolon delimitter is no use with that input format).

Little Santi
  • 8,563
  • 2
  • 18
  • 46