Пример #1
0
  public void run() {
    BufferedReader rd = openFileReader("enter filename: ");
    try {
      while (true) {
        String line = rd.readLine();
        if (line == null) break;
        // println(line);
        // count lines
        lines++;
        // println("Lines: " + lines);
        // println("File: " + fileName);
        // count characters
        chs += line.length();
        // println("Characters: " + chs);
        // count words
        StringTokenizer tokenizer = new StringTokenizer(line, " '");
        while (tokenizer.hasMoreTokens()) {
          String token = tokenizer.nextToken();
          words++;
          // println("Words: " + words + " " + token);
        }
      }

      rd.close();
    } catch (IOException ex) {
      println("oops errorrr");
    }

    println("Total Lines: " + lines);
    println("Total Words: " + words);
    println("Total Characters: " + chs);
  }
  private void parseLine(String line) {
    // gets the name
    int nameEnd = line.indexOf(" ");
    Name = line.substring(0, nameEnd);

    // gets the popularity ranking and puts it into an array
    String numbers = line.substring(nameEnd + 1);
    StringTokenizer tokenizer = new StringTokenizer(numbers);
    for (int count = 0; tokenizer.hasMoreTokens(); count++) {
      int popularityRank = Integer.parseInt(tokenizer.nextToken());
      rankings[count] = popularityRank;
    }
  }