Nabend,
Ich habe ein Problem mit meiner Hand-in Aufgabe.
Das Programm soll max. 10 Kurse und die jeweiligen Zensuren in Arrays speichern.
Der Nutzer kann zwischen Option 1, 2, 3 & 4 auswählen.
1 nimmt den Input entgegen ( Kurs und Zensur ) und speichert dies dann in den Arrays ab
2 stellt alles folgender maßen dar: (Überhaupt mit JOptionPane und Arrays möglich??)
3 soll den Durchschnitt der eingegebenen Zensuren berechnen und darstellen
und 4 soll ganz einfach das Programm beenden.
Mein Problem gerade ist, dass er den Durchschnitt nicht berechnen will.
Bekomme da nur 'ne NullPointerException
Hier mein derzeitiger Code (Ja, noch das totale Chaos^^)
Geht als Anfänger aber denke ich noch klar :toll:
Und gerade bin ich nur rein auf die Funktion aus
Wenn jemand den Fehler sieht und vielleicht noch tausend andere Fehler, die ich gemacht habe, wäre das sehr cool ^^
Ich habe ein Problem mit meiner Hand-in Aufgabe.
Das Programm soll max. 10 Kurse und die jeweiligen Zensuren in Arrays speichern.
Der Nutzer kann zwischen Option 1, 2, 3 & 4 auswählen.
1 nimmt den Input entgegen ( Kurs und Zensur ) und speichert dies dann in den Arrays ab
2 stellt alles folgender maßen dar: (Überhaupt mit JOptionPane und Arrays möglich??)
Code:
COURSE NAME GRADE
---------------------
MATH 7
PROGRAMMING 10
BIOLOGY 4
Code:
Total of grades: 21.0
Count of grades: 3.0
Average of grades: 7.0
Mein Problem gerade ist, dass er den Durchschnitt nicht berechnen will.
Bekomme da nur 'ne NullPointerException
Code:
Exception in thread "main" java.lang.NullPointerException
at GradesClass.sum(GradesClass.java:70)
at UserInteractionMain.main(UserInteractionMain.java:29)
Hier mein derzeitiger Code (Ja, noch das totale Chaos^^)
Geht als Anfänger aber denke ich noch klar :toll:
Und gerade bin ich nur rein auf die Funktion aus
Wenn jemand den Fehler sieht und vielleicht noch tausend andere Fehler, die ich gemacht habe, wäre das sehr cool ^^
Java:
import javax.swing.JOptionPane;
public class UserInteractionMain
{
public static void main(String[] args)
{
int index = 0;
String optionChoice = optionDialog();
while ( index < GradesClass.MAX_COURSES )
{
if ( optionChoice.equals( "1" ) )
{
GradesClass.courses[index] = GradesClass.courseInput();
GradesClass.grades[index] = GradesClass.gradeInput();
optionChoice = optionDialog();
index++;
}
else if ( optionChoice.equals( "2" ) )
{
// Nicht fertig - Test
JOptionPane.showMessageDialog( null, "Option 2" );
optionChoice = optionDialog();
}
else if ( optionChoice.equals( "3" ) )
{
int sum = GradesClass.sum( GradesClass.grades );
double average = GradesClass.average( GradesClass.grades );
JOptionPane.showMessageDialog( null, "Total of Grades: " + sum
+ "Count of Grades: " + index
+ "Average of Grades: " + average );
optionChoice = optionDialog();
}
else if ( optionChoice.equals( "4" ) )
{
int exit = JOptionPane.showConfirmDialog( null, "Are you sure you want to quit?", "Quit", JOptionPane.YES_NO_CANCEL_OPTION );
if ( exit == JOptionPane.YES_OPTION )
{
JOptionPane.showMessageDialog( null, "Bye!", "EXIT", JOptionPane.INFORMATION_MESSAGE);
return;
} else {
optionChoice = optionDialog();
}
}
} // While end
} // Main end
// Option handling
public static String optionDialog( )
{
String choice = JOptionPane.showInputDialog( null, "1. Enter a course and grade. \n2. Display all grades. \n3. Calculate the average. \n4. Exit" );
choice = optionDialogCheck(choice);
return choice;
}
public static String optionDialogCheck( String options )
{
final String REGEX_OPTIONS = "(^[1-4]$)";
if ( options.isEmpty() ) {
JOptionPane.showMessageDialog( null, "Nothing entered!" );
options = optionDialog();
} else if ( !options.matches( REGEX_OPTIONS ) ) {
JOptionPane.showMessageDialog( null, "Not an option!" );
options = optionDialog();
}
return options;
}
} // Class end
Java:
import javax.swing.JOptionPane;
public class GradesClass
{
public static final int MAX_COURSES = 10;
public static String[] courses = new String[MAX_COURSES];
public static String[] grades = new String[MAX_COURSES];
// --------------------------- COURSE INPUT ----------------------------- \\
public static String courseInput( )
{
String course = JOptionPane.showInputDialog( "Please enter the Course : " );
course = courseInputCheck(course);
return course;
}
public static String courseInputCheck( String courseInput )
{
if ( courseInput.isEmpty() ) {
JOptionPane.showMessageDialog( null, "Nothing entered!" );
courseInput = courseInput();
}
return courseInput;
}
// --------------------------- GRADE INPUT ----------------------------- \\
public static String gradeInput( )
{
String grade = JOptionPane.showInputDialog( "Please enter your Grade : " );
grade = gradeInputCheck(grade);
return grade;
}
public static String gradeInputCheck( String gradeInput )
{
final String REGEX_POSSIBLE_GRADES = "[-3 0 2 4 7 10 12]+";
if ( gradeInput.isEmpty() )
{
JOptionPane.showMessageDialog( null, "Nothing entered!" );
gradeInput = gradeInput();
} else if ( !gradeInput.matches( REGEX_POSSIBLE_GRADES ) ) {
JOptionPane.showMessageDialog( null, "Not a possible Grade!" );
gradeInput();
}
return gradeInput;
}
// --------------------------------- METHODS ---------------------------------\\
public static int sum( String[] grades )
{
int sum = 0;
int[] gradesToInt = new int[grades.length];
for ( int i = 0; i < grades.length; i++ )
{
if ( grades[i].isEmpty() )
{
gradesToInt[i] = 0;
} else {
gradesToInt[i] = Integer.parseInt(grades[i]);
}
sum += gradesToInt[i];
}
return sum;
}
public static double average( String[] grades )
{
int sum = 0;
int index = 0;
int[] gradesToInt = new int[grades.length];
for ( int i = 0; i < grades.length; i++ )
{
if ( grades[i].isEmpty() )
{
gradesToInt[i] = 0;
} else {
gradesToInt[i] = Integer.parseInt(grades[i]);
index++;
}
sum += gradesToInt[i];
}
double average = sum / index;
return average;
}
}