Help! what im i doing wrong

Write a Java program that asks a user to input a number of values of type double and then prints:

the average of the values

the smallest of the values

the largest of the values

the range, that is, the difference between the smallest and the largest of the values

The user is allowed to enter either positive or negative values. An entry of "end" or "stop" indicates there are no more values to be input.

Use a loop to repeatedly ask a user for input values. To get the inputs, use JOptionPane, which returns a String. Test the String for the stopping words and, if there is no match, assume the input is a number of type double. Convert the String to a double using

double value = Double.parseDouble(input);

// I imported JOption Pane

import javax.swing.JOptionPane;

public class Lab5 {

public static void main(String[] args) {

// TODO Auto-generated method stub

//Set string to input

String input = " ";

int counter = 0;

double Sum = 0,Average, Smallest =0 , Largest =0,Range = 0,Value = 0;

while(!input.equals("end"))

{

input = JOptionPane.showInputDialog("Plese Input a number of values.");

if(input.equals("end"))

{

break;

}

Sum = Sum + Value;

counter++;

Largest = Double.MAX_VALUE;

Smallest = Double.MIN_VALUE;

}

Average = Sum / counter;

Range = Largest - Smallest;

JOptionPane.showMessageDialog(null, "The average of the Values:" +" " + Average);

JOptionPane.showMessageDialog(null, "The Smallest of the Values:" +" " + Smallest);

JOptionPane.showMessageDialog(null, "The Largest of the Values:" +" " + Largest);

JOptionPane.showMessageDialog(null, "The Range of the Values:" +" " + Range);

}

}

Re: Help! what im i doing wrong

The first thing you're doing wrong is not telling us exactly what problem you are having. Also, please place your code in between [CODE][/CODE] tags.

Regards,

Jim

Re: Help! what im i doing wrong

well when i write run the application it doesnt tell me the correct average or the maximum or the minimum or the range. and i apologize that was my first post ever and i am two weeks into my freshman year of college and computer science is new to me.

Re: Help! what im i doing wrong

Code:

`// I imported JOption Pane`

import javax.swing.JOptionPane;

public class Lab5 {

public static void main(String[] args) {

// Set string to input

String input = " ";

int counter = 0;

double Sum = 0, Average, Smallest = 0, Largest = 0, Range = 0, Value = 0;

while (!input.equals("end")) {

input = JOptionPane.showInputDialog("Plese Input a number of values.");

if (input.equals("end")) {

break;

}

Sum = Sum + Value;

counter++;

Largest = Double.MAX_VALUE;

Smallest = Double.MIN_VALUE;

}

Average = Sum / counter;

Range = Largest - Smallest;

JOptionPane.showMessageDialog(null, "The average of the Values:" + " "

+ Average);

JOptionPane.showMessageDialog(null, "The Smallest of the Values:" + " "

+ Smallest);

JOptionPane.showMessageDialog(null, "The Largest of the Values:" + " "

+ Largest);

JOptionPane.showMessageDialog(null, "The Range of the Values:" + " "

+ Range);

}

}

OK. I went ahead and placed your code between code tags.

First, you are not converting the input to a value. You are simply summing up the initialized value of 0 which does not change.

Second, I believe the instructor wants you to determine the min and max of the values you enter and use that as the range. Not the absolute maximum or minimum of a double.

Regards,

Jim

Re: Help! what im i doing wrong

Okay so i tried to put it in tags this time. Sorry if im not doing it right. but thanks for the all the help. now i did it but it only works if at least one of the inputs is a negative integer. How could i make it work for both all positve, positive negative, or all negative integers?

[// I imported JOption Pane

import javax.swing.JOptionPane;

public class Lab5 {

public static void main(String[] args) {

// TODO Auto-generated method stub

//Set string to input

String input = " ";

int counter = 0;

// Named all the double integers

double Sum = 0,Average, Smallest =0 , Largest =0,Range = 0,Value = 0;

//while loop

while(!input.equals("end"))

{

input = JOptionPane.showInputDialog("Plese Input a number of values.");

//set the brake

if(input.equals("end"))

{

break;

}

//Finding the largest and smallest values

Value = Double.parseDouble(input);

Sum = Sum + Value;

counter++;

if(Double.parseDouble(input) >= Largest)

{

Largest = Double.parseDouble(input);

}

if(Double.parseDouble(input) <= Smallest)

{

Smallest = Double.parseDouble(input);

}

}

Average = Sum / counter;

Range = Largest - Smallest;

JOptionPane.showMessageDialog(null, "The average of the Values:" +" " + Average);

JOptionPane.showMessageDialog(null, "The Smallest of the Values:" +" " + Smallest);

JOptionPane.showMessageDialog(null, "The Largest of the Values:" +" " + Largest);

JOptionPane.showMessageDialog(null, "The Range of the Values:" +" " + Range);

}

}

]

Re: Help! what im i doing wrong

Think about this for a second. You initialize Largest and Smallest to zero. But actually if you want to find what is the largest number you should initialize Largest to a small number. And you have already used that number earlier. Same for Smallest number.

Also, check out the link in my signature below for naming conventions. Variables, fields and methods should start with lower case. Classes with upper case.

Regards,

Jim

Re: Help! what im i doing wrong

Alright thanks you are very helpful. but if i set the largest number to a small number and the smallest number to a really large number wouldnt that be considered as hard coding?

Re: Help! what im i doing wrong

No more than setting the values to 0 to start with. There is another way though. If count is equal to 1, that is, you have only entered one number. Then simply initialize both largest and smallest to that number. That way you don't depend on any outside value.

Regards,

Jim

Re: Help! what im i doing wrong

what about if i use this

Code:

`double Sum = 0,Average, Smallest= Double.POSITIVE_INFINITY, Largest=Double.NEGATIVE_INFINITY ,Range = 0,Value = 0;`

Re: Help! what im i doing wrong

That will work too!

Regards,

Jim