[英]Java parse String to Int
This is my code to ask the database for all users which I save later to a txt-file line-by-line. 这是我的代码,用于向数据库询问所有用户,然后我将它们逐行保存到txt文件中。 Works good so far except for the String steamid
. 到目前为止效果很好,除了String steamid
。
public String getAllUsers() {
Connection conn = null;
Statement stmt = null;
try {
Path out = Paths.get("C:\\Teamspeak\\alluserlist.txt");
List<String> arrayList = new ArrayList<String>();
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, dbUser, dbUserPW);
stmt = conn.createStatement();
String sql = "select nickname, unique_id, is_admin, steamid, is_banned from users where nickname not like 'Unknown from%'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String nickname = rs.getString("nickname");
String id = rs.getString("unique_id");
String admin = rs.getString("is_admin");
String steamid = String.valueOf(rs.getString(4));
String banned = rs.getString("is_banned");
int steamnr = Integer.parseInt(steamid.replaceAll("[\\s|\\u00A0]+", ""));
//System.out.println(nickname + ":" + id + ":" + admin + ":" + steamid);
if (!nickname.equals("")) {
if (Integer.valueOf(banned) == 1) {
nickname = "<s>" + nickname + "</s>";
id = "banned";
// <a href="somepage.html" target=newtab>text</a>
arrayList.add(nickname + ";" + id + ";" + admin + ";" + "<a href=\"http://steamcommunity.com/profiles/" + steamnr + "\"target=newtab>go to Steam</a>");
} else {
arrayList.add(nickname + ";" + id + ";" + admin + ";" + "<a href=\"http://steamcommunity.com/profiles/" + steamnr + "\"target=newtab>go to Steam</a>");
}
}
}
try {
Charset charset = Charset.forName("UTF-8");
Files.write(out,arrayList, charset);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return "error";
}
I tried to remove whitespaces using 我尝试使用删除空白
int steamnr = Integer.parseInt(steamid.replaceAll("[\\s|\\u00A0]+", ""));
but it shows no effect. 但没有效果。
What am I doing wrong? 我究竟做错了什么?
java.lang.NumberFormatException: For input string: " 7 6 5 6 1 1 9 8 0 5 7 5 9 7 5 3 1"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at database.DatabaseHandler.getAllUsers(DatabaseHandler.java:329)
at main.Main.main(Main.java:139)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
steamid is a varchar in database steamid是数据库中的varchar
Even if you remove all blanks (which you don't, as the error message shows), the number is too big to fit into an Integer
object. 即使删除了所有空格(错误消息所显示的也不删除),该数字仍然太大而无法容纳到Integer
对象中。 So you need to use a data type capable of storing larger values. 因此,您需要使用能够存储较大值的数据类型。
I would advice you to use the class BigInteger
to store such values, as there is likely no guarantee that it won't exceed the range of Long
as well. 我建议您使用BigInteger
类存储此类值,因为可能无法保证它也不会超出Long
的范围。
It seems that even if they are displayed as whitespaces, \\\\s
is not removing them, so they may be other characters. 似乎即使将它们显示为空格, \\\\s
也不会删除它们,因此它们可能是其他字符。
The way to still remove them regardless of them being whitespaces or not, is to remove all non-digits , by replacing this: 无论是否为空格,仍然要删除它们的方法是通过替换以下内容来删除所有非数字 :
steamid.replaceAll("[\\s|\\u00A0]+", "")
with this: 有了这个:
steamid.replaceAll("\\D+", "")
Also, that number is too big to fit in an int
, you should use a long
instead (or a BigInteger
, as others suggested): 另外,该数字太大而无法容纳int
,您应该改用long
(或其他建议的BigInteger
):
long steamnr = Long.parseLong(steamid.replaceAll("\\D+", ""));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.