- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道“如何从字符串转换为字符?”已被问过很多次,但我不知道为什么这对我的项目来说是一个问题,因为我不认为我正在尝试从字符转换为字符串。
这是我的代码:
private void BtnAddClientActionPerformed(java.awt.event.ActionEvent evt) {
try {
ArrayList<Cliente> clientes = ClienteDataSer.LoadData();
Cliente C = new Cliente();
JOptionPane.showMessageDialog(null, "reading: dni");
C.setDni(Integer.parseInt(TxTDni.getText()));
JOptionPane.showMessageDialog(null, "reading: nombre");
C.setNombre(TxTNombre.getText());
JOptionPane.showMessageDialog(null, "reading: apellido");
C.setApellido(TxTApellido.getText());
JOptionPane.showMessageDialog(null, "reading: sexo");
C.setSexo(TxTSexo.getText()); //<--- error: "Incopatible types:String cannot be converted to char
JOptionPane.showMessageDialog(null, "reading: edad");
C.setEdad(Integer.parseInt(TxTEdad.getText()));
JOptionPane.showMessageDialog(null, "Se añade a la tabla clientes");
clientes.add(C);
JOptionPane.showMessageDialog(null, "Se guarda");
ClienteDataSer.saveData(clientes);
JOptionPane.showMessageDialog(null, "Se corre el metodo llenartablaclientes");
llenarTablaClientes();
JOptionPane.showMessageDialog(null, "Hecho");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Faltan campos por llenar");
}
}
(多个消息对话框只是调试语句,以查看程序在哪里失败。)
这里是 TxTSexo.getText()
类的摘录,来自:
public char getSexo() {
return sexo;
}
public void setSexo(char sexo) {
if (this.sexo == 'M'|| this.sexo == 'm'|| this.sexo == 'f'|| this.sexo == 'F')
this.sexo = sexo;
}
我已经尝试转换为字符串,但它再次显示错误。
如果有人想阅读整个项目,这里是 link ,也许你还可以用它做其他事情。
最佳答案
相关类的 sexo
属性的类型为 char
。 TxTSexo.getText()
返回一个String
。它们不是同一件事,它们之间没有自动转换。您需要自己执行合适的转换,才能使用 TxTSexo.getText()
设置 sexo
属性的值。
如果您可以依靠 TxTSexo.getText()
始终返回包含一个字符或包含所需字符作为第一个字符的非 null
字符串,则您可以简单地使用C.setSexo(TxTSexo.getText().charAt(0))
。
如果您需要容纳 TxTSexo.getText()
为 null
或为空或包含前导空格,那么您可能会这样做
char sexoChar;
try {
sexoChar = TxTSexo.getText().trim().charAt(0);
} catch (NullPointerException | IndexOutOfBoundsException e) {
sexoChar = ' ';
}
C.setSexo(sexoChar);
您可能想要执行进一步的验证或转换。
关于Javaform : how to use a char in this case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38234240/
我知道“如何从字符串转换为字符?”已被问过很多次,但我不知道为什么这对我的项目来说是一个问题,因为我不认为我正在尝试从字符转换为字符串。 这是我的代码: private void BtnAddClie
所以,我想知道如何在我的 JavaForm 中实现世界上所有国家/地区的列表。 由于我不想坐下来写出世界上所有的国家/地区,因此我想实现一个外部列表,但我找不到任何地方可以真正找到如何实现下拉列表像这
我正在尝试在 Play 框架中实现 Java 表单。我正在按照文档中的方式执行所有操作,但出现错误。 https://www.playframework.com/documentation/2.5.x
我一直在看剧本! 2.1 示例按照 ZenTasks 示例设置基本登录系统。我被卡住的地方是 JavaForms 部分。我想使用通过 Guice DI 提供的身份验证服务实例来验证登录请求。 我正在关
我是一名优秀的程序员,十分优秀!