2

I need to preface this with I am not allowed to use an IDE in class, I must use TextPad to compile and run.

I am getting the following errors:

F:\Java\Lab 3\AveragesLab.java:63: error: class, interface, or enum expected public static double averageDbl (double[] arrayDbl) ^ F:\Java\Lab 3\AveragesLab.java:66: error: class, interface, or enum expected double average = 0.0; ^ F:\Java\Lab 3\AveragesLab.java:68: error: class, interface, or enum expected for(int i = 0; i < array.length; i++) ^ F:\Java\Lab 3\AveragesLab.java:68: error: class, interface, or enum expected for(int i = 0; i < array.length; i++) ^ F:\Java\Lab 3\AveragesLab.java:68: error: class, interface, or enum expected for(int i = 0; i < array.length; i++) ^ F:\Java\Lab 3\AveragesLab.java:71: error: class, interface, or enum expected } ^ F:\Java\Lab 3\AveragesLab.java:73: error: class, interface, or enum expected return average; ^ F:\Java\Lab 3\AveragesLab.java:74: error: class, interface, or enum expected } ^ 8 errors Tool completed with exit code 1 

Code

import java.util.Scanner; public class AveragesLab { public static void main (String[] args) { // Create a scanner Scanner input = new Scanner(System.in); // Receive the integer values System.out.print ("In this exercise you will be asked to enter a series of 10 numbers, twice.\n The first set will be integers or whole numbers and the second set will be doubles or \n numbers with a decimal. Please press the enter key after each number.\n"); System.out.print ("Enter 10 integers or whole numbers, for example 75: "); // Create the integer array int arrayInt [] = new int [10]; for (int i = 0; i < arrayInt.length; i++) { arrayInt [i] = input.nextInt(); } int averageInt = average (arrayInt); System.out.println ("\nThe average of the intger array is: "+averageInt); System.out.println (); System.out.println (); // Receive the double values System.out.println ("Enter 10 double amounts including decimals, for example 75.5: "); // Create the double array double arrayDbl [] = new double [10]; for(int i = 0; i < arrayDbl.length; i++) { arrayDbl [i] = input.nextDouble(); } double average = averageDbl (arrayDbl); System.out.printf ("%7.2f\nThe average of the doubles array is: "+average); System.out.println (); System.out.println (); } public static int average (int [] array) { int sum = 0; int average = 0; for (int i = 0; i < array.length; i++) { sum = sum + array [i]; } average = sum / array.length; return average; } } 

The error is below this line

 public static double averageDbl (double [] arrayDbl) { double sum = 0.0; double average = 0.0; for(int i = 0; i < array.length; i++) { sum = sum + array [i]; } average = sum / array.length; return average; } } 

4 Answers 4

1

There is a closing } too much before public static double averageDbl. I saw that average had one too much.

With that the class is closed, and the compiler expects another class/interface.

Sign up to request clarification or add additional context in comments.

Comments

0

The array var in averageDbl doesn't exist. Make the three occurances into arrayDbl.

2 Comments

I changed all the array to arrayDbl and the error is still there.
I took your code from the two sections into Eclipse, made the changes I recommended, and it compiled fine. Is there any code between the two sections? If so, then there is a syntax error there. Probably an extra brace?
0

Assuming you have posted all the code and just split it with the comment about "error is below this line" in half, your parenthesis are not matching.
So " public static double averageDbl (double [] arrayDbl)" is defined outside the class.

Comments

0

This is better:

package homework; /** * AveragesLab description here * @author Michael * @link * @since 2/9/12 9:08 PM */ import java.util.Scanner; public class AveragesLab { public static void main(String[] args) { // Create a scanner Scanner input = new Scanner(System.in); // Receive the integer values System.out.print("In this exercise you will be asked to enter a series of 10 numbers, twice.\n The first set will be integers or whole numbers and the second set will be doubles or \n numbers with a decimal. Please press the enter key after each number.\n"); System.out.print("Enter 10 integers or whole numbers, for example 75: "); // Create the integer array int arrayInt[] = new int[10]; for (int i = 0; i < arrayInt.length; i++) { arrayInt[i] = input.nextInt(); } int averageInt = average(arrayInt); System.out.println("\nThe average of the intger array is: " + averageInt); System.out.println(); System.out.println(); // Receive the double values System.out.println("Enter 10 double amounts including decimals, for example 75.5: "); // Create the double array double arrayDbl[] = new double[10]; for (int i = 0; i < arrayDbl.length; i++) { arrayDbl[i] = input.nextDouble(); } double average = averageDbl(arrayDbl); System.out.printf("The average of the doubles array is %7.2f ", average); System.out.println(); System.out.println(); } public static int average(int[] array) { int sum = 0; int average = 0; for (int i = 0; i < array.length; i++) { sum = sum + array[i]; } average = sum / array.length; return average; } public static double averageDbl(double[] arrayDbl) { double sum = 0.0; double average = 0.0; for (int i = 0; i < arrayDbl.length; i++) { sum = sum + arrayDbl[i]; } average = sum / arrayDbl.length; return average; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.