gpt4 book ai didi

Java解析String到Int

转载 作者:行者123 更新时间:2023-12-01 19:58:35 26 4
gpt4 key购买 nike

这是我的代码,用于向数据库询问所有用户,稍后我将其逐行保存到 txt 文件中。到目前为止,除了字符串 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";
}

我尝试使用删除空格

int steamnr = Integer.parseInt(steamid.replaceAll("[\\s|\\u00A0]+", ""));

但没有显示任何效果。

我做错了什么?

 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 是数据库中的 varchar

dbscreen

最佳答案

看来即使它们显示为空格,\\s也没有删除它们,因此它们可能是其他字符。

无论它们是否是空格,仍然删除它们的方法是删除所有非数字,通过替换:

steamid.replaceAll("[\\s|\\u00A0]+", "")

这样:

steamid.replaceAll("\\D+", "")

此外,该数字太大,无法容纳 int,您应该使用 long 代替(或使用 BigInteger,如其他建议):

long steamnr = Long.parseLong(steamid.replaceAll("\\D+", ""));

Demo here

关于Java解析String到Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48666501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com