Average program with array and loop and JOptionPane.
The project is to do a grade averaging program in a loop using JOptionPane.
Bellow is the errors I am getting, does int[] array = new int[numberOfGradesString]; have to be defined like int[] array = new int[100];?
Because I have seen examples that compiled Java: Arrays
Thank you for the help.
Code :
import javax.swing.*;
public class Project2TestEdit1 {
public static void main( String[] args ) {
//...Varibles declared for whole program.
String numberOfGradesString, gradeString;
int average, sumOfGrades, selection;
int numberOfGrades = 1;
double quotient;
boolean runAgain = true;
//...Loop
do{
//...Enter a Grade to be averaged for each iteration of the loop.
gradeString = JOptionPane.showInputDialog( "Enter a grade to be averaged." );
//...Asks if you want to run anouther.
selection = JOptionPane.showConfirmDialog(null,
"Would you like to run another?","Confirmation",
JOptionPane.YES_NO_OPTION);
//...Runs loop again if option YES is selected.
runAgain = (selection == JOptionPane.YES_OPTION);
//...Adds anouther place value to the array increment each iteration of the loop.
numberOfGrades = numberOfGrades++;
//...Continues the loop.
}while (runAgain);
//...The array, each iteration of the loop a place value is added with the grade intered as the value.
int[] array = new int[numberOfGradesString {gradeString}];
//...Avergaing formula.
sumOfGrades = numberOfGrades + gradeString;
average = (sumOfGrades / numberOfGrades);
//...
JOptionPane.showMessageDialog(null,
average + " Is the Average" );
System.exit(0); //...Ends the window.
}
}
ERRORS
----jGRASP exec: javac -g C:\Documents and Settings\jeremy\Desktop\SCHOOL\CMIS 141\week 7\Project2TestEdit1.java
Project2TestEdit1.java:45: incompatible types
found : java.lang.String
required: int
int[] array = new int[numberOfGradesString];
^
Project2TestEdit1.java:49: incompatible types
found : java.lang.String
required: int
sumOfGrades = numberOfGrades + gradeString;
^
2 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Re: Average program with array and loop and JOptionPane.
Code :
int[] array = new int[numberOfGradesString]
found String, expect int.
try using Integer.parseInt(numberOfGradesString)
Code :
[B][U]int[] array = new int[Integer.parseInt(numberOfGradesString)][/U][/B]
---
basically, you are trying to set the size of the array using a String when really it has to be a number...the code above should fix everything.
still confused? google "convert string to int java"
example: Convert string to int : ConvertLanguage BasicsJava
Re: Average program with array and loop and JOptionPane.
Now I am getting this error?
Also am I missing something here gradeString is the value entered for the grade, numberOfGradesString defines the size of the arrayand array is the name of the array. So shouldn't I some how have gradeString defined in
int[] array = new int[Integer.parseInt(numberOfGradesString)];
Code :
sumOfGradesString = numberOfGrades + gradeString;
average = (sumOfGradesString / numberOfGradesString);
Project2TestEdit1.java:50: operator / cannot be applied to java.lang.String,java.lang.String
average = (sumOfGradesString / numberOfGradesString);
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Thank you.
Re: Average program with array and loop and JOptionPane.
/*...I think this fixes my array but I am not sure and I have a few other qestions, gradeString is the actual value entered for each grade, numberOfGradeStrings keeps track of each iteration of the loop and makes up the array size, array is just the name of the array.*/
Code :
int[] array = new int[Integer.parseInt(numberOfGradesString)];
Am I right in this logic or do I have things confused?
Can a string not be a part of an arithmetic problem?
/*This is the last compile error that I am getting.
Error
Project2TestEdit1.java:50: operator / cannot be applied to java.lang.String,java.lang.String
average = (sumOfGradesString / numberOfGradesString);
/\ */
Code :
import javax.swing.*;
public class Project2TestEdit1 {
public static void main( String[] args ) {
//...Varibles declared for whole program.
String numberOfGradesString, gradeString, sumOfGradesString;
int average, selection;
int numberOfGrades = 1;
double quotient;
boolean runAgain = true;
//...Loop
do{
//...Enter a Grade to be averaged for each iteration of the loop.
gradeString = JOptionPane.showInputDialog( "Enter a grade to be averaged." );
//...Asks if you want to run anouther.
selection = JOptionPane.showConfirmDialog(null,
"Would you like to run another?","Confirmation",
JOptionPane.YES_NO_OPTION);
//...Runs loop again if option YES is selected.
runAgain = (selection == JOptionPane.YES_OPTION);
//...Adds anouther place value to the array increment each iteration of the loop.
numberOfGrades = numberOfGrades++;
//...Continues the loop.
}while (runAgain);
//...The array, each iteration of the loop a place value is added with the grade intered as the value.
int[] array = new int[Integer.parseInt(numberOfGradesString)];
//...Avergaing formula.
sumOfGradesString = numberOfGrades + gradeString;
average = (sumOfGradesString / numberOfGradesString);
//...
JOptionPane.showMessageDialog(null,
average + " Is the Average" );
System.exit(0); //...Ends the window.
}
}
Re: Average program with array and loop and JOptionPane.
You cannot mix Strings with primitive types to perform arithmetic operations.
The way you initialized the array in your first code snippet is correct.
Re: Average program with array and loop and JOptionPane.
Quote:
Originally Posted by
literallyjer
You cannot mix Strings with primitive types to perform arithmetic operations.
I tried declaring them as int and got an error expecting string, so I changed them to string and got an arithmetic error. Is there another way that I should declare these variables.
Code :
String numberOfGradesString, gradeString, sumOfGradesString;
Quote:
Originally Posted by
literallyjer
The way you initialized the array in your first code snippet is correct.
So I do have this array right, I have not been able to compile due to other errors, so I have not been able to prove that I have variable which defines the size of my array right. Don't I have to declare gradeString in the array some were since that is the actual value being held in the array?
Code :
int[] array = new int[Integer.parseInt(numberOfGradesString)];
Re: Average program with array and loop and JOptionPane.
No, you need some of them as Strings and some of them as int's. Int's can automatically be casted up to Strings, but not the other way around. Also, Strings don't really have any mathematical operators that can be used with them except '+', which takes the first string and combines it with the second string (ex: what is "hello" / "world" suppose to mean?). Now, the Integer class does provide a handly little method that will perform the necessary conversion from String to int, and you must use this method to do any "real" math that your program needs. The method is called Integer.parseInt().