我正在尝试创建一种从哈希表中检索用户输入的articleName 的authorID 的方法。以下是当用户按下按钮时在客户端激活的代码:
public String getAuthorID() // returns a String
{
try
{
articleName = txtArticleName.getText();
argAuthorID = new Vector();// create vector for the args
argAuthorID.addElement(articleName);// name to search for to get AuthorID
// make the call to the server
authorIDVector = (Integer)client.execute("GetSize.sendAuthorID", argAuthorID);
System.out.println(argAuthorID);
}
catch (XmlRpcException exception) {
System.err.println("JavaClient: XML-RPC Consumer Fault #" +
Integer.toString(exception.code) + ": " +
exception.getCause() + "" + exception.toString());
} catch (Exception exception) {
System.err.println("JavaClient: XML-RPC Consumer Fault #" + exception.toString());
}
String StrAuthorID = Integer.toString(authorID); // Cast AuthorID to String
return StrAuthorID;
}
这是服务器端的方法:
public int sendAuthorID(String articleNameRequest) {
// get info from the hashtable
aNumber = (Integer) theHashtable.getAuthorID(articleNameRequest); // was this.
return aNumber;
}
这是包含哈希表的类中的代码:
public int getAuthorID(String articleName)
{
int intfoundit;
String foundit = (String)hashtab.get(articleName);
System.out.print(foundit);
intfoundit = Integer.parseInt(foundit);
System.out.print(foundit);
System.out.print(intfoundit);
return intfoundit;
}
程序可以检索 AuthorID,但不会将其输入到文本框中。通过测试我发现异常是由以下代码引发的:
catch (XmlRpcException exception) {
System.err.println("JavaClient: XML-RPC Consumer Fault #" +
Integer.toString(exception.code) + ": " +
exception.getCause() + "" + exception.toString());
这是给出的错误:
'JavaClient: XML-RPC Consumer Fault #0: nullorg.apache.xmlrpc.XmlRpcException: java.lang.Exception: java.lang.NumberFormatException: For input string: " 3377"'
更新:删除了哈希表中 ID 号之前的空格,它不再抛出错误,但它仍然没有将 ID 号输入到文本框中,而只是输入“0”
最佳答案
当字符串中有空格时,它似乎会失败。正如我们在异常跟踪中看到的那样,parseInt
无法解析 "3377"
并且在执行时抛出了 NumberFormatException
:
intfoundit = Integer.parseInt(foundit);
因此,您可以尝试修剪
字符串,看看它是否可以解决您的问题:
intfoundit = Integer.parseInt(foundit.trim());
更好的是,您应该在哈希表中保存/放置键/值的位置进行修剪。
关于java - 不打印到文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18289595/