Results 1 to 10 of 10
  1. #1
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Question 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);

    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default 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.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default 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);
    
       }
    }
    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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default 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);

    }
    }

    ]

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default 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?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default 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;

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Help! what im i doing wrong

    That will work too!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. I can't find anything wrong with this but somehow it's wrong.
    By Biscuit Tickler in forum New To Java
    Replies: 2
    Last Post: 09-12-2012, 09:28 PM
  3. What Am I doing Wrong?
    By siren1111 in forum New To Java
    Replies: 3
    Last Post: 12-20-2011, 02:01 AM
  4. what am i doing wrong here??
    By tornbacchus in forum New To Java
    Replies: 19
    Last Post: 04-16-2009, 03:54 AM
  5. what's wrong?
    By rayda in forum New To Java
    Replies: 3
    Last Post: 04-14-2009, 09:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •