Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Code won't compile

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Code won't compile

    Hello all! i'm trying to complete the following Java programming assignment, which is late because it won't compile. The prof has not been helpful and the TA is nowhere to be found. I'm using two classes. For the life of me, I cannot figure out what is wrong. Please help! Here's the assignment:

    Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:
    • getTotal. This method should accept a one- dimensional array as its argument and return the total of the values in the array.
    • getAverage. This method should accept a one- dimensional array as its argument and return the average of the values in the array.
    • getHighest. This method should accept a one- dimensional array as its argument and return the highest value in the array.
    • getLowest.
    This method should accept a one- dimensional array as its argument and return the lowest value in the array. Demonstrate each of the methods in the program.

    [java=highlight]

    import javax.swing.JOptionPane;

    public class TestScores
    {
    public static void main (String [] args)
    {
    double [] scores = { 100, 80, 85, 92, 95, 72, 59, 77, 96, 88 }; //Elements in array

    //Create an array that holds ten test scores.
    double [] scores = new double[scores];

    //Get total of test scores
    getValues(scores);

    //Create TestData object and initialize it with test data.
    TestData total = new TestData(scores);

    //Display the following test data: total of test scores,
    //average score, highest score, lowest score.
    JOptionPane.showmessageDialog(null,
    "The total of all test scores is " +
    scores.getTotal() +
    "/nThe average test score is " +
    scores.getAverage() +
    "/nThe highest test score is " +
    scores.getHighest() +
    "nThe lowest test score is " +
    scores.getLowest());
    System.exit(0);
    }
    }

    public class TestData
    {
    private double [] scores; //The test data

    /**A constructor that copies the array elements
    * to the tests array.
    @param s Array elements copied.*/

    public TestData(double [] s)
    {
    //Create an array the same size as s.
    scores = new double[s.length];

    //Copy the array elements from s to tests.
    for (int index = 0; index < s.length; index ++)
    scores[index] = s[index];
    }

    /**getTotal method
    * @return The total of test scores in the array.*/

    public double getTotal()
    {
    int total = 0; //Initialize the accumulator

    //Accumulate the sum of the test scores in the array.
    for (int index = 0; index < scores.length; index++)
    total += scores[index];

    //Return total
    return total;
    }
    /** getAverage method
    * @return The average of all test scores in the array.
    */

    public double getAverage()
    {
    return getTotal()/(scores.length);
    }
    /**getHighest method
    * @return the highest value test score in the array.
    */

    public double getHighest()
    {
    double highest = scores[0];

    for (int index = 1; index < scores.length; index++)
    {
    if (scores[index] > highest);
    highest = scores[index];
    }
    return highest;
    }

    /**getLowest method
    * @return The lowest test score in the array.
    */
    public double getLowest()
    {
    double lowest = scores[0];

    for (int index = 1; index < scores.length; index++)
    {
    if (scores[index] < lowest)
    lowest = scores[index];
    }
    return lowest;
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Code won't compile

    it won't compile
    What are the compiler messages? If you post them and say which lines of your code they are referring to someone may be able to explain what they mean.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Code won't compile

    Apologies, I'm new to this forum. Here are the compiler messages. I'm using Dr. Java, which is what the school requires. All of the error messages are for the TestScores class.

    8 errors found:
    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 16]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:16: scores is already defined in main(java.lang.String[])
    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 16]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:16: incompatible types
    found : double[]
    required: int
    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 19]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:19: cannot find symbol
    symbol : method getValues(double[])
    location: class TestScores
    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 26]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:26: cannot find symbol
    symbol : method showmessageDialog(<nulltype>,java.lang.String)
    location: class javax.swing.JOptionPane
    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 28]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:28: cannot find symbol
    symbol : method getTotal()
    location: class double[]
    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 30]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:30: cannot find symbol
    symbol : method getAverage()
    location: class double[]
    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 32]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:32: cannot find symbol
    symbol : method getHighest()
    location: class double[]
    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 34]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:34: cannot find symbol
    symbol : method getLowest()
    location: class double[]

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Code won't compile

    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 16]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:16: scores is already defined in main(java.lang.String[])
    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 16]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:16: incompatible types
    found : double[]
    required: int
    These refer to the fact that you have declared the scores array twice:

        // you declare scores here...
    double [] scores = { 100, 80, 85, 92, 95, 72, 59, 77, 96, 88 }; //Elements in array
     
    //Create an array that holds ten test scores.
        // ... and again here
    double [] scores = new double[scores];

    Change this so that you only declare the array once. You might want to consult your textbook for how to declare an array and put specific values into it.

    File: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java [line: 19]
    Error: F:\SCSU Classwork Spring 2012\Java 2\TestScores.java:19: cannot find symbol
    symbol : method getValues(double[])
    location: class TestScores
    The compiler is saying that there is no getValues() method defined anywhere.

    I suggest you swap the order of these two:

    //Get total of test scores
    getValues(scores);
     
    //Create TestData object and initialize it with test data.
    TestData total = new TestData(scores);

    ie create the TestData instance before you get the total of the test scores. This makes sense because, until you have created the TestData instance there is nothing to get the total of.

    Also give the TestData instance a reasonable name. total is not a reasonable name for something that represents a set of data. data is a better name, or test. In any case you will use this name later when you call the methods you have written: data.getTotal(), data.getAverage() etc.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    JavaChallenged (February 12th, 2012)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code won't compile

    Which of the error messages don't you understand?
    scores is already defined <<< the compiler sees that scores has already been defined.
    cannot find symbol <<< the compiler can not find the symbol whose name is mentioned in the error message
    incompatible types
    found : double[] <<< The compiler found this data type but its the wrong one
    required: int <<< you should use an int not a double[]

  7. The Following User Says Thank You to Norm For This Useful Post:

    JavaChallenged (February 12th, 2012)

  8. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Code won't compile

    Many thanks to pbrockway and Norm for your help. I have difficulty interpreting the compiler messges because, in my limited experience, the message does not always refer to the exact problem. Sometimes, the problem lies in an earlier part of the script.

    Having said that, I still have 3 error messages for the following code. I'm sure it's an obvious fix, and I've looked at my textbook and tried changing the keywords, but nothing has worked:

    [java=highlight]

    JOptionPane.showMessageDialog(null,
    "The average test score is " +
    average);
    JOptionPane.showMessageDialog(null,
    "The highest test score is " +
    highest);
    JOptionPane.showMessageDialog(null,
    "The lowest test score is " +
    lowest);
    [/highlight]

    The error messages are:

    TestScores.java:25: cannot find symbol
    symbol : variable average
    location: class TestScores


    TestScores.java:28: cannot find symbol
    symbol : variable highest
    location: class TestScores

    TestScores.java:31: cannot find symbol
    symbol : variable lowest
    location: class TestScores

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code won't compile

    Now you have three variables that the compiler can not find definitions for.
    Where are these variables defined?
    If they are defined in one method, you can not see and use them in another. Their definitions must be at the class level for multiple methods to use them.

Similar Threads

  1. Code won't compile, but can't find the error....
    By RockDoc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 02:09 AM
  2. Cannot get to compile
    By theoneyouenvy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 19th, 2011, 10:17 PM
  3. Cannot get to compile
    By Goff256 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 19th, 2011, 01:09 PM
  4. [SOLVED] Picture won't go with the .jar (Wrong code or compile error?)
    By Fermen in forum Object Oriented Programming
    Replies: 3
    Last Post: March 15th, 2011, 05:22 PM
  5. *why won't this compile?*
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 2nd, 2010, 07:18 PM