当前位置: 首页 > 知识库问答 >
问题:

jGrasp Java字符串到布尔值错误!“ClientId.java:20:错误:不兼容类型:字符串无法转换为布尔值”

贺宏富
2023-03-14
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\".   ");
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个答案

盖弘毅
2023-03-14

joptionpane.showInputDialog()返回包含用户输入的字符串,这意味着如果您想检查用户输入的是true还是false,您必须解析他们输入的内容,如下所示:

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

如果用户输入true,这将将布尔值设置为true,否则为false。

编辑:您还可以使用boolean.valueof(String s)

 类似资料: