So if I understand you correctly, what you want to do is about the following:
if (containsName(studentNamesArray, studentName) == false) {
addStudent(studentNamesArray, studentName);
}
Besides that this could be easily solved with a Java Set like this:
studentSet.add(studentName); // automatically ignores duplicates
all you need to do for the first code part is to write the functions containsName and addStudent
boolean containsName(String[] studentNamesArray, String studentName) {
for (int i = 0; i < studentNamesArray.length; ++i) { // specialized while loop over all array elements
if (studentName.equals(studentNamesArray[i])) { // use equals for String comparison! This will as well not match on "null"
return true; // we found a match
}
}
return false; // no match was found
}
And
void addStudent(String[] studentNamesArray, String studentName) {
int i = 0;
while ((studentNamesArray[i] != null) && // this student entry is already filled by someone else
(i < studentNamesArray.length)) { // we are still within the array bounds
++i;
}
if (i < studentNamesArray.length) { // is i still within the array bounds?
studentNamesArray[i] = studentName; // add the student
} else {
// we have too many students. What to do now?
}
}
There are several things not checked in this code:
- What if there are more students than the size of the array?
- What if studentName is
null?
This approach to writing code is called Top-down approach and makes coding much easier, if you know the general program logic, but not yet the details.
Edit: The full code:
import java.util.Scanner;
public class Test {
static boolean containsName(String[] studentNamesArray, String studentName) {
for (int i = 0; i < studentNamesArray.length; ++i) { // specialized while loop over all array elements
if (studentName.equals(studentNamesArray[i])) { // use equals for String comparison! This will as well not match on "null"
return true; // we found a match
}
}
return false; // no match was found
}
static void addStudent(String[] studentNamesArray, String studentName) {
int i = 0;
while ((studentNamesArray[i] != null) && // this student entry is already filled by someone else
(i < studentNamesArray.length)) { // we are still within the array bounds
++i;
}
if (i < studentNamesArray.length) { // is i still within the array bounds?
studentNamesArray[i] = studentName; // add the student
} else {
// we have too many students. What to do now?
}
}
public static void main(String[] args) {
final String[] studentNamesArray = new String[10];
String studentName;
final Scanner scanner = new Scanner(System.in);
do {
System.out.println("Please insert your name:");
studentName = scanner.nextLine();
if (studentName.length() > 0) {
if (containsName(studentNamesArray, studentName) == false) {
addStudent(studentNamesArray, studentName);
System.out.println(studentName + " added.");
} else {
System.out.println(studentName + " was already in the list.");
}
}
} while (studentName.length() > 0);
for (int i = 0; i < studentNamesArray.length; ++i) {
System.out.println("Student #" + i + ": " + studentNamesArray[i]);
}
}
}
Using sample input, I get the following:
Please insert your name:
Two
Two added.
Please insert your name:
Test
Test added.
Please insert your name:
Test
Test was already in the list.
Please insert your name:
test
test added.
Please insert your name:
test
test was already in the list.
Please insert your name:
Student #0: Two
Student #1: Test
Student #2: test
Student #3: null
Student #4: null
Student #5: null
Student #6: null
Student #7: null
Student #8: null
Student #9: null