2017-11-11 95 views
0

我在寫這個簡單的程序,並在數據驗證部分,其中程序必須拒絕該用戶輸入,如果它包含包含一個數字,用ASCII文件,但使用(ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))似乎並沒有工作enter image description hereASCII檢查輸入是否包含字母或數字不起作用?

enter image description here

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Scanner; 

public class Liu_YiJun_A2Q4 { 
public static void main(String[] args) { 

    String fName = "", lName = "", type = "", add = "", post = "", use = "c", use2 = ""; 
    int age = 0, phone = 0, numOfU = 0; 
    char ch = 0; 

    while (use.equalsIgnoreCase("c")) { 
     System.out.println(numOfU); 
     Scanner input = new Scanner(System.in); 

     System.out.print("Please enter your FIRST name: "); 
     fName = input.nextLine(); 
     while (fName.length() >= 15 && (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){ 
      System.out.println("The characters in the name are too long OR The value is not a letter"); 
      System.out.print("Please enter your FIRST name: "); 
      fName = input.nextLine(); 
     } 

     System.out.print("Please enter your LAST name: "); 
     lName = input.nextLine(); 
     System.out.print("Please enter your AGE:   "); 
     age = input.nextInt(); 
     input.nextLine(); 
     System.out.print("Please either FT ot PT:   "); 
     type = input.nextLine(); 
     System.out.print("Please enter the ADDRESS:  "); 
     add = input.nextLine(); 
     System.out.print("Please enter the POSTAL CODE: "); 
     post = input.next(); 
     System.out.print("Please enter your PHONE NUMBER: "); 
     phone = input.nextInt(); 

     System.out.println(); 

     System.out.println("*********************************************"); 

     String val = "" + ((int) (Math.random() * 9000) + 1000); 
     System.out.println("Membership ID: " + val); 
     System.out.println("Name of new member: " + fName + " " + lName); 
     System.out.println("Age: " + age); 
     System.out.println("User is: " + type); 
     System.out.println("Address: " + add + " " + post); 
     Calendar cal = Calendar.getInstance(); 
     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyy"); 
     System.out.println("Member Since: " + sdf.format(cal.getTime())); 

     System.out.println(); 
     System.out.print("Enter (C) to enter a new user or X to Exit: "); 
     use = input.next(); 

     if (use.equals("x")) { 
      System.out.println("Thanks for using ICMSG Membership System. You have entered " + numOfU + " users in this session. Have a nice day!"); 
     } 
    } 

} 

}

+2

正如您從屏幕截圖中可以看到的,intelliJ突出顯示了「if」條件中的某些內容。將鼠標懸停在上面,看看它告訴你什麼。這或者意味着你的情況是錯誤的,總是_false_(我相信它),或者它總是正確的或者intelliJ表明你可以縮短條件。儘管如此,情況總是錯誤的。 –

+2

請注意,您從未將0以外的任何內容分配給'ch'。 – luk2302

+0

還應指定哪些其他內容? @ luk2302 –

回答

0

我不會直接回答,因爲學習的目的,但這裏是你應該怎麼做:

final String alphabet = "abcdefghijklmnoprstuwxyz"; // define allowed chars 
char inputChar = 'c'; // get the input from scanner or whatever 
boolean allowed = alphabet.toUpperCase().contains(Character.toUpperCase(inputChar) + ""); 

然後你就會知道,如果給定一個字符是一個字母開頭。 由於@VGR指出評論,這不是正則表達式的任務。

而且,把alphabet變量作爲一類構件,以防止在循環恆定的內存分配。

0

你檢查了字符ch是ASCII字母。 但是,您必須檢查輸入中的每個字符是否爲字母。 讀取輸入後。

要做到這一點,你通過從字符串fName等所有字符需要循環。

for (int i = 0; i < fName.length(); ++i) { 
    char ch = fName.charAt(i); 
    if (is not a letter) { 
     break; // Error, jump out of the for-loop. 
    } 
} 

對於fName和所有其他輸入。

如果for循環仍然沒有處理,使用while循環。

int i = 0; 
while (i < fName.length()) { 
    ... 
    ++i; 
} 
相關問題