2016-04-08 5 views
0

入力文字列を検証し、それをint配列として出力する次のメソッドを記述しました。このメソッドは必要なだけ完全に動作しますが、入力に整数とカンマしか使えないように、エラーがないようにいくつかの検証を追加したいと思います。Java:文字列でintとカンマのみを許可する方法は?

例正しい入力は次のようになります。

"7,23,62,8,1130" 

方法は次のとおりです。

public static int[] validator (String [] check) { 

    int [] out = new int[5]; 

    try 
    { 
     if (0 < Integer.parseInt(check[0]) && Integer.parseInt(check[0]) < 100) 
     { 
      out[0] = Integer.parseInt(check[0]); 
     } 
     else 
     { 
      throw new InvalidMessageException(); 
     } 
    } 
    catch (InvalidMessageException ex) 
    { 
     System.err.println("Invalid instruction message"); 
     return null; 
    } 

    try 
    { 
     if (0 < Integer.parseInt(check[1])) 
     { 
      out[1] = Integer.parseInt(check[1]); 
     } 
     else 
     { 
      throw new InvalidMessageException(); 
     } 
    } 
    catch (InvalidMessageException ex) 
    { 
     System.err.println("Invalid instruction message"); 
     return null; 
    } 

    try 
    { 
     if(0 < Integer.parseInt(check[2])) 
     { 
      out[2] = Integer.parseInt(check[2]); 
     } 
     else 
     { 
      throw new InvalidMessageException(); 
     } 
    } 
    catch (InvalidMessageException ex) 
    { 
     System.err.println("Invalid instruction message"); 
     return null; 
    } 

    try 
    { 
     if (0 <= Integer.parseInt(check[3]) && Integer.parseInt(check[3]) < 256) 
     { 
      out[3] = Integer.parseInt(check[3]); 
     } 
     else 
     { 
      throw new InvalidMessageException(); 
     } 
    } 
    catch (InvalidMessageException ex) 
    { 
     System.err.println("Invalid instruction message"); 
     return null; 
    } 

    try 
    { 
     if(0 < Integer.parseInt(check[4])) 
     { 
      out[4] = Integer.parseInt(check[4]); 
     } 
     else 
     { 
      throw new InvalidMessageException(); 
     } 
    } 
    catch (InvalidMessageException ex) 
    { 
     System.err.println("Invalid instruction message"); 
     return null; 
    } 

    return out; 

} 

私が何かやって考えた:

inputText = inputText.replace(".", ""); 
    inputText = inputText.replace(":", ""); 
    inputText = inputText.replace(";", ""); 
    inputText = inputText.replace("\"", ""); 

などを...しかし、それはありません特に素晴らしい解決策ではないようです。誰かが良いアイデアを持っているなら、私に知らせてください。助けてくれてありがとう!

+3

正規表現の使用はどうですか?このようなものは、^(\ d、* \ d $)となります – Walfrat

+0

あなたがしたいことを完全に理解していませんでした。これらの値を文字列入力からintに変換しますか? – Ducaz035

+0

@Walfrat ^(\ d +、)* \ d + $はおそらくもっと正確です – pablochan

答えて

1

私はあなたのコードを読んでいなくても、あなたのメソッドを置き換える必要があると言っています。要件:

String input = "7,23,62,8,1130"; 
if (input.matches("(?:\\d+(?:,|$))+")) { 
    int[] result = Arrays.stream(input.split(",")).mapToInt(Integer::parseInt).toArray(); 
} else { 
    throw new InvalidMessageException(""); 
} 
+0

ありがとうございました!これは私が探していたものです。 –

+0

感謝のためにhttp://stackoverflow.com/help/someone-answersとupvoteを読んでください。 :-) – Vampire

2

あなたはあなたの入力を検証する正規表現式を使用することができます。

[0-9]+(,[0-9]+)*,? 

は、文字列の一致(正規表現)メソッドでそれをチェックして:

if (yourString.matches("[0-9]+(,[0-9]+)*,?")) { 

} 
+1

彼が表示されている数字と一致しません –

+0

@PoulBakありがとう、正規表現を更新しました –

0

これは正規表現のためのものですまさにです。

return inputText.matches("\\d+(,\\d+)*"); 
関連する問題