This is my code to ask the database for all users which I save later to a txt-file line-by-line. Works good so far except for the 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? Have you debugged your code?replaceAllcall doesn't seem to be working, probably because the characters between the digits are not actually spaces. You should print each character ofsteamidas a hex number (perhaps using your debugger) just to check.|character is taken as a literal character because it's inside[ ], which matches any single character from the set of whatever is contained in the brackets. So you don't want to include|. But even[\\s\\u000A]doesn't make sense. That matches any "space" character OR the 0A (linefeed) character. But the latter belongs to the set of the former! So[\\s]is enough. Then the brackets become redundant: a simple\\swill do. (And you don't need\\s+withreplaceAllsince it replaces All.)