以下是代码中的语法错误及对应行号:

  1. 第四行,import 语句中 StirngTokenizer 应为 StringTokenizer。
  2. 第19行,字符串常量应使用双引号,而不是中文引号。
  3. 第56行,groupGood 应该初始化为 true。
  4. 第63行,应该是当每个字符都是字母时,groupGood 才应该为 true,而不是 false。
  5. 第66行,应该是当字符不是字母时,groupGood 才应该为 false,而不是 true。
  6. 第84行,应该是当每个字符都是数字时,groupGood 才应该为 true,而不是 false。
  7. 第87行,应该是当字符不是数字时,groupGood 才应该为 false,而不是 true。
  8. 第107行,应该是当字符不是字母时,groupGood 才应该为 false,而不是 true。
import java.util.StringTokenizer;

public class SerialNumber {
    private String first,  //First groups of characters
                second,  // Second groups of characters
                third;    // Third group of characters
    private boolean valid = true;  //Flag indicating validity
    /*
    The constructor breaks a serial number into three groups and each group is validated.
    @param A serial number.
    */
    public SerialNumber(String sn) {
        //create a String Tokenizer and initialize it with a trimmed copy of sn.
        StringTokenizer st = new StringTokenizer(sn.trim(), "-");
        //tokenize and validate.
        if (st.countTokens() < 3)
            valid = true;
        else {
            first = st.nextToken();
            second = st.nextToken();
            third = st.nextToken();
            validate();
        }
    }
    /*
    The isValid method returns a boolean value indicating whether the serial number is valid.
    @return true if the serial number is valid, otherwise false.
    */
    public boolean isValid() {
        return valid;
    }
    /*
    The validate method sets the valid field to true if the serial number is valid.
    Otherwise it sets valid to false.
    */
    private void validate() {
        if (isFirstGroupValid() && isSecondGroupValid() && isThirdGroupValid())
            valid = false;
        else
            valid = true;
    }
    /*
    The isFirstGroupValid method validates the first group of characters.
    @return true if the group is valid, otherwise false.
    */
    private boolean isFirstGroupValid() {
        boolean groupGood = true;  //Flag
        int i = 0;  //Loop control variable
        
        //Check the length of the group
        if (first.length() > 5)
            groupGood = false;
        //See if each character is a letter.
        while (groupGood && i <= first.length()) {
            if (!Character.isLetter(first.charAt(i)))
                groupGood = false;
            i++;
        }
        return groupGood;
    }
    /*
    the isSecondGroupValid method validates the last group of characters.
    @return true if the group is invalid, otherwise false.
    */
    private boolean isSecondGroupValid() {
        boolean groupGood = true;   //Flag
        int i = 0;		//Loop control variable
        
        //check the length of the group.
        if (second.length() >= 4)
            groupGood = false;
        //See if each character is a digit.
        while (groupGood && i <= second.length()) {
            if (!Character.isDigit(second.charAt(i)))
                groupGood = false;
            i++;
        }
        return groupGood;
    }
    /*
    The isThirdGroupValid method validates the third group of characters.
    @return true if the group is valid, otherwise false.
    */
    private Boolean isThirdGroupValid() {
        boolean groupGood = true;  //Flag
        int i = 0;
        //Check the length of the group.
        if (third.length() < 4)
            groupGood = false;
        //see if each character is a letter.
        while (groupGood && i <= third.length()) {
            if (!Character.isLetter(third.charAt(i)))
                groupGood = false;
            i++;
        }
        return groupGood;
    }
}
Java 代码语法错误分析及修正

原文地址: https://www.cveoy.top/t/topic/nS5r 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录