需要识别哪个变量?

时间:2014-07-21 16:30:38

标签: java variables

我是java的初学者。第24,27,29,30,35,36,37,42,43和46行出现错误15次,说:

 //Tokens cannot be resolved to a variable

// i cannot be resolved to a variable

这些错误想要识别哪些变量?因为我认为我确定了所有内容,而且我不明白我的代码仍然需要哪些变量。

这是我的代码:

import java.util.Scanner;

public class Jo_code {

    public static int wordFrequency(String s) {
        String tokens[] = s.split(" ");// splits the words
        int count = tokens.length;
        return count;
    }

    public static void main(String args[]) {
        System.out.println("Write your sentence or type END to quit the program:");// prints out the first instruction.
        Scanner scan = new Scanner(System.in); //object initialisation.
        String line = " ";//declaration for letters(String) characters.
        int max = 20; //declaration for number(int) characters.
        while ((line = scan.nextLine()) != null) { //scanner instruction, get a line from the key board.
            if (line.equals("END")) {
                break;// if you are happy with the code, then type "END", and the code will stop running.
            } else { // if you decide not to type "END" the program will continue to allow you to type sentences.  
                String[] array = line.split(" ");// splits the words
                System.out.println("your sentence is " + line);//prints out what you have typed.
                System.out.println("The total of words for the line is " + tokens.length);// prints out the total of words that you have typed.
                int maxTokensLength = 0;
                int tokensLength = 0;
                for (int i = 0; i < tokens.length; i++) {// this line of code checks for what must be true to carry on.
                    array[i] = array[i].replaceAll("a-zA-Z]", "");
                }
                tokensLength = array[i].length();
                tokensLength = tokens[i].length();
                if (tokensLength > maxTokensLength) {
                    maxTokensLength = tokensLength;
                }
            }
        }
        int[] intArray = new int[maxTokensLength + 1];
        for (int i = 0; i < tokens.length; i++) { // this line of code checks for what must be true to carry on.
            intArray[array[i].length()]++;
        }
        for (int i = 1; i < intArray.length; i++) { // this line of code checks for what must be true to carry on.
            System.out.printf("%word(s) of length %d<br>", intArray[i], i);
        }
        for (int i = 0; i < tokens.length; i++) { // this line of code checks for what must be true to carry on.
            System.out.println("word " + i + ": " + tokens[i] + " = " + tokens[i].length() + " characters");
        }

        System.out.println("The length for the word " + tokens[i] + " is = " + tokens[i].length());//This line of code prints out the word frequency for each word. 
        System.out.println("The word frequency of the whole sentence is =");//results 
        System.out.println("type END to quit");//instructions
    }
}

非常感谢您的帮助,非常感谢!

1 个答案:

答案 0 :(得分:2)

首先,你的第一个for循环没有打开和关闭括号。这意味着只有第一个语句被认为是for循环的一部分。声明'tokensLength = array [i] .length();'无法到达变量i,因为它不在for循环中。我相信在第一个for循环中为你想要的东西添加开括号和右括号应该修复它