Results 1 to 10 of 10
Thread: Help! what im i doing wrong

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 Autogenerated 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);
}
}
 09082013, 07:05 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,924
 Rep Power
 4
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.

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.
 09082013, 07:40 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,924
 Rep Power
 4
Re: Help! what im i doing wrong
Java 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); } }
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.

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 Autogenerated 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);
}
}
]
 09082013, 10:30 PM #6Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,924
 Rep Power
 4
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.

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?
 09082013, 11:04 PM #8Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,924
 Rep Power
 4
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.

Re: Help! what im i doing wrong
what about if i use this
Java Code:double Sum = 0,Average, Smallest= Double.POSITIVE_INFINITY, Largest=Double.NEGATIVE_INFINITY ,Range = 0,Value = 0;
 09092013, 01:07 AM #10Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,924
 Rep Power
 4
Re: Help! what im i doing wrong
That will work too!
Regards,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
Similar Threads

Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06112013, 01:37 AM 
I can't find anything wrong with this but somehow it's wrong.
By Biscuit Tickler in forum New To JavaReplies: 2Last Post: 09122012, 09:28 PM 
What Am I doing Wrong?
By siren1111 in forum New To JavaReplies: 3Last Post: 12202011, 02:01 AM 
what am i doing wrong here??
By tornbacchus in forum New To JavaReplies: 19Last Post: 04162009, 03:54 AM 
what's wrong?
By rayda in forum New To JavaReplies: 3Last Post: 04142009, 09:07 PM
Bookmarks