Read next word in java

前端 未结 5 1424
广开言路
广开言路 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 18:54

    you're better off reading a line and then doing a split.

    File file = new File("path/to/file");
    String words[]; // I miss C
    String line;
    HashMap<String, String> hm = new HashMap<>();
    try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")))
    {
        while((line = br.readLine() != null)){
            words = line.split("\\s");
            if (hm.containsKey(words[0])){
                    System.out.println("Found duplicate ... handle logic");
            }
            hm.put(words[0],words[1]); //if index==0 is ur key
        }
    
    } catch (FileNotFoundException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
    
    0 讨论(0)
  • 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);
            }
        }
    
    0 讨论(0)
  • 2020-11-27 19:02

    You can just use Scanner to read word by word, Scanner.next() reads the next word

    try {
      Scanner s = new Scanner(new File(filename));
    
      while (s.hasNext()) {
        System.out.println("word:" + s.next());
      }
    } catch (IOException e) {
      System.out.println("Error accessing input file!");
    }
    
    0 讨论(0)
  • 2020-11-27 19:03

    Using Scanners, you will end up spawning a lot of objects for every line. You will generate a decent amount of garbage for the GC with large files. Also, it is nearly three times slower than using split().

    On the other hand, If you split by space (line.split(" ")), the code will fail if you try to read a file with a different whitespace delimiter. If split() expects you to write a regular expression, and it does matching anyway, use split("\\s") instead, that matches a "bit" more whitespace than just a space character.

    P.S.: Sorry, I don't have right to comment on already given answers.

    0 讨论(0)
  • 2020-11-27 19:10

    You already get the next line in this line of your code:

     String line = sc.nextLine();  
    

    To get the words of a line, I would recommend to use:

    String[] words = line.split(" ");
    
    0 讨论(0)
提交回复
热议问题