卸下支架和空字符串

时间:2017-10-12 14:59:56

标签: java

代码的最终结果是能够将字符串数组解析为整数数组,但正如您所看到的,我得到了一个异常。我试图拆分[和]符号但是当我调用数组的第一个(0)元素splited_game_result [0]时,它什么都不返回。这里有什么解决方案?我尝试使用trim方法。

    String[] ca_po_pe = {"4:7","PAUSE","2:0","1:3 PINE","PAUSE","CANCEL","2:4","0:5 PINE","PAUSE","CANCEL"}; 

    String canc_postp_pen_games = ""; 

   boolean confirming = true;

   for(String looped_games : ca_po_pe) { 

   if(confirming) { canc_postp_pen_games+=looped_games; confirming=false; } 

    else { canc_postp_pen_games+=", "+looped_games; } }

   System.out.println("LOOPED FINAL RESULT GAMES TO INSERT COMMAS: " + "\n" + canc_postp_pen_games + "\n"); 

    String[] fin_games_res = canc_postp_pen_games.split("[,]");

    ArrayList<String> arraylist= new ArrayList<String>();

    Collections.addAll(arraylist, fin_games_res);

    for (String str: arraylist) {     
      }

    System.out.println("ELEMENTS ADDED TO ARRAYLIST: " + arraylist + "\n");

int noItems = arraylist.size(); 

for (int i = 0; i < noItems; i++) { 

String currItem = arraylist.get(i); 

if (currItem.contains("PAUSE")) { 
arraylist.add(new String("400"));
noItems++; } 

if (currItem.contains("CANCEL")) { 
arraylist.add(new String("300")); 
noItems++; } 

if (currItem.contains(" PINE")) { 
arraylist.add(new String("500")); 
noItems++; }
    }

   System.out.println("ELEMENTS BEFORE REMOVAL: " + "\n" + arraylist + "\n");

 Iterator<String> iter_getting = arraylist.iterator(); 

 while(iter_getting.hasNext()) { 

 if(iter_getting.next().contains("PAUSE")){ 
   iter_getting.remove(); }}

   Iterator<String> iter_getting1 = arraylist.iterator(); 

   while(iter_getting1.hasNext()) { 

 if(iter_getting1.next().contains("CANCEL")){ 
   iter_getting1.remove(); }}

   Iterator<String> iter_getting2 = arraylist.iterator(); 

   while(iter_getting2.hasNext()) { 

 if(iter_getting2.next().contains(" PINE")){ 
   iter_getting2.remove(); }}

  System.out.println("ELEMENTS AFTER REMOVAL: " + "\n" + arraylist);

    System.out.println("ELEMENT ON INDEX 3 BEFORE CONVERTION TO ARRAY: " + "\n" + arraylist.get(3));

    String convert = arraylist.toString();

    System.out.println("CONVERTED STRING: " + convert);

    String[]splited_game_result = convert.trim().split("[\\[:,\\]]");

    System.out.println(" AFTER SPLITING TO ARRAY: " + "\n" + splited_game_result[0]);

    Integer[] final_result = new Integer[splited_game_result.length];

    int g = 0; 

    for(String fsgr : splited_game_result)

{final_result [g] = Integer.parseInt(fsgr);克++;}

    System.out.println("INTEGER ELEMENT ON INDEX 0: " + "\n" + final_result[1]);     

2 个答案:

答案 0 :(得分:0)

我仍然不知道你要做什么,但这是你的例外。您正在解析空字符串并尝试在数字前解析空格。把评论放在你的逻辑中。

for (String fsgr : splited_game_result) {
    try {
        final_result[g] = Integer.parseInt(fsgr);
        g++;
    } catch(Exception e2) {
       System.out.println("Exception = ["+ fsgr + "]");
    }
}

输出(在put中间。使用第一个和最后一个printlns作为位置)

CONVERTED STRING: [4:7,  2:0,  2:4, 400, 500, 400, 300, 500, 400, 300]
AFTER SPLITING TO ARRAY: 

Exception = []
Exception = [  2]
Exception = [  2]
Exception = [ 400]
Exception = [ 500]
Exception = [ 400]
Exception = [ 300]
Exception = [ 500]
Exception = [ 400]
Exception = [ 300]
INTEGER ELEMENT ON INDEX 0: 
7

答案 1 :(得分:0)

对于每个有奇迹的人来说,在源头的最后提出异常。它说NumberFormatException: For input string: ""。见下面的来源:

for(String fsgr : splited_game_result)
{ final_result[g] = Integer.parseInt(fsgr); g++;}

原因在于用于分割数组的常规尝试作者:

String[]splited_game_result = convert.trim().split("[\\[:,\\]]");

它产生以下字符串数组:
["", "4", "7", " 2", "0", " 2", "4", " 400", " 500", " 400", " 300", " 500", " 400", " 300"]

问题是Integer.parseInt需要string that contain parsable integer

可能的解决方法是添加if condition以确保您的fsgr不是空字符串且不包含空格:

for(String fsgr : splited_game_result) {

    String fsgrTrim = fsgr.trim();

    if (!fsgrTrim.isEmpty()) {
        final_result[g] = Integer.parseInt(fsgrTrim);
        g++;
    }
}

或者添加try-catch子句:

for(String fsgr : splited_game_result) {
    try {
        final_result[g] = Integer.parseInt(fsgr);
        g++;
    } catch (Exception e) {
        e.printStackTrace();
    }
}