I'm having a problem where I'm trying to add a name to an array and two amounts to another multidimensional array of equal length to the first.
I'm using a for loop to process the array with an if statement to check if the array is null/0. Is so, then the name/numbers are written to that element. The problem is that it writes to the name/numbers to every element in the array because the entire array is null/0.
Is there a way to get it to search, write the information, then stop?
public static void addStudent(String[] nameArray, double[][] fees) {
Scanner input = new Scanner(System.in);
String name; // Initializes name variable to store student name in for loop
double fees, paid;
System.out.println("Enter Member Name: ");
name = input.nextLine();
for (int i = 0; i < nameArray.length; i ) {
if (nameArray[i] == null) { // Searches for next empty array element to store name
name = nameArray[i]; // Stores name in empty element
}
}
System.out.println("Enter Member Fees: ");
fees = input.nextDouble();
for (int j = 0; j < fees.length; j ) { // Searches for empty element, which coincides w/ nameArray element number
if (fees[j][0] == 0) {
fees[j][0] = fees; // Stores fee in empty element
}
}
System.out.println("Enter Amount Paid: ");
paid = input.nextDouble();
for (int k = 0; k < fees.length; k ) { // Searches for empty element, which coincides w/ nameArray element number
if (fees[k][1] == 0) {
fees[k][1] = paid; // Stores paid fee in empty element
}
}
}
CodePudding user response:
It seems to me that you could acheive everything in one loop as follows:
public static void addStudent(String[] nameArray, double[][] fees) {
Scanner input = new Scanner(System.in);
String name; // Initializes name variable to store student name in for loop
double fees, paid;
System.out.println("Enter Member Name: ");
name = input.nextLine();
System.out.println("Enter Member Fees: ");
fees = input.nextDouble();
System.out.println("Enter Amount Paid: ");
paid = input.nextDouble();
for (int i = 0; i < nameArray.length; i ) {
nameArray[i] = name; // Stores name in empty element
fees[i][0] = fees; // Stores fee in empty element
fees[i][1] = paid; // Stores paid fee in empty element
}
}
Also there is no need to check if the element is empty because your for loop starts from 0 and increases in each iteration so the next element in the array will always be empty.
If you really wanna check if it's empty you could still do it much simpler
public static void addStudent(String[] nameArray, double[][] fees) {
Scanner input = new Scanner(System.in);
String name; // Initializes name variable to store student name in for loop
double fees, paid;
System.out.println("Enter Member Name: ");
name = input.nextLine();
System.out.println("Enter Member Fees: ");
fees = input.nextDouble();
System.out.println("Enter Amount Paid: ");
paid = input.nextDouble();
for (int i = 0; i < nameArray.length; i ) {
if(nameArray[i] == null) {
nameArray[i] = name; // Stores name in empty element
fees[i][0] = fees; // Stores fee in empty element
fees[i][1] = paid; // Stores paid fee in empty element
}
}
}