1

I want to print out the values in this array of sorts with each value being comma separated in java. However, I do not want my last value to have a comma after it.

for (double val : output.getPvalues()) 
{
    System.out.print(val + ",");
}

Right now, my output looks like this and I don't want the comma at the end.

0.3151985084814564,0.12439565272818136,0.08395574717762709,
0.23648887465370277,0.13987884822617208,0.1316174439685804,
0.23774083903133078,0.14471871035755607,0.16040529724318148,
0.23138041484992727,0.12692146250237307,0.10444029009575935,

Sorry it's my first day in java, and I don't know how to find the length of this object. How can I do it?

chiwangc
  • 3,566
  • 16
  • 26
  • 32
tubby
  • 2,074
  • 3
  • 33
  • 55
  • 1
    Well does `output.getPvalues()` actually return an array, or a list, or something else? – Jon Skeet May 01 '15 at 06:15
  • You give no information about the class of `output`, so... – Dici May 01 '15 at 06:16
  • It depends on the result you get from getPvalues. if its a list you can use .size() or .length for arrays – 0riginal May 01 '15 at 06:28
  • 1
    possible duplicate of [Java: convert List to a join()d string](http://stackoverflow.com/questions/1751844/java-convert-liststring-to-a-joind-string) – Tugrul Ates May 01 '15 at 08:24
  • 1
    Possible duplicate of [What's the best way to build a string of delimited items in Java?](http://stackoverflow.com/questions/63150/whats-the-best-way-to-build-a-string-of-delimited-items-in-java) – Raedwald Feb 23 '16 at 10:06

5 Answers5

3

If output.getPvalues() returns an array, you could use:

double[] values = output.getPvalues();
for (int i = 0; i < values.length(); i++) {
     System.out.print(values[i]);
     if (i != values.length - 1) {
         System.out.print(",");
     }
}

Or to make the condition simpler, you could just print a comma before all but the first item - at which point you can go back to an enhanced-for loop, because it's easy to tell if you're in the first iteration:

boolean firstIteration = true;
for (double value : output.getPvalues()) {
     if (firstIteration) {
         firstIteration = false;
     } else {
         System.out.print(",");
     }
     System.out.print(values[i]);
}

Alternatively, if you're using Java 8, you could use streams and Collectors.joining - but that's rather more advanced.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Change your code to below snippet:

StringBuffer stBuffValue = new StringBuffer();
for (double val : output.getPvalues()) 
{
    stBuffValue.append(val).append(",");
}
System.out.println(stBuffValue.substring(0, stBuffValue.length()-1));
1

First look at the doc to see if such a method exists whether in output or output.getPValues(). However, even without it you should not be blocked, because there are at least two ways to do it :

boolean init = true;
for (double val : output.getPvalues()) {
    if (init) init = false;
    else      System.out.print(",");
    System.out.print(val);
}

// or
StringBuilder sb = new StringBuilder();
for (double val : output.getPvalues()) {
    sb.append(val);
    sb.append(",");
}
System.out.println(sb.substring(0,sb.length()-1);
Dici
  • 25,226
  • 7
  • 41
  • 82
1

You can skip "," at last using "\b"

for (double val : output.getPvalues()) {  
System.out.print(val + ",");
}
System.out.print("\b\n");   // it will skip last , and add \n at last 
Kushal
  • 8,100
  • 9
  • 63
  • 82
  • Could you please [edit] your answer to give an explanation of why this code answers the question? Code-only answers are [discouraged](http://meta.stackexchange.com/questions/148272), because they don't teach the solution. – DavidPostill May 01 '15 at 08:19
-1
public static String trimFront(String text, char character) {
String normalizedText;
int index;

if (StringUtils.isNull(text)) {
  return text;
}

normalizedText = text.trim();
index = 0;

while (normalizedText.charAt(index) == character) {
  index++;
}
return normalizedText.substring(index).trim();}
Jaimin Soni
  • 1,061
  • 1
  • 13
  • 29
  • 1
    I definitely *wouldn't* start using regular expressions for this. That's way more complicated than it needs to be, violates Java naming conventions, and you haven't even shown how it fits in with the question. – Jon Skeet May 01 '15 at 07:07