Read next word in java

前端 未结 5 1439
广开言路
广开言路 2020-11-27 18:08

I have a text file that has following content:

ac und
accipio annehmen
ad zu
adeo hinzugehen
...

I read the text file and iterate through t

5条回答
  •  無奈伤痛
    2020-11-27 19:00

    You do not necessarily have to split the line because java.util.Scanner's default delimiter is whitespace.

    You can just create a new Scanner object within your while statement.

        Scanner sc2 = null;
        try {
            sc2 = new Scanner(new File("translate.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();  
        }
        while (sc2.hasNextLine()) {
                Scanner s2 = new Scanner(sc2.nextLine());
            while (s2.hasNext()) {
                String s = s2.next();
                System.out.println(s);
            }
        }
    

提交回复
热议问题