0

I need help to figure out what i'm missing. I've gone though the code and I don't understand why I am getting this error. I new to java and mistakes do tend to happen when learning the syntax please help me fix this return from method.

import javax.swing.JOptionPane;

public class TestScores {


public static void main(String[] args) 
{
  int[] testScores;
  testScores = getTestScores();
  getAverage(testScores);
  double average;


  average = getAverage(); //*method getAverage in class TestScores cannot be
                            *applied to given types;
                            *required: int[]
                            *found: no arguments
                            *reason: actual and formal argument lists differ                     
                            */in length

  displayScores(average);

  System.out.println(testScores[0]);
  System.out.println(testScores[1]);
  System.out.println(testScores[2]);
}
        public static int[] getTestScores()
            {
    int[] scores = new int[3];


        for (int testValues = 0; testValues < scores.length; testValues++)
            {
                String input;

                input =
                        JOptionPane.showInputDialog("What is the test score for number " + (testValues + 1) + " ? ");


                while (input.isEmpty())
                {
                    input = JOptionPane.showInputDialog("Nothing was enterd for test " + (testValues + 1) + " please enter a value.");
                }

                        scores[testValues] = Integer.parseInt(input);


                while (scores[testValues] <= 0 || scores[testValues] > 100)
                    {
                        input =
                                JOptionPane.showInputDialog("The number " + scores[testValues] + " for test " + (testValues + 1) + " is invalid."
                                                            + " Please enter a number in the range of 1 though 100");

                        scores[testValues] = Integer.parseInt(input);
                    }
    }

    return scores;

}

        public static int[] getAverage(int[] input)
        {
            for (int avg = 0; avg < input.length; avg++)
            {
                int total;
                double result;

                total = input[avg];
                result = total / input.length;
            }
            return result; //cannot find symbol
                             *symbol:   variable result
                             */location: class TestScores
        }

        public static void displayScores(double average)
        {
            JOptionPane.showMessageDialog(null, "The average is " + average + ".");

            if (average <= 100 || average >= 90)
            {
                JOptionPane.showMessageDialog(null, "The letter grade is A.");                   
            }
            else if (average < 90 || average <= 80)
            {

                JOptionPane.showMessageDialog(null, "The letter grade is B.");
            }
            else if (average < 80 || average <= 70)
            {

                JOptionPane.showMessageDialog(null, "The letter grade is C.");
            }
            else if (average < 70 || average <= 60)
            {

                JOptionPane.showMessageDialog(null, "The letter grade is D.");
            }
        }
    }
toha
  • 4,097
  • 3
  • 31
  • 49
Daimeon
  • 23
  • 7
  • what error are you currently getting? – hurnhu Oct 14 '16 at 02:39
  • 1
    as per the signature you need to pass an array as an argument to getAverage method . change this line average = getAverage(); to average = getAverage(testScores); – Satya Oct 14 '16 at 02:40
  • i made a comment of the error im getting from viewing the code next to the values – Daimeon Oct 14 '16 at 02:41
  • Changed the line and i got this error "incompatible types: int[] cannot be converted to double" – Daimeon Oct 14 '16 at 02:44
  • @Daimeon Do not post so much code to Stack Overflow. [Strip it down to the bare minimum](https://stackoverflow.com/help/mcve) to show your issue. In doing so, you would likely have solved your problem. Rewriting a minimal case is a good debugging technique, FYI. – Basil Bourque Oct 14 '16 at 02:49
  • then you have to use int, don't use double. otherwise you will need to parse it into double. your variable average must be int too not double. – Polar Oct 14 '16 at 02:50
  • @BasilBourque i didnt know if my code from other classes might have caused this problem. – Daimeon Oct 14 '16 at 02:52
  • @Daimeon Which is why you should write your code slowly, step by step, getting one little thing at a time to work. Compile & run often. – Basil Bourque Oct 14 '16 at 02:54
  • 1
    @BasilBourque: exactly as I stated in a comment to your answer. – Hovercraft Full Of Eels Oct 14 '16 at 02:54
  • @Satya you helped and now the code had compiled correctly i see the issue and fix it Thank You. – Daimeon Oct 14 '16 at 03:00

1 Answers1

2

Out of scope

{
   double result;
   …
}
return result; //cannot find symbol

You define the variable inside the curly braces. Those braces define a block of code. That block has scope. After the closing curly brace, any variables defined within are gone (all references dropped, become candidates for garbage collection). When you say return result, there is no result.

Move that definition outside the braces.

double result;
{
   result = …
   …
}
return result; //cannot find symbol

See: Java, What do curly braces mean by themselves?

Community
  • 1
  • 1
Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
  • Just did and now im getting a error from the variable result might not have been initialized – Daimeon Oct 14 '16 at 02:50
  • Is that an error or a warning? A warning I suspect. You can set a default value for the variable. Or tell your IDE to ignore that rule. `double result = 0;` – Basil Bourque Oct 14 '16 at 02:53
  • 1
    @Daimeon: then you're not creating code correctly. Don't type a large amount of code and then test it. Free association code writing and throwing junk at the wall to see what sticks won't work. Instead if you can't use a modern IDE such as NetBeans or Eclipse (which warns you of compilation issues almost immediately), then it is up to you to compile your code **early and often**, and most importantly **to not add any new code until current compilation issues are fixed**. Else you'll end up with a rat's nest of hard to fix errors. – Hovercraft Full Of Eels Oct 14 '16 at 02:54
  • I am currently using NetBeams and i'm writing the code by methods most of the time just came into this warring and getting frustrated i don't know why – Daimeon Oct 14 '16 at 02:57
  • @BasilBourque Thank you for your help i fixed it. I learned from this mistake. – Daimeon Oct 14 '16 at 03:01
  • @Daimeon Slow down. Google is your friend. Do your coding with a classmate; see [Pair Programming](https://en.wikipedia.org/wiki/Pair_programming). Please search Stack Overflow before posting, your Netbeans messages have already been covered. Do not conflate a compiler error with a compiler warning; warnings may or may not be valid, depending on your context. – Basil Bourque Oct 14 '16 at 03:01
  • @BasilBourque don't have classmates and there is so much to run though in Google. Thank you and sorry for my mistake on what kinds of error i was getting. – Daimeon Oct 14 '16 at 03:04