Here is what I'm trying to do:
Write an application that stores at least four different course names and meeting days and times in a two-dimensional array. Allow the user to enter a course name (such as "CS 110") and display the day of the week and time that the course is held (such as Th 3:30). If the course does not exist, display an error message.
Here's the code I have:
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Schedule {
public static void main(String[] args) {
//declare variables and arrays
final int NUM_RANGES = 3;
int sub = NUM_RANGES - 1;
String[][] classNames = {
{"CS 2401", "TS 5697", "UO 7896"},
{"Tue 8:30", "Thu 7:30", "Fri 9:15" }
};
//get input
String classInput = JOptionPane.showInputDialog("Please input a class name: ");
//match to output and print
while(sub >= 0)
--sub;
if (classInput.equals(classNames[0])) {
JOptionPane.showMessageDialog(null, "Class time is: " + classNames[0][0]);
System.exit(0);
} else if (classInput.equals("TS 5697")) {
JOptionPane.showMessageDialog(null, "Class time is: " + classNames[1][1]);
System.exit(0);
} else if (classInput.equals("UO 7896")) {
JOptionPane.showMessageDialog(null, "Class time is: " + classNames[2][2]);
System.exit(0);
} else {
JOptionPane.showMessageDialog(null, "Please enter a valid class name.");
System.exit(0);
}
}
}
As you can see, I'm trying to do the equals method on the first IF but can't figure it out. Thank you for your help.
subvariable is redundant and can be removed. It will be assigned the value -1 (after thewhile(sub >= 0)loop has completed) and then it is never used again.subgood for, does not make sense to me. Your whole approach is fairly static - just retrieve user input, iterate through the array of courses and compare them with user input (e.g.equalsorequalsIgnoreCase).