0

I have a JList model (listModelGrid) with items with labels like this:

LastName, FirstName Spouse // e.g. This is 1st list item with labels
Children                   // e.g. This is 2nd list item with labels
Street                     // e.g. This is 3rd list item with labels
City, State Postal         // e.g. This is 4th list item with labels

enter image description here

I want to replace Labels with ResultSet.getString method like this:

String labels = "";
labels += resultSet.getString("LastName")+", "+resultSet.getString("FirstName")+" "+
          resultSet.getString("Spouse") + "\n";
labels += resultSet.getString("Children") + "\n";
labels += resultSet.getString("Street") + "\n";
labels += resultSet.getString("City")+", "+resultSet.getString("State")+" "+
          resultSet.getString("Postal");

I have tried it but stuck in loops:

private String getPrintingLabels(ResultSet rs) throws SQLException {
    String str = "";
    for (int i = 0; i < listModelGrid.getSize(); i++) {
        String element = String.valueOf(listModelGrid.getElementAt(i));

        String[] lbls = element.split(",\\s");

        str += rs.getString(lbls[0])+", ";
        for(int j = 1; j < lbls.length ; j++) {
            // Stuck on her
        }

        String[] lbls2 = element.split("\\s");

        str += rs.getString(lbls2[0])+" ";
        for(int j = 1; j < lbls2.length ; j++) {
            // Stuck on her
        }
    }
    return str;
}

Thanks in advance!

1 Answers1

0

The code in your method is written in a little complicated way.

I have used regex and simplified the code you wanted to write and here it is.

private String getPrintingLabels(ResultSet rs) throws SQLException {
        Pattern p = Pattern.compile("([a-zA-Z]+)(,? )?");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < listModelGrid.getSize(); i++) {
            String element = String.valueOf(listModelGrid.getElementAt(i));

            Matcher m = p.matcher(element);
            while(m.find()) {
                sb.append(rs.getString(m.group(1)));
                if (m.group(2) != null) {
                    sb.append(m.group(2));
                }
            }
            sb.append(System.getProperty("line.separator")); // helps correctly insert a new line for any platform linux/windows/any
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

I don't know if you are familiar with regex but by using regex your job is done quite easily. Also, using String concatenation is not a good idea specially when you have to do it quite much hence I have used StringBuild for same purpose. Also used line.separator property so no matter in what platform you run the code it will have appropriate line in your string. Just use my method instead of yours and see if the desired string the one you wanted.

Also in my code, you won't have to manually manage inserting ", " or " " as that is done automatically as it is present in the string.

Also make sure you import these two or any needed imports,

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • The main problem is how to insert ", " and " " between labels strings. how I can add this? –  Sep 30 '18 at 07:33
  • @AsifAli72090: My code already does this by regex matches. Try it out and you will see it as you wanted. I ran the code and here is the output which is exactly like how you wanted. Raj, Pushpesh Parul Anaya A203 New Delhi, Delhi 110075 – Pushpesh Kumar Rajwanshi Sep 30 '18 at 07:35
  • 1
    My pleasure :) I'll suggest you to explore regex here, https://www.regular-expressions.info/ Very useful when you are dealing with parsing/processing text. – Pushpesh Kumar Rajwanshi Sep 30 '18 at 07:44