Java 如何修复此程序中的编译器错误,该程序对用户输入的字符、换行符、单词进行计数,并将它们作为数组返回

Java 如何修复此程序中的编译器错误,该程序对用户输入的字符、换行符、单词进行计数,并将它们作为数组返回,java,Java,我正在尝试编写一个Count.java程序,该程序使用三个计数的数组作为返回值,对用户输入的字符串中的字符、换行符和单词进行计数 我编写了一个程序,该程序应该将用户从主方法输入的字符串作为变量文本存储在方法public static int[]analyze(string text)中,然后使用三个整数计数变量wordCount、charCount和lineCount对其进行分析 public class Count { public static void main(String[

我正在尝试编写一个Count.java程序,该程序使用三个计数的数组作为返回值,对用户输入的字符串中的字符、换行符和单词进行计数

我编写了一个程序,该程序应该将用户从主方法输入的字符串作为变量文本存储在方法
public static int[]analyze(string text)
中,然后使用三个整数计数变量
wordCount
charCount
lineCount
对其进行分析

public class Count { 

    public static void main(String[] args) {
        System.out.print("Please enter a string: ");
        System.out.println(arr);
        System.out.println("Total number of words: " + arr[0]);
        System.out.println("Total number of characters: " + arr[1]);
        System.out.println("Total number of newlines: " + arr[2]);

    }

    public static int[] analyze(String text) {
        Scanner in = new Scanner(System.in);
        String text = in.nextLine();
        int wordCount = 0;
        int charCount = 0;
        int lineCount = 1;
        int[] arr;
        arr = new arr[] {wordCount, charCount, lineCount}; 

        for (int i = 0; i < text.length(); i++) {

            if (text.charAt(i) == ' ' && text.charAt(i + 1) != ' ') {
                wordCount++;
            }

            if (text.charAt(i) != ' ') {
                charCount++;
            }
        }

        if (text.charAt(i) == text.charAt(i)) {
            lineCount++;
        }

        return arr;
    }

}
公共类计数{
公共静态void main(字符串[]args){
System.out.print(“请输入字符串:”);
系统输出打印项次(arr);
System.out.println(“总字数:+arr[0]);
System.out.println(“字符总数:+arr[1]);
System.out.println(“换行总数:+arr[2]);
}
公共静态int[]分析(字符串文本){
扫描仪输入=新扫描仪(系统输入);
字符串text=in.nextLine();
int字数=0;
int charCount=0;
int lineCount=1;
int[]arr;
arr=newarr[]{wordCount,charCount,lineCount};
对于(int i=0;i

我希望它输出用户输入字符串的字符数、换行符和单词数。但是,当我尝试运行它时,编译器无法识别我的字符串变量text和array arr。有没有关于如何解决此问题的建议?

我认为您应该分析可用新字符串的输入数据,因为不要将其存储在一位
string
对象中。另一个注意事项是使用
数据
对象而不是数组;这更直观:

public class Count {

    public static void main(String... args) {
        try (Scanner scan = new Scanner(System.in)) {
            System.out.print("Please enter a string (empty line to stop): ");

            Data data = analyze(scan);

            System.out.println("Total number of words: " + data.totalWords);
            System.out.println("Total number of characters: " + data.totalCharacters);
            System.out.println("Total number of newlines: " + data.totalNewLines);
        }
    }

    public static Data analyze(Scanner scan) {
        Data data = new Data();

        while (true) {
            String line = scan.nextLine();

            if (line.isEmpty())
                break;
            else {
                data.totalNewLines++;

                for (int i = 0; i < line.length(); i++)
                    if (!Character.isWhitespace(line.charAt(i)))
                        data.totalCharacters++;

                data.totalWords += line.split("\\s+").length;
            }
        }

        if (data.totalNewLines > 0)
            data.totalNewLines--;

        return data;
    }

    private static final class Data {

        private int totalCharacters;
        private int totalNewLines;
        private int totalWords;
    }
}
公共类计数{
公共静态void main(字符串…参数){
尝试(扫描仪扫描=新扫描仪(System.in)){
System.out.print(“请输入字符串(空行停止):”;
数据=分析(扫描);
System.out.println(“总字数:“+data.totalWords”);
System.out.println(“字符总数:“+data.totalCharacters”);
System.out.println(“换行总数:“+data.totalNewLines”);
}
}
公共静态数据分析(扫描仪扫描){
数据=新数据();
while(true){
String line=scan.nextLine();
if(line.isEmpty())
打破
否则{
data.totalNewLines++;
对于(int i=0;i0)
data.totalNewLines--;
返回数据;
}
私有静态最终类数据{
私有整数字符;
私人新线;
私人语言;
}
}
Jess, 这是我的解决办法。此外,我建议您学习更多java核心,并记住在提问之前尝试用谷歌搜索问题。祝你旅途好运:)

公共类计数{
公共静态void main(字符串[]args){
System.out.print(“请输入字符串:”);
扫描仪输入=新扫描仪(系统输入);
字符串text=in.nextLine();
int arr[]=分析(文本);
系统输出打印项次(arr);
System.out.println(“总字数:+arr[0]);
System.out.println(“字符总数:+arr[1]);
System.out.println(“换行总数:+arr[2]);
}
公共静态int[]分析(字符串文本){
int[]arr=新的int[3];
//数行
String[]line=text.split(“\r\n |\r |\n”);//取自https://stackoverflow.com/questions/454908/split-java-string-by-new-line
arr[2]=线长度;
//计算字数
arr[0]=countWords(文本);
//计数字符数
arr[1]=counteCharacters(文本);
返回arr;
}
静态最终输入输出=0;
静态最终整数IN=1;
静态int countWords(String str)//取自https://www.geeksforgeeks.org/count-words-in-a-given-string/
{
int状态=OUT;
int-wc=0;
int i=0;
而(i
public class Count {

    public static void main(String[] args) {
        System.out.print("Please enter a string: ");
        Scanner in = new Scanner(System.in);
        String text = in.nextLine();
        int arr[] = analyze(text);
        System.out.println(arr);
        System.out.println("Total number of words: " + arr[0]);
        System.out.println("Total number of characters: " + arr[1]);
        System.out.println("Total number of newlines: " + arr[2]);

    }

    public static int[] analyze(String text) {
        int[] arr = new int[3];

        // count lines
        String[] lines = text.split("\r\n|\r|\n"); // taken from https://stackoverflow.com/questions/454908/split-java-string-by-new-line
        arr[2] = lines.length;

        // count number of words
        arr[0] = countWords(text);

        // count number of characters
        arr[1] = counteCharacters(text);
        return arr;
    }

    static final int OUT = 0;
    static final int IN = 1;

    static int countWords(String str)  // taken from https://www.geeksforgeeks.org/count-words-in-a-given-string/
    {
        int state = OUT;
        int wc = 0;
        int i = 0;

        while (i < str.length()) {
            if (str.charAt(i) == ' ' || str.charAt(i) == '\n'
                    || str.charAt(i) == '\t')
                state = OUT;

            else if (state == OUT) {
                state = IN;
                ++wc;
            }
            ++i;
        }
        return wc;
    }

    static int count = 0; // taken from https://www.javatpoint.com/java-program-to-count-the-total-number-of-characters-in-a-string

    static int counteCharacters(String text) {
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) != ' ')
                count++;
        }
        return count;
    }
}