Java解析String到Int

时间:2018-02-07 14:41:13

标签: java

这是我的代码,向数据库询问我之后保存到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

3 个答案:

答案 0 :(得分:1)

好像您的号码对于整数来说太大了。尝试将其解析为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);

答案 1 :(得分:1)

即使您删除了所有空白(您没有,如错误消息所示),该数字太大而无法放入Integer对象中。因此,您需要使用能够存储更大值的数据类型。

我建议您使用课程BigInteger来存储此类值,因为可能无法保证它不会超出Long的范围。

答案 2 :(得分:1)

似乎即使它们显示为空格,\\s也不会删除它们,因此它们可能是其他字符。

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

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

用这个:

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

此外,这个数字太大而无法容纳int,您应该使用long代替(或BigInteger,正如其他人所建议的那样):

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

Demo here