在Java中,检查ArrayList子字符串的方法和循环似乎未返回正确的值。

huangapple go评论71阅读模式
英文:

Method and Loop to check substring of Array List doesn't seem to be returning correct value in Java

问题

以下是你要翻译的内容:

public boolean isValidCode(String subjectCode){
    boolean validCode = true;
    if (subjectCode.length() != 6){
        validCode = false;
        String codeLetters = subjectCode.substring(0, 3);
        for (int i = 0; i < codeLetters.length(); i++){
            Character letters = subjectCode.charAt(i);
            if (!Character.isAlphabetic(letters))
                validCode = false; break;
        }
        String codeDigits = subjectCode.substring(3, subjectCode.length());
        for (int j = 3; j < codeDigits.length(); j++){
            Character digits = subjectCode.charAt(j);
            if (!Character.isDigit(digits))
                validCode = false; break;
        }
    }
    return validCode;
}

public boolean codeExist(String code, List<Subject> subjectList){

    boolean exists = false;
    for (int i = 0; i < subjectList.size(); i++){
        Subject newSubject = subjectList.get(i);
        String subject = newSubject.getSubjectCode();
        if (subject.equals(code)){
        }
    }
    return exists;
}

public static void main(String[] args) {
    // create array list        
    List<Subject> subjectList = new ArrayList<>();
    Scanner input = new Scanner(System.in); // inport scanner
    //add a new object ot the array list
    subjectList.add(new Subject("Programming in Java", "ITC206"));
    subjectList.add(new Subject("Cyber Ethics", "ITC311"));
    subjectList.add(new Subject("IT Helpdesk", "ITC232"));
    showSubjectList(subjectList);

    //user to add name and codes to list
    String newName = "Test";
    String newCode = "AAA123";
    Subject newSubject = new Subject(newName, newCode);

    boolean valid = newSubject.isValidCode(newCode);
    System.out.println(valid);

    boolean exists = newSubject.codeExist(newCode, subjectList);
    System.out.println(exists);

    showSubjectList(subjectList);
}
英文:

working on a few different methods to check that a code entered meets the criteria of 3 Characters and 3 Intergers i.e. ABC123 or JJJ111 etc I then also need to check if this code is used in an array list of objects that include a name and code i.e. Programming in Java, ITC206.

I have written two methods in a subject class to check the rerquirements are met, that the code doesnt already exist, but I am having a couple of issues. the first one isValidCode, seems to run through the loop but only returns false is there are not 6 characters, the .isAlphabetic and .isDigit methods don't seem to be affecting the outcome.

The second method, being codeExists just returns false all the time, even if the code is a new one and not being used.

I have tried changing a couple of things around and can't seem to figure out what is wrong with either..

Below are the methods and the main class I am using to check.

public boolean isValidCode (String subjectCode){
boolean validCode = true;
if (subjectCode.length() !=6){
validCode = false;               
String codeLetters = subjectCode.substring(0, 3);
for (int i = 0; i &lt; codeLetters.length(); i++){
Character letters = subjectCode.charAt(i);
if (!Character.isAlphabetic(letters)) 
validCode = false; break;
}
String codeDigits = subjectCode.substring(3, subjectCode.length());
for (int j = 3; j &lt; codeDigits.length(); j++){
Character digits = subjectCode.charAt(j);
if (!Character.isDigit(digits)) 
validCode = false; break;
} 
}                 
return validCode;
}
public boolean codeExist (String code, List&lt;Subject&gt; subjectList){
boolean exists = false;
for (int i = 0; i &lt; subjectList.size(); i++){
Subject newSubject = subjectList.get(i);
String subject = newSubject.getSubjectCode();
if (subject.equals(code)){  
}
}   
return exists;
}
public static void main(String[] args) {
// create array list        
List&lt;Subject&gt; subjectList = new ArrayList&lt;&gt;();
Scanner input = new Scanner(System.in); // inport scanner
//add a new object ot the array list
subjectList.add(new Subject(&quot;Programming in Java&quot;, &quot;ITC206&quot;));
subjectList.add(new Subject(&quot;Cyber Ethics&quot;, &quot;ITC311&quot;));
subjectList.add(new Subject(&quot;IT Helpdesk&quot;, &quot;ITC232&quot;));
showSubjectList(subjectList);
//user to add name and codes to list
String newName = &quot;Test&quot;;
String newCode = &quot;AAA123&quot;;
Subject newSubject = new Subject(newName, newCode);
boolean valid = newSubject.isValidCode(newCode);
System.out.println(valid);
boolean exists = newSubject.codeExist(newCode, subjectList);
System.out.println(exists);
showSubjectList(subjectList);

答案1

得分: 0

codeExist()函数会返回变量exists,该变量始终为false。如果检查匹配,只需返回true

public boolean codeExist(String code, List<Subject> subjectList){

    for (int i = 0; i < subjectList.size(); i++){
        Subject newSubject = subjectList.get(i);
        String subject = newSubject.getSubjectCode();
        if (subject.equals(code)){  
            return true;
        }
    }   
    return false;
}
英文:

codeExist() does return the variable exists which is always false. You should just return true if your check matches.

public boolean codeExist (String code, List&lt;Subject&gt; subjectList){
for (int i = 0; i &lt; subjectList.size(); i++){
Subject newSubject = subjectList.get(i);
String subject = newSubject.getSubjectCode();
if (subject.equals(code)){  
return true;
}
}   
return false;
}

答案2

得分: 0

我会在您的 `isValidCode` 方法中使用正则表达式来检查您的代码是否遵循特定的模式

在您的情况下是 `&quot;[A-Z]{3}[0-9]{3}&quot;`,即三个大写字母后跟三个数字

    public boolean isValidCode(String subjectCode) {
        String regex = &quot;[A-Z]{3}[0-9]{3}&quot;;
        return subjectCode.matches(regex);
    }

您的 `codeExist` 方法如果您使用的是 Java 8 或更高版本可以重写如下

    public boolean codeExist(String code, List&lt;Subject&gt; subjectList) {
        return subjectList.stream()
                          .map(Subject::getSubjectCode)
                          .anyMatch(code::equals);
    }
英文:

I would use regex in your isValidCode method to check if your code follows a certain pattern:

in your case &quot;[A-Z]{3}[0-9]{3}&quot; 3 capital letters followed by 3 digits:

public boolean isValidCode (String subjectCode){
String regex = &quot;[A-Z]{3}[0-9]{3}&quot;;
return subjectCode.matches(regex);
}

Your codeExist method, provided you use java 8 or higher, can be rewritten as follows:

public boolean codeExist (String code, List&lt;Subject&gt; subjectList){
return subjectList.stream()
.map(Subject::getSubjectCode)
.anyMatch(code::equals);
}

huangapple
  • 本文由 发表于 2020年10月9日 17:47:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64277622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定