java - 不兼容的类型 : void cannot be converted to string

标签 java string swing error-handling void

这个问题在这里已经有了答案:





What does "Incompatible types: void cannot be converted to ..." mean?

(1 个回答)


5年前关闭。




各位程序员,大家好,我需要一些关于学校作业文本的帮助。

使用此文本,我收到错误消息:无法编译的源代码 - 不兼容的类型:void 无法转换为 java.lang.String

我的意图是程序将获取一个句子,然后计算元音和辅音的数量并在对话窗口中显示它们。

我对编程很陌生,所以答案对你们所有人来说可能都很明显,但非常感谢您的帮助:D

所以要清楚,我的问题如下。如何修复 void to string 错误?

public static void main (String args[]) {

    String string1= JOptionPane.showInputDialog("Write a sentence.");
    int count = 0;
    int vowels = 0;
    int consonants = 0;

    for (int i = 0; i <string1.charAt(0); i++) {
            char ch = string1.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' || 
                            ch == 'o' || ch == 'u')
            {
                    vowels++;
            }
            else
            { 
                    consonants++;
            }
           String finished= JOptionPane.showMessageDialog(null,"You have written " +vowels+ "vowels and you have written " +consonants+ "consonants");
    }
}

最佳答案

如果你使用任何 IDE,这条线会向你提示

String finished= JOptionPane.showMessageDialog

因为showMessageDialog不返回任何东西,它是一个 void 方法,所以没有完成的 String 可以从中获取,它只是显示一个弹出窗口

关于java - 不兼容的类型 : void cannot be converted to string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37528860/

相关文章:

java - 通过在 ArrayList 中以逗号分割来存储字符串

java - 获取固定大小的 GridLayout

java - 哪里需要调用方法 "super.paint()"

java - doClick() 和西蒙 : All buttons will unpress at the same time instead of individually

Java - 使用 Hibernate (HBM) 在数据库中引用 Enum(数字值)

java - 在没有任何中间文件的情况下在 Java 中创建 GraphViz 图

java - 如何将 Boost::make_recursive_variant 对象转换为字符串?

Java程序设计题

java - JFreeChart 箱线图外观

c - 将文件中的数字输入到 char 数组中