Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert a string like 2,4,5,6,7,8,10,12,14,15,16 to 2,4-8,10,12,14-16
    text
    copied!<p>I want to convert a string input like 2,3,6,7,8,10,12,14,15,16 to 2-3,6-8,10,12,14-16 using java</p> <p>I tried using the below code</p> <pre><code> Vector ar=new Vector(); int lastadded=0; String ht=""; String [] strarray=str.split(","); strarray=sortArray(strarray); Vector intarray=new Vector(); for(int i=0;i&lt;strarray.length;i++) { int temp=1; for(int j=1;j&lt;=intarray.size();j++) { if(Integer.parseInt(strarray[i])==Integer.parseInt(intarray.get(j-1).toString())) { temp=0; } } if(temp==1) { intarray.add(Integer.parseInt(strarray[i])); ar.add(Integer.parseInt(strarray[i])); } } ht=""; String strdemo=""; for(int i=0;i&lt;intarray.size();i++) { if(ht=="") { ht=ar.get(i)+""; lastadded=i; } else { strdemo=(String)ht; if(strdemo.length()==ar.get(0).toString().length()) { if(Integer.parseInt(strdemo.substring(0))==Integer.parseInt(ar.get(i).toString())-1) { strdemo=strdemo+"-"+ar.get(i); lastadded=Integer.parseInt(ar.get(i).toString()); ht=strdemo; } else { strdemo=strdemo+","+ar.get(i); lastadded=Integer.parseInt(ar.get(i).toString()); ht=strdemo; } } else if(strdemo.length()==3) { strdemo=(String)ht; if(Integer.parseInt(strdemo.substring(strdemo.length()-1,strdemo.length()))==Integer.parseInt(ar.get(i).toString())-1) { strdemo=strdemo.substring(0,strdemo.length()-2)+"-"+Integer.parseInt(ar.get(i).toString()); lastadded=Integer.parseInt(ar.get(i).toString()); ht=strdemo; } else { strdemo=strdemo+","+Integer.parseInt(ar.get(i).toString()); lastadded=Integer.parseInt(ar.get(i).toString()); ht=strdemo; } }//Else IF else{ strdemo=(String)ht; int de=1; int ddd=lastadded; if(ddd==Integer.parseInt(ar.get(i).toString())-1) { int lastaddedlen=(lastadded+"").length(); String symbol=strdemo.substring(strdemo.length()-lastaddedlen-1,strdemo.length()-lastaddedlen); if(symbol.equalsIgnoreCase("-")) strdemo=strdemo.substring(0,strdemo.length()-lastaddedlen-1)+"-"+Integer.parseInt(ar.get(i).toString()); else strdemo=strdemo+"-"+Integer.parseInt(ar.get(i).toString()); lastadded=Integer.parseInt(ar.get(i).toString()); ht=strdemo; } else { strdemo=strdemo+","+Integer.parseInt(ar.get(i).toString()); lastadded=Integer.parseInt(ar.get(i).toString()); ht=strdemo; } } } } </code></pre> <p><strong>Here <code>sortArray</code> function sorts the array descending and returns</strong></p> <pre><code>protected static String[] sortArray(String ss[]) { String temp; for(int i=0;i&lt;ss.length;i++) { for(int j=0;j&lt;ss.length;j++) { if(Integer.parseInt(ss[i])&lt;Integer.parseInt(ss[j])) { temp=ss[i]; ss[i]=ss[j]; ss[j]=temp; } } } return ss; } </code></pre> <p>I am not getting consistant results for some inputs for example for the below case <code>2,3,6,7,8,10,12,14,15,16</code> it gives <code>2-3,6-8,10,12,14-16</code> (<strong>which is correct</strong>) while for <code>2,4,5,6,7,8,10,12,14,15,16</code> it gives <code>2-8,10,12,14-16 (which actually should have been</code> <strong>2,4-8,10,12,14-16</strong><code>)</code></p> <p>Where does the code go inconsistent is what I need to find out..</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload