2017-03-29 4 views
3

Ceci est mon code et affiche l'erreur à while.Index hors d'exception liée est coming.The sous-chaînes doivent être telles queComment sousChaîne une chaîne donnée avec des règles spécifiques en java

  1. Max 50 caractères par chaîne
  2. Aucun nombre ne doit être rompu - par exemple "4318, 4466, 486" est acceptable mais "4318, 4466, 48" n'est pas
  3. La sous-chaîne doit toujours commencer par un espace et se terminer par une virgule ", "

quelqu'un peut me aider avec cette s'il vous plaît

public class MyString { 

/** 
* @param args 

*/ 
static String input = " 4318, 4466, 486, 1698, 478, 590, 582, 1594, 486, 1690, 702, 378, 486, 586, 486, 1694, 486, 598, 538, 526, 486, 1694, 486, 622, 434, 630, 462, 1730, 422, 1754, 450, 586, 486, 1690, 498, 1682, 486, 586, 502, 606, 454, 1698, 478, 1694, 486, 1718, 458, 1686, 726, 1458, 646, 426, 486, 1694, 482, 1694, 486, 590, 482, 622, 490, 550, 490, 602, 450, 634, 466, 586, 486, 1730, 426, 1714, 494, 622, 446, 590, 494, 618, 450, 586, 490, 586, 486, 1730, 450, 586, 486, 590, 502, 1674, 530, 1650, 482, 1690, 558, 1622, 486, 1690, 486, 5298, 4306, 4466, 530, 1646, 486, 594, 650, 1502, 546, 1650, 490, 586, 486, 622, 522, 1622, 486, 602, 454, 634, 462, 1694, 482, 590, 486, 590, 486, 1694, 482, 1730, 450, 590, 498, 1638, 526, 1690, 498, 606, 458, 586, 486, 1698, 482, 1694, 482, 1690, 486, 1694, 486, 1698, 574, 494, 486, 1714, 462, 1690, 486, 626, 430, 610, 486, 590, 490, 622, 426, 610, 498, 618, 446, 1690, 486, 1734, 446, 590, 482, 590, 502, 606, 454, 590, 502, 582, 478, 1690, 486, 586, 506, 578, 482, 1686, 558, 1622, 674, 1506, 482, 1698, 590, 1606, 462, 1000"; 
char space=' '; 
static char comma=','; 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    int start=0, end=0; 
    while (end-1 < input.length()){ 

    start=end; 
    end= start+ 50; 

    String output= input.substring(start,end); 
    if(output.charAt(start)==' ' && output.charAt(end-1)==',') 
    { 
    System.out.println(output); 
    } 
    else 
    { 

    while(!(output.charAt(end-1)==',')) 
    { 

    end--; 

    } 
    output= input.substring(start,end); 
    System.out.println(output); 

    } 
    } 
    } 



} 
+0

si fin = 0 est la fin 1 = -1 (while (fin 1 Jens

Répondre

1

J'ai fait une légère modification dans votre code.

class Ideone { 

static String input = " 4318, 4466, 486, 1698, 478, 590, 582, 1594, 486, 1690, 702, 378, 486, 586, 486, 1694, 486, 598, 538, 526, 486, 1694, 486, 622, 434, 630, 462, 1730, 422, 1754, 450, 586, 486, 1690, 498, 1682, 486, 586, 502, 606, 454, 1698, 478, 1694, 486, 1718, 458, 1686, 726, 1458, 646, 426, 486, 1694, 482, 1694, 486, 590, 482, 622, 490, 550, 490, 602, 450, 634, 466, 586, 486, 1730, 426, 1714, 494, 622, 446, 590, 494, 618, 450, 586, 490, 586, 486, 1730, 450, 586, 486, 590, 502, 1674, 530, 1650, 482, 1690, 558, 1622, 486, 1690, 486, 5298, 4306, 4466, 530, 1646, 486, 594, 650, 1502, 546, 1650, 490, 586, 486, 622, 522, 1622, 486, 602, 454, 634, 462, 1694, 482, 590, 486, 590, 486, 1694, 482, 1730, 450, 590, 498, 1638, 526, 1690, 498, 606, 458, 586, 486, 1698, 482, 1694, 482, 1690, 486, 1694, 486, 1698, 574, 494, 486, 1714, 462, 1690, 486, 626, 430, 610, 486, 590, 490, 622, 426, 610, 498, 618, 446, 1690, 486, 1734, 446, 590, 482, 590, 502, 606, 454, 590, 502, 582, 478, 1690, 486, 586, 506, 578, 482, 1686, 558, 1622, 674, 1506, 482, 1698, 590, 1606, 462, 1000"; 
char space = ' '; 
static char comma = ','; 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    int start = 0, end = 0; 
    int temp = 0; 
    while (end < input.length()) { 

     start = end; 
     end = Math.min(start + 50, input.length()); 

     String output = input.substring(start, end - 1); 
     if (output.charAt(0) == ' ' && output.charAt(output.length() - 1) == ',') { 
      System.out.println(output); 
      end--; 
     } else { 
      temp = output.length() - 1; 
      while (temp!=-1 && output.charAt(temp) != ',') { 
       temp--; 
      } 
      if(temp==-1)break; 
      output = input.substring(start, start + temp + 1); 
      System.out.println(output); 
      end = start + temp + 1; 
     } 

    } 
} 
} 

Demo

+0

ce code est correct et il donne la sortie comme prévu que le dernier nombre, c'est-à-dire 1000 n'est pas imprimé.Pouvez-vous s'il vous plaît suggérer les changements nécessaires ? – riya

+0

oui, ce ne sera pas selon vos besoins, il devrait seulement imprimer * Substring devrait toujours commencer avec un espace et se terminer par une virgule "," * Et, le dernier n'a pas – Ravi

0

Je ne comprends pas la question, mais je pense que vous êtes à la recherche de cette

public class Snippet { 
    static String input = " 4318, 4466, 486, 1698, 478, 590, 582, 1594, 486,   1690, 702, 378, 486, 586, 486, 1694, 486, 598, 538, 526, 486, 1694, 486, 622, 434, 630, 462, 1730, 422, 1754, 450, 586, 486, 1690, 498, 1682, 486, 586, 502, 606, 454, 1698, 478, 1694, 486, 1718, 458, 1686, 726, 1458, 646, 426, 486, 1694, 482, 1694, 486, 590, 482, 622, 490, 550, 490, 602, 450, 634, 466, 586, 486, 1730, 426, 1714, 494, 622, 446, 590, 494, 618, 450, 586, 490, 586, 486, 1730, 450, 586, 486, 590, 502, 1674, 530, 1650, 482, 1690, 558, 1622, 486, 1690, 486, 5298, 4306, 4466, 530, 1646, 486, 594, 650, 1502, 546, 1650, 490, 586, 486, 622, 522, 1622, 486, 602, 454, 634, 462, 1694, 482, 590, 486, 590, 486, 1694, 482, 1730, 450, 590, 498, 1638, 526, 1690, 498, 606, 458, 586, 486, 1698, 482, 1694, 482, 1690, 486, 1694, 486, 1698, 574, 494, 486, 1714, 462, 1690, 486, 626, 430, 610, 486, 590, 490, 622, 426, 610, 498, 618, 446, 1690, 486, 1734, 446, 590, 482, 590, 502, 606, 454, 590, 502, 582, 478, 1690, 486, 586, 506, 578, 482, 
1686, 558, 1622, 674, 1506, 482, 1698, 590, 1606, 462, 1000"; 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     int start=0, end=0; 
     String[] a=input.split(","); 
     System.out.println(a); 
     for (int i = 0; i < a.length; i++) { 

      System.out.print(a[i]); 
      if(!(a.length-1==i)) 
      { 
      System.out.print(","); 
      } 
     } 
    } 
} 
+0

I veulent que les sous-chaînes contiennent chacune 50 caractères maximum et chaque sous-chaîne commence par un espace et se termine par une virgule. Et une sous-chaîne comme "4318,4466", fera mais pas "4318,44" – riya

0
while(!(output.charAt(end-1)==','))  
{  
end--;  
} 

changement à

while(!(output.charAt(end-1)==',')) 
    { 

    if(end>=1) 
    end--; 
    else 
    break; 

    } 

Vous n'obtiendrez pas d'erreur mais vous voudrez peut-être changer d'autre pa RT En outre, vous n'avez pas demandé spécifiquement, mais vérifiez ce lien.

How to split a string in Java

0
public class StringSplitTest 
{ 
    static String input = " 4318, 4466, 486, 1698, 478, 590, 582, 1594, 486, 1690, 702, 378, 486, 586, 486, 1694, 486, 598, 538, 526, 486, 1694, 486, 622, 434, 630, 462, 1730, 422, 1754, 450, 586, 486, 1690, 498, 1682, 486, 586, 502, 606, 454, 1698, 478, 1694, 486, 1718, 458, 1686, 726, 1458, 646, 426, 486, 1694, 482, 1694, 486, 590, 482, 622, 490, 550, 490, 602, 450, 634, 466, 586, 486, 1730, 426, 1714, 494, 622, 446, 590, 494, 618, 450, 586, 490, 586, 486, 1730, 450, 586, 486, 590, 502, 1674, 530, 1650, 482, 1690, 558, 1622, 486, 1690, 486, 5298, 4306, 4466, 530, 1646, 486, 594, 650, 1502, 546, 1650, 490, 586, 486, 622, 522, 1622, 486, 602, 454, 634, 462, 1694, 482, 590, 486, 590, 486, 1694, 482, 1730, 450, 590, 498, 1638, 526, 1690, 498, 606, 458, 586, 486, 1698, 482, 1694, 482, 1690, 486, 1694, 486, 1698, 574, 494, 486, 1714, 462, 1690, 486, 626, 430, 610, 486, 590, 490, 622, 426, 610, 498, 618, 446, 1690, 486, 1734, 446, 590, 482, 590, 502, 606, 454, 590, 502, 582, 478, 1690, 486, 586, 506, 578, 482, 1686, 558, 1622, 674, 1506, 482, 1698, 590, 1606, 462, 1000,"; 
    static ArrayList chunks = new ArrayList(); 
    static char comma = ','; 
    static String commaString = ","; 
    static boolean more = true; 
    static int startIndex = 0; 
    static String str = null; 
    static int splitIndex = 50; 
    static int remainingInputLength = 0; 

    public static void main(String[] args) 
    { 
     while (more) 
     { 
      str = input.substring(startIndex, startIndex + splitIndex); 
      if (str.endsWith(commaString)) 
      { 
       startIndex += 50; 
      } 
      else 
      { 
       splitIndex = str.lastIndexOf(comma) + 1; 
       str = input.substring(startIndex, startIndex + splitIndex); 
       startIndex += splitIndex; 
      } 
      chunks.add(str); 

      remainingInputLength = input.substring(startIndex).length(); 
      if (remainingInputLength == 0) 
      { 
       more = false; 
      } 
      else 
      { 
       if (remainingInputLength >= 50) 
        splitIndex = 50; 
       else 
        splitIndex = remainingInputLength; 
      } 
     } 
     for (String s: chunks) System.out.println(s); 
    } 
}