Skip to main content

Split and Replace methods

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
aboyahia1435
Offline
Joined: 2013-12-11

I have data in MySQl database i want to retrieve it by splitting spaces and replace it with comma, and after 6th point i want to go to new line,plus ignoring comma
458.41016 425.70843 427.74316 392.55343 403.93516 370.91243 399.48516 366.83843 398.54916 368.02743 397.41516 372.27043 394.75116 382.25643 392.96616 392.69543 391.09516 402.03043 390.35916 405.62343 389.79116 406.92443 392.62616 409.52743 406.00316 421.83343 442.19716 458.07143 444.89016 482.76843 431.76716 528.31343 393.39116 574.56743 350.22516 594.56743 316.63916 610.12643 278.88716 614.34043 242.18316 610.35243 232.12112 609.27843 228.38012 619.29143 238.47016 621.92243 274.01216 631.28543 320.32416 637.73643 356.57416 628.91043 420.03416 613.46343 456.48216 533.71643 457.61616 470.82943
i've tried this, How can i get it in proper way ?

int sum=0;
String values = null;
while (rs.next()) {

values = rs.getString(1);
String[] valueTokens = values.split("\\s");
for(int i=0;i System.out.print(valueTokens[i]);
System.out.print(",");
if( i % 6 == 0 ) {
System.out.println();
sum += i;}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tejesh
Offline
Joined: 2006-05-05

Hi aboyahia1435,

Below code snippet may help you,

String values = null;
while (rs.next()) {
values = rs.getString(1);

//Assume if the resultset retuns the below line as output.
//values = "458.41016 425.70843 427.74316 392.55343 403.93516 370.91243 399.48516 366.83843";
String subres = values.replace(" ", ",");
String[] strarr = values.split(" ");
StringBuilder sb = new StringBuilder();
for (int index = 0; index < strarr.length; index++) {
sb.append(strarr

);
if (index == 5) {
sb.append("\n");
} else {
sb.append(", ");
}
}
String res = sb.substring(0, sb.length() - 2);
System.out.println( res);
}