Reputation:
Im suppose to write a code using an Array of numbers and apparently not a string and they need to be double. The way I wrote the code, the only way I know how, works. Maybe im making it too complicated and an array would make it simple idk. Im new to programming, new as in a few days. please help.
What is needed is: Write a code that will average the input of 10 numbers and show the avg along with if they pass or fail. if < 50 fail, else > 50 pass.
we have to use an array, we need to use JOptionPane.showMessageDialog., the numbers need to be double and rounded to two decimals.
I declared double but i get an error if i enter a decimal number. If i just run the code as is, it will let me enter 10 numbers, avg them and then tell me if i pass or fail. Im just lost when it comes to using the other factors. thanks
the working code follows:
package avgpassorfail;
import javax.swing.JOptionPane;
public class Avgpassorfail {
public static void main(String[] args) {
String firstNumber,
secondNumber,
thirdNumber,
fourthNumber,
fifthNumber,
sixthNumber,
seventhNumber,
eighthNumber,
ninethNumber,
tenthNumber;
double number1,
number2,
number3,
number4,
number5,
number6,
number7,
number8,
number9,
number10,
sum;
firstNumber =
JOptionPane.showInputDialog ( "Enter 1st Grade" );
secondNumber =
JOptionPane.showInputDialog ( "Enter 2nd Grade" );
thirdNumber =
JOptionPane.showInputDialog ( "Enter 3rd Grade" );
fourthNumber =
JOptionPane.showInputDialog ( "Enter 4th Grade" );
fifthNumber =
JOptionPane.showInputDialog ( "Enter 5th Grade" );
sixthNumber =
JOptionPane.showInputDialog ( "Enter 6th Grade" );
seventhNumber =
JOptionPane.showInputDialog ( "Enter 7th Grade" );
eighthNumber =
JOptionPane.showInputDialog ( "Enter 8th Grade" );
ninethNumber =
JOptionPane.showInputDialog ( "Enter 9th Grade" );
tenthNumber =
JOptionPane.showInputDialog ( "Enter 10th Grade" );
number1 = Integer.parseInt ( firstNumber);
number2 = Integer.parseInt ( secondNumber);
number3 = Integer.parseInt ( thirdNumber);
number4 = Integer.parseInt ( fourthNumber);
number5 = Integer.parseInt ( fifthNumber);
number6 = Integer.parseInt ( sixthNumber);
number7 = Integer.parseInt ( seventhNumber);
number8 = Integer.parseInt ( eighthNumber);
number9 = Integer.parseInt ( ninethNumber);
number10 = Integer.parseInt ( tenthNumber);
sum = (number1 + number2 + number3 + number4 + number5 + number6 + number7 + number8 +number9 + number10)/10;
JOptionPane.showMessageDialog (
null, "The Average is " + sum, "Results",
JOptionPane.PLAIN_MESSAGE);
if (sum < 50){
JOptionPane.showMessageDialog (
null, "Fail", "Results",
JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog (
null, "Pass", "Results",
JOptionPane.PLAIN_MESSAGE);
}
System.exit ( 0 );
}
}
Upvotes: 0
Views: 193
Reputation: 17226
number1 = Integer.parseInt ( firstNumber);
expects to recieve a string that "looks like" an integer, if you give it something that "looks like" anything else you will recieve an error. You should instead use Double.parseDouble(firstNumber);
And yes, you should absolutely use an array for this. The use of arrays allows for a far shorter program;
public class UseArrays {
public static void main(String[] args){
int numberOfEntries=10
double[] numbers =new double[numberOfEntries];
for(int i=0;i< numbers .length;i++){
String option =
JOptionPane.showInputDialog ( "Enter Grade " + i );
numbers[i]=Double.parseDouble(option);
}
double sum=0;
for(int i=0;i< numbers .length;i++){
sum+=numbers[i];
}
double average=sum/10;
System.out.println(average);
}
}
Note also that with the program created in this way it is incredibly easy to change the number of entries, in addition to being much shorter to write. You would not want to type out each entry by hand if you had hundreds, thousands or even millions of entries.
You could make this program even shorter by not storing each double then adding them later (which would also make using the array unnessissary). This program would look like;
public class DontUseArrays {
public static void main(String[] args){
int numberOfEntries=10
double sum=0;
for(int i=0;i< numberOfEntries;i++){
String option =
JOptionPane.showInputDialog ( "Enter Grade " + i );
double number=Double.parseDouble(option);
sum+=number;
}
double average=sum/10;
System.out.println(average);
}
}
Upvotes: 1
Reputation: 30997
To parse your Strings
to the double
type use Double.parseDouble(String s)
You can assign the int
s returned from Integer.parseInt(String s)
to your double
variables, because double
s are higher precision than int
s and so type conversion happens automatically without the need for an explicit cast.
However you can not parse any String
with a decimal point to an int
using Integer.parseInt(String s)
, by definition.
Additional point - There's no need to declare the String
variables, or have a separate variable for each number - it'll be much cleaner to use an array and a loop to input the numbers, and possibly sum the numbers as they come in, something like:
double[] numbers = new double[10];
double sum = 0d;
for(int i = 0; i < numbers.length; i++ ) {
numbers[i] = Double.parseDouble(JOptionPane.showInputDialog ( "Enter 1st Grade" ));
sum += numbers[i];
}
double mean = sum / numbers.length;
Upvotes: 0
Reputation: 9928
What if you had to calculate 1000 grades? That's why we use arrays.
package avgpassorfail;
import javax.swing.JOptionPane;
public class Avgpassorfail
{
public static void main(String[] args)
{
String[] mumStr = new String[10];
double sum = 0;
for (int i = 0; i < numStr.length; i++) //0 to 10 (10 not included)
{
numStr[i] = JOptionPane.showInputDialog ( "Enter Grade " + (i+1)+ ": " );
sum += Double.parseDouble(numStr[i]);
}
double avg = sum / numStr.length;
JOptionPane.showMessageDialog (null, "The Average is " + avg, "Results", JOptionPane.PLAIN_MESSAGE);
if (avg < 50)
{
JOptionPane.showMessageDialog (null, "Fail", "Results", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog (null, "Pass", "Results", JOptionPane.PLAIN_MESSAGE);
}
System.exit ( 0 );
}
}
Upvotes: 0
Reputation: 65831
Here's a fragment of code that should get you on your way:
public void test() {
double[] numbers = new double[10];
double sum = 0;
for ( int i = 0; i < numbers.length; i++ ) {
String number = JOptionPane.showInputDialog ( "Enter Grade "+(i+1) );
numbers[i] = Double.parseDouble(number);
sum += numbers[i];
}
}
Upvotes: 0
Reputation: 59293
Woah there, that's a lot of repetition. You should probably clean this up with loops, like:
double sum = 0; // initialize variable for sum of all the numbers
for (int i = 1; i <= 10; i ++) { // go from 1 to 10
String strNum = JOptionPane.showInputDialog("Enter grade #" + i); // prompt for input
double dNum = Double.parseDouble(strNum); // convert to double
sum += dNum; // add to sum
}
double avg = sum / 10; // now get the average
Upvotes: 1