2016-12-12 29 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下的用戶驗證輸入

我想要的是如果我輸入一個整數然後它會提示我這條消息「請輸入一個有效的字符串」,或者如果我沒有在字段中輸入任何內容,那麼它會提示我這個另一個消息「不是空的「。但是,即使我在字段中輸入了一個字符串值,它仍然會提示「請輸入一個有效的字符串」,並且每當我按下回車鍵時,循環仍會一遍又一遍地執行相同的操作。

+0

什麼是假設這個正則表達式'「[a-zA-Z] + $」'?在此添加一些輸入示例。 – AxelH

+0

for(int i = 0; i

+0

這是我用來驗證字符串的代碼。 –

回答

0

你的第二個while循環有一些問題。首先,你的循環條件是檢查petName,離開第一個while循環後沒有改變。其次,for循環似乎嵌套錯誤。既然你想循環每個寵物名稱的有效輸入,你應該把第二個while循環放在for循環中,而不是反過來。

使用以下修改的代碼可能會更容易看到。還請注意,調用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循環中。 我真的很抱歉,當你開始時,你知道你是什麼感覺:)。但是,我真的很感謝你的直接幫助和非常友善的態度。上帝,祝福你,先生。 –