英文:
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 < 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);
答案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<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;
}
答案2
得分: 0
我会在您的 `isValidCode` 方法中使用正则表达式来检查您的代码是否遵循特定的模式:
在您的情况下是 `"[A-Z]{3}[0-9]{3}"`,即三个大写字母后跟三个数字:
public boolean isValidCode(String subjectCode) {
String regex = "[A-Z]{3}[0-9]{3}";
return subjectCode.matches(regex);
}
您的 `codeExist` 方法,如果您使用的是 Java 8 或更高版本,可以重写如下:
public boolean codeExist(String code, List<Subject> 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 "[A-Z]{3}[0-9]{3}"
3 capital letters followed by 3 digits:
public boolean isValidCode (String subjectCode){
String regex = "[A-Z]{3}[0-9]{3}";
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<Subject> subjectList){
return subjectList.stream()
.map(Subject::getSubjectCode)
.anyMatch(code::equals);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论