jGrasp Java String to Boolean错误! “ClientID.java:20:错误:不兼容的类型:字符串无法转换为布尔值”

时间:2015-01-03 08:41:10

标签: java jgrasp

我有这行代码,我遇到了问题。

name = (JOptionPane.showInputDialog("What is the name of the new Client? \n (Format) \" Jake Michael Drewberg\"  "));
nameCheck= JOptionPane.showInputDialog("Please check that this is correct: "+name+". \n If this is correct please enter \"true\". if not enter \"false\".   ");

我得到了不兼容类型的错误,因为我在nameCheck行中有一个布尔值,但我只想在“名称”中输入的名称出现在nameCheck的文本中让用户检查他们输入的内容实际上是否正确。 但我不知道如何在布尔对话框窗口中加入名称enter,以便在名称正确时键入true,如果不正确则键入false,然后将其带到行

if (nameCheck==false)
     {
        name = JOptionPanel.showInputDialog("Please enter the full name of the Client with the first letter of each name in Capital letters. ");  
        System.out.println("This is the final name being saved to our files, if any further issue present themselves please reffer to our Admin staff at Admin_Med@clinic.ae; \n Client Name: "+name); 
     }

这样他们就可以重新输入他们想要保存的名字,如果他们在开头犯了错误。

但我仍然遇到“错误:不兼容的类型:字符串无法转换为布尔值”的问题

1 个答案:

答案 0 :(得分:0)

JOptionPane.showInputDialog() 会返回一个包含用户输入的字符串,这意味着如果您要检查用户输入的是true还是false,那么您必须解析什么他们进入了,就像这样:

boolean b = nameCheck.equalsIgnoreCase("true");

如果用户输入true,则将boolean设置为true,否则设置为false。

编辑:您还可以使用 Boolean.valueOf(String s)