Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 48

Thread: Need help

  1. #21
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    Not sure.whats that line?
    After trying a lot of things to be honest with you i'm lost.
    I think i did the avg but I still have a line under "return"
    I don't know whats the prob;/

    Java Code:
    public class LabAssig8 {
    
        public static void main(String[] args) {
            getUserInput();
        }
    
        public static double[] getUserInput() {
            int[] scores = new int[4];
    
    
            for (int i = 0; i <= scores.length; i++) {
                String numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
    
                double score = Double.parseDouble(numT);
    
                while (score <= 0 || score >= 100) {
                  numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
                  score = Double.parseDouble(numT);
                }
            }
    
            return ();
        }
    
    
    public static void calculateAverage(double[] scores) {
            double sum = 0;
    
            for (int i = 0; i <= scores.length; i++)
                sum = sum + scores[i];
            double avg = sum / scores.length;
    
            JOptionPane.showInputDialog("The averageof 5 test scores is " + avg);
    
    }
    
            public static void
    findHighLow(double[] tests) {
                
        }
    }
    Thats what I have soo far. and I don't think I can get any further lol

  2. #22
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your average method looks correct. You are very close on the getUserInput() method.

    Your loop doesn't actually do anything but collect the data and then forget about it. You are supposed to return an array of user input data, so you need a return statement. You are also supposed to be using a double[] not an int [].

  3. #23
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    ohhh now I got it. I wasn't payin attention to the int[], I had return(scores); but it wasn't working because I had int[] not double. I'm right right? haha
    ok I tested it and its working fine but instead of show enter score 1,2,3,4,5... it shows enter score 1.0, 2.0,.... why?
    and how am I suppose to display avg?
    thanks soooo much for being patience

  4. #24
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Show me your most up to date code. It should be saying "Please enter score 1", 2, 3, since i is the variable you are using and i is an int.

  5. #25
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    Got it fixed
    now the problem is displaying the avg. I think I calculated it right but Its not displaying
    here's my code
    Java Code:
    public class LabAssig8 {
    
        public static void main(String[] args) {
            double[] r;
           r= getUserInput();
            calculateAverage(r);
        }
    
        public static double[] getUserInput() {
            double[] scores = new double[4];
    
    
            for (int i = 0; i <= scores.length; i++) {
                String numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
    
                double score = Double.parseDouble(numT);
    
                while (score <= 0 || score >= 100) {
                    numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
                    score = Double.parseDouble(numT);
    
                }
            }
    
            return (scores);
        }
    
        public static void calculateAverage(double[] scores) {
            double sum = 0;
    
            for (int i = 0; i <= scores.length; i++) {
                sum = sum + scores[scores.length];
            }
            double avg = sum / scores.length;
    
            JOptionPane.showInputDialog("The averageof 5 test scores is " + avg);
    
            System.out.println(avg);
        }
    
        public static void findHighLow(double[] tests) {
        }
    }

  6. #26
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There is still 1 big flaw, and 1 minor nitpick. The array only contains 4 elements. When you declare an array you specify the size in the brackets, not the max index it will contain.
    Java Code:
    int[] x = new int[4]; //has 4 elements, 0, 1, 2, 3
    int[]x = new int[6]; //has 6 elements, 0, 1, 2, 3, 4, 5
    Now for the big flaw. It's a small mistake but causes big problems. While I can give you a fairly good statement, I need you to be the one that actually figures it out.

    Your code does nothing with the user input. All it does is collect it, parse it, and then move onto the next prompt. It never "saves" it. Try this
    Java Code:
    double[] r = getUserInput();
    for(int i = 0; i < r.length; i++){
      System.out.println(r[i]);
    }
    put that in your main method and see what is printed.

  7. #27
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    it gave me
    Java Code:
    run:
    0.0
    0.0
    0.0
    0.0
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
            at LabAssig8.calculateAverage(LabAssig8.java:46)
            at LabAssig8.main(LabAssig8.java:19)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 7 seconds)

  8. #28
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, Im assuming you entered values in for each input. Why do you think all the values in the array are 0?

  9. #29
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    causae r = 0?
    I don't know haha kinda confusing

  10. #30
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    But r got it's input from you, how come it never got added? Shouldn't the array that getUserInput() returned be filled with the user input? Why isn't it filled? Did you fill the returned array before returning it?

  11. #31
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    let me guess. i'm missing count++ ?
    If yes its probably cause i don;t know how to use it :)

  12. #32
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    haha I'm tired of this. please tell me i'm a bit close to the end?
    I've been workin on it since ever. like 5 hours every day for the past 3 days
    My brain is damaged hahaha

  13. #33
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are very close to the answer. In the getUserInput() method you are never populating the array you are returning. The population should occur in the for loop(after you get and verify user input.)

  14. #34
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    My brain is about to explode haha
    Sorry but I really didn't get it.
    are we still working with getting the input?
    I thought its right.

  15. #35
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    We are still working on getting the user input. Since it doesn't actually get user input. The method may as well say
    Java Code:
    public static double[] getUserInput(){
      double[] x = new double[4];
      return x;
    }
    This is all your code is doing. The loop is really just asking the user for information and doing NOTHING WITH THE INPUT.

  16. #36
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    so whats the solution?

  17. #37
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The solution is to store the user input in the array. Not going to give you the direct answer, sorry.

    the input should be stored in the for loop.

  18. #38
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    when you say input you mean numT right?

  19. #39
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Perhaps. I mean the input you got and parsed from the user.

  20. #40
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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