0

Good morning,

I want to read a .txt file using Scanner in this format:

Scanner scan = < trains>< intercity>< from>< stationname>

Question: how to seperate the tokens such that I will remain with tokens

trains intercity from stationname

such that I can use scan.next() ?

Thank you very much!

Lokesh Pandey
  • 1,589
  • 20
  • 37
Eef266
  • 1

1 Answers1

1

You can make a Scanner instance from the String and use a regex for the delimiter. Following is an example:

    String a = "< trains>< intercity>< from>< stationname>";
    Scanner scanner = new Scanner(a);
    scanner.useDelimiter(Pattern.compile("\\<|\\>|\\>\\<"));
    while(scanner.hasNext()) {
        System.out.print(scanner.next());
    }
    scanner.close();lose();

Output:

 trains intercity from stationname
Yash
  • 6,123
  • 4
  • 15
  • 32