“#39;'无法解决#39;'我该如何解决这个错误?

时间:2014-07-17 12:46:08

标签: java string

我是一名对Java缺乏了解的初学者。我使用Dr.Java,我收到了这个错误:

     "S cannot be resolved"

使用这个Java程序:

import java.util.Scanner;

public class HDtest5

{
    public static int countWords(String str) {
        String words[] = str.split(" ");
        int count = words.length;
        return count;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String sentence = in.nextLine();
        System.out.print("Your sentence has " + countWords(sentence) + " words.");
    }

    {
        while (true) // have created infinite loop
        {

            if (s.equals("quit"))
            // if enterd value is "quit" than it comes out of loop

            {

                String[] words = s.split(" "); // get the individual words
                System.out.println("#words = " + words.length);
                for (int i = 0; i < words.length; i++)
                    System.out.println(s + "word[" + i + words[i] + " nchars = " + words[i].length());

            }
            System.out.println(s); // to Print string
            System.out.println(s.length()); // to print Entered string's length

        }
    }
}

导致此错误消息的原因是什么? 我认为所有的字符串都叫做&#39; s&#39;应该没有任何问题地一起工作。你能帮我找一下它有什么问题吗?非常感谢您的帮助,非常感谢!

聚苯乙烯。为了更清楚,我的程序的目的是输出字符总数,每个单词的字符和在文本窗口(扫描仪)中输入的文本的单词数。代码应该完美地工作,除了这个&#39;字符串问题,使得代码块不能一起工作。

5 个答案:

答案 0 :(得分:1)

在Java中,您必须先在之前声明一个变量,然后才能使用它。

缺少一行代码:

String s;
顺便说一下,你可能想要使用sentence代替s,正如其他人注意到的那样。可能的值"quit"将位于sentence变量中。


通过声明变量

String personName;

你说编译器:

  

从现在开始,将personName理解为能够存储String的内存。

编译器将为您完成一项伟大的工作 - 它不会让您分配给personName任何其他内容(例如int),如果您输入错字并在某处写personMame ,你会立即找到它,而不是在运行时很晚,你不知道为什么你的代码不起作用,就像在JavaScript中一样。

答案 1 :(得分:1)

你还没有在代码中声明s。在String s=null;方法的开头写上这样的内容,main(),然后在代码后面给它一些值

答案 2 :(得分:1)

你得到这个错误,因为你没有声明任何变量S但你在if条件下使用它。你也错误地放置了{}大括号。 将主要方法改为:

注意:你的编程在infinte循环中运行。

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a sentence: ");
    String s = in.nextLine();
    System.out.print("Your sentence has " + countWords(s) + " words.");

    while (true) // have created infinite loop
    {

        if (s.equals("quit"))
        // if enterd value is "quit" than it comes out of loop

        {

            String[] words = s.split(" "); // get the individual words
            System.out.println("#words = " + words.length);
            for (int i = 0; i < words.length; i++)
                System.out.println(s + "word[" + i + words[i]
                        + " nchars = " + words[i].length());

        }
        System.out.println(s); // to Print string
        System.out.println(s.length()); // to print Entered string's length

    }
}

答案 3 :(得分:1)

我认为您想要使用sentence变量。 并且您不会为while循环的每次迭代读取变量。

也许你想要更多这样的东西。

public class HDtest5 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) { // have created infinite loop
            System.out.print("Enter a sentence: ");
            String sentence = in.nextLine();
            System.out.print("Your sentence has " + countWords(sentence) + " words.");

            if (sentence.equals("quit")) { // if enterd value is "quit" than it comes out of loop
                break;
            } else {
                String[] words = sentence.split(" "); // get the individual words
                System.out.println("#words = " + words.length);
                for (int i = 0; i < words.length; i++)
                    System.out.println(sentence + "word[" + i + words[i] + " nchars = " + words[i].length());

            }
            System.out.println(sentence); // to Print string
            System.out.println(sentence.length()); // to print Entered string's length

        }
        in.close();
    }

    private static int countWords(String str) {
        String words[] = str.split(" ");
        int count = words.length;
        return count;
    }
}

答案 4 :(得分:1)

我想我想出了你想要实现的目标并修复了你的代码:

import java.util.Scanner;

public class HDtest5

{
    public static int countWords(String str) {
        String words[] = str.split(" ");
        int count = words.length;
        return count;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (true) // have created infinite loop
        {
            System.out.print("Enter a sentence: ");
            String sentence = in.nextLine();
            if (sentence.equals("quit")) {
                // if enterd value is "quit" than it comes out of loop
                break;
            }
            System.out.print("Your sentence has " + countWords(sentence) + " words.");

            String[] words = sentence.split(" "); // get the individual words
            System.out.println("#words = " + words.length);
            for (int i = 0; i < words.length; i++)
                System.out.println("word[" + i + "] = " + words[i] + " nchars = " + words[i].length());
            }
            System.out.println(sentence); // to Print string
            System.out.println(sentence.length()); // to print Entered string's length
        }
        in.close();
    }
}