0

Currently I'm pulling data out of SQL database table unfortunately one specific column contains more than one value (specifically an address). So I'm trying to .append this address data into a textArea of my program but I need to break the lines there so I figured I might try to tokenize it and .append(.nextToken + "\n") unless there is a better way to do it?

Here's some of my tryhard stuff

 try {
            rs = statement.executeQuery(query);

            if (rs.next()) {
                String addressLine = rs.getString("adres");
                StringTokenizer st = new StringTokenizer(addressLine);
                String token = st.nextToken();
                dbNameField.setText(rs.getString("name"));
                dbNazwiskoField.setText(rs.getString("surname"));
                dbAgeField.setText(""+rs.getInt("age"));
                rs.getInt("id");
                while(st.hasMoreTokens()){
                dbAddressField.append(token+ "\n");
                }

            }

except it doesn't seem to be working

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pat
  • 29
  • 1
  • 7

1 Answers1

0

StringTokenizer is a legacy class, so there is no problem in using it (as in docs), but it is not recommended.

A simpler solution recommended by JavaDoc is to use String split(String regex) method and iterate over the returned array using foreach cycle.

One more detail - do not use builder.append(token + "\n"), there is a cleaner aproach: builder.append(token).append(System.lineSeparator()).

Using \n is platform dependent as it doesn't work on unix-based systems.

Using String concatenation can also slow your program if the compiler doesn't optimize it correctly. Especially if you have a StringBuilder ready :)