Below is the code im having issues with it compiles fine and runs , but as i put in information in the joptionpane box with option 1)init and try to display it with option 2) listinformation it pops up the error that "no students entered".
What I want it do is list all the students , gpas , and need with a jOptionPane.showMessageDialog
Please please help.
import javax.swing.JOptionPane;
public class Test {
public static void main (String[] args) {
String[] firstname = new String[1000];
String[] lastname = new String[1000];
double[] gpa = new double[1000];
int[] award = new int[1000];
int awardsum = 0;
boolean[] need = new boolean[1000];
int numStudent = 0;
double classaverage;
int schtotal = 0;
String choice = "";
while (!choice.equals("4")) {
choice = getMenuChoice ();
if(choice.equals("1")){
init(firstname,lastname, gpa, need, numStudent);
}else if(choice.equals("2")){
listinformation(firstname, lastname, gpa, need, numStudent);
}else if(choice.equals("3")){
total(schtotal,award, numStudent);
}
}
}//main
public static int init (String[] firstname, String[] lastname, double[] gpa, boolean[] need, int count){
do {
firstname[count] = JOptionPane.showInputDialog("First name?");
lastname[count] = JOptionPane.showInputDialog("Last name?");
gpa[count] = Double.parseDouble(JOptionPane.showInputDialog("Gp a; Input as X.XX"));
need[count] = Boolean.parseBoolean(JOptionPane.showInputDialog(" Financial need? ;True or False"));
count++;
}while (JOptionPane.showConfirmDialog(null,"Add another student?")==JOptionPane.YES_OPTION);
return count;
}//init
public static int[] awardcal(int awardsum, int[] award, double[] gpa, boolean[] need, int count){
if (count > 0){
for (int index = 0; index < count; index++){
if(gpa[index] == 4.0){
awardsum = awardsum + 1000;
award[index] = award[index] + awardsum;
}
}
}return award;
}
public static int total(int schtotal,int[]award, int count){
for(int index = 0; index < count; index ++){
schtotal = schtotal + award[index];
}
JOptionPane.showMessageDialog(null, schtotal);
return schtotal;
}
public static void listinformation(String[] firstname, String[] lastname, double[] gpa, boolean[] need, int count){
String output = "";
if (count > 0){
for (int index = 0; index < count; index++) {
output += firstname[index] + " " + lastname[index] + " " + gpa[index] + " " + need[index] + "\n";
}
}else output = "No students entered";
JOptionPane.showMessageDialog(null, output);
}
public static String getMenuChoice () {
String menu = "1) Add Student Information\n2)View info for all students\n3)Total scholarships awarded\n4) Exit";
String inputValue = JOptionPane.showInputDialog(menu);
return inputValue;
} // getMenuChoice()
}//class
Method parameters are passed by value only, and so your int numStudent field will not be changed by the init method. Rather, a local variable, the count parameter will only be changed, and numStudent will be unaffected. You will want to check the value of the numStudent after init(...)
finishes and you'll see that it remains at 0.
ArrayList<Student>
to hold the student data. Then you won't even need a numStudent field but can directly check the ArrayList's size()
. For example:
public class CounterTest {
private static int counter1 = 0;
private static int counter2 = 0;
public static void incrCounter1ViaParameter(int counter) {
counter++; // increments the **parameter**
}
public static void incrCounter2Directly() {
counter2++; // directly increments the **static class field**
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
incrCounter1ViaParameter(counter1);
incrCounter2Directly();
}
System.out.println("Counter 1: " + counter1);
System.out.println("Counter 2: " + counter2);
}
}