如何解决不兼容的类型错误?

时间:2019-10-20 03:35:58

标签: java arrays

我编写的程序给我一个错误,我不知道如何解决。

我尝试了几种不同的方法,但是它们没有起作用。

public static boolean isVowel(char c) {
    c = Character.toLowerCase(c);   
    return c == "a" || c == "e" || c == "i" || c == "o" || c == "u";
}

我希望我的程序正常运行,目标是使代码返回字符串是否为元音。

3 个答案:

答案 0 :(得分:0)

您应该将字符与字符文字而不是字符串文字进行比较:

public static boolean isVowel(char c) {
    c = Character.toLowerCase(c);
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

您可以尝试使用一组保存所有可能的可接受输入值的代码来简化上述代码:

public static boolean isVowel(char c) {
    Set<Character> chars = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
    return chars.contains(Character.toLowerCase(c));
}

答案 1 :(得分:0)

因此,当您说Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:748) Caused by: javafx.fxml.LoadException: file:/home/Projects/2019.08.03_C482/InventorySystem/dist/run1962514670/InventorySystem.jar!/ViewController/MainView.fxml at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) at c482.C482.start(C482.java:23) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication.lambda$null$10(GtkApplication.java:245) ... 1 more Caused by: java.lang.NullPointerException at ViewController.MainViewController.initialize(MainViewController.java:142) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) ... 17 more Exception running application c482.C482 Java Result: 1 时,实际上是在尝试比较参数,字符和字符串“ a”,等等。

对于其余条件,尝试将这些行更改为c == "a"(用单引号表示字符),依此类推。

答案 2 :(得分:0)

cchar"a"StringcharString是不兼容的类型。

return c == "a" || c == "e" || c == "i" || c == "o" || c == "u";

c == "a"您比较charString的值。

要解决此问题,我将其更改为

return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';以将c与字符进行比较。