2016-12-12 11 views
0
public static void main(String args[]) { 
    Scanner input = new Scanner(System.in); 

    String regex = "[a-zA-Z ]+$"; 
    String regex1 = "\\d[0-9]|[1-9]"; 
    String regex2 = "^[a-zA-Z0-9 ]+$"; 
    String petName; 

    StringBuilder output = new StringBuilder(); 

    do { 
     System.out.print("\nHow Many Pet do you have? Give from 1-3:"); 
     petName = input.nextLine(); 

     if (petName.isEmpty()) { 
      System.out.println("Number field should not be Empty."); 
     } else if (!petName.matches(regex1)) { 
      System.out.println("Please Enter A Valid Number!"); 
     } 
    } while (!petName.matches(regex1)); 

    do { 
     Integer.parseInt(petName); 
     String[] pets = new String[Integer.parseInt(petName)]; 

     System.out.print("\nList Down All Your Pet Names:\n"); 

     for (int i = 0; i < pets.length; i++) { 
      System.out.print("\nPET" + (i + 1) + ":"); 
      pets[i] = input.nextLine(); 

      if (pets[i].isEmpty()) { 
       System.out.print("String field should not be Empty."); 
      } else if (!pets[i].matches(regex)) { 
       System.out.print("Please input a valid String."); 
      } 
     } 
     output.append("\nThese Are The List Of The Pets You Have:"); 
     for (int i = 0; i < pets.length; i++) { 
      output.append("\nPET:").append(i + 1).append(" ").append(pets); 
     } 
    } while (!petName.matches(regex)); 

    System.out.println(output); 
} 

私は上記のコードに少し問題があります。forloopのユーザー確認入力

整数を入力すると、「有効な文字列を入力してください」というメッセージが表示されます。フィールドに何も入力しなかった場合、この別のメッセージが表示されます。空ではない "。しかし、たとえフィールドに文字列値を入力しても、 "有効な文字列を入力してください"というメッセージが表示され、Enterキーを押すたびに繰り返しループが繰り返されます。

+0

この正規表現 '' [a-zA-Z] + $ "'はどういう意味ですか?ここで使用される入力の例をいくつか追加してください。 ( "\ nPET" +(i + 1)+ ":"); – AxelH

+0

for(int i = 0; i

+0

これは文字列の検証に使用するコードです。 –

答えて

0

2番目のwhileループに問題があります。まず、あなたのループ状態はpetNameです。これは最初のwhileループを離れると変更されません。第2に、forループが正しく入れ子になっていないようです。各ペットの名前の有効な入力のためにループしたいので、forループの中に2番目のwhileループを置くべきです。

次の変更されたコードではわかりやすいでしょう。 append(pets)を呼び出すと、pets配列の結果であるtoStringの結果が出力され、個々のペットの名前は出力されません。このためにはappend(pets[i])を使用してください。

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 

    String regex = "[a-zA-Z ]+$"; 
    String regex1 = "\\d[0-9]|[1-9]"; 
    String regex2 = "^[a-zA-Z0-9 ]+$"; 
    String petName; 

    StringBuilder output = new StringBuilder(); 

    do 
    { 
     System.out.print("\nHow Many Pet do you have? Give from 1-3:"); 
     petName = input.nextLine(); 

     if (petName.isEmpty()) 
     { 
      System.out.println("Number field should not be Empty."); 
     } 
     else if (!petName.matches(regex1)) 
     { 
      System.out.println("Please Enter A Valid Number!"); 
     } 

    } while (!petName.matches(regex1)); 

    String[] pets = new String[Integer.parseInt(petName)]; 

    System.out.print("\nList Down All Your Pet Names:\n"); 

    for (int i = 0; i < pets.length; i++) 
    { 
     do 
     { 
      System.out.print("\nPET" + (i + 1) + ":"); 
      pets[i] = input.nextLine(); 

      if (pets[i].isEmpty()) 
      { 
       System.out.print("String field should not be Empty."); 
      } 
      else if (!pets[i].matches(regex)) 
      { 
       System.out.print("Please input a valid String."); 
      } 
     } while (!pets[i].matches(regex)); 
    } 

    output.append("\nThese Are The List Of The Pets You Have:"); 

    for (int i = 0; i < pets.length; i++) 
    { 
     output.append("\nPET:").append(i + 1).append(" ").append(pets[i]); 
    } 

    System.out.println(output); 
} 
+0

私はエンドレスループの部分に同意します。あなたの答えにコードの一部を表示するだけかもしれません;) – AxelH

+0

Yahhh!それが私が望む出力を思いつくことができなかった理由です、私はその入れ子になったwhileループを考えていましたが、私はそれをどのように配置するか分かりませんでした。それは私がまだこの行を含んでいるからです。String [] pets = new String [Integer.parseInt(petName)]; Do whileループの内側にあります。 私は本当にごめんなさい、あなたが始まったときにそれがあなたのためにどのようなものかを知っています:)。しかし、あなたのすぐ援助と非常に親切なアプローチに感謝します。神様、あなたを祝福してください。 –

関連する問題