[英]Java parse String to Int
這是我的代碼,用於向數據庫詢問所有用戶,然后我將它們逐行保存到txt文件中。 到目前為止效果很好,除了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";
}
我嘗試使用刪除空白
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
似乎您的數字對於Integer來說太大了。 嘗試將其解析為Long
String steamid = " 7 6 5 6 1 1 9 8 0 5 7 5 9 7 5 3 1";
Long steamnr = Long.parseLong(steamid.replaceAll("[\\s|\\u00A0]+", ""));
System.out.println(steamnr);
即使刪除了所有空格(錯誤消息所顯示的也不刪除),該數字仍然太大而無法容納到Integer
對象中。 因此,您需要使用能夠存儲較大值的數據類型。
我建議您使用BigInteger
類存儲此類值,因為可能無法保證它也不會超出Long
的范圍。
似乎即使將它們顯示為空格, \\\\s
也不會刪除它們,因此它們可能是其他字符。
無論是否為空格,仍然要刪除它們的方法是通過替換以下內容來刪除所有非數字 :
steamid.replaceAll("[\\s|\\u00A0]+", "")
有了這個:
steamid.replaceAll("\\D+", "")
另外,該數字太大而無法容納int
,您應該改用long
(或其他建議的BigInteger
):
long steamnr = Long.parseLong(steamid.replaceAll("\\D+", ""));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.