Arrays in JAVA

Page 1 of 1

1 Replies - 335 Views - Last Post: 17 November 2010 - 12:29 AM Rate Topic: -----

#1 Mavado  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 14-April 09

Arrays in JAVA

Posted 17 November 2010 - 12:19 AM

Good day. I am new to JAVA and i'm having this problem.

I wrote this code in NetBeans but for some reason it is not working how i want it to. When i try to run it, i would get this error: "java.lang.RuntimeException: Uncompilable source code - class largestAvg is public, should be declared in a file named largestAvg.java
at largest.largestAvg.<clinit>(Main.java:25)
Could not find the main class: largest.largestAvg."

I went ahead and created the file name largestAvg.java but for some reason it is still not working.

This is what i have some far:

/*Write a program that reads integers, finds the largest of them, and counts
 its occurrences. Assume that the input ends with number 0. Suppose that you
 entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and the occurrence
 count for 5 is 4. (Hint: Maintain two variables, max and count. max stores the
 current max number, and count stores its occurrences. Initially, assign the first
 number to max and 1 to count. Compare each subsequent number with max. If the
 number is greater than max, assign it to max and reset count to 1. If the number
 is equal to max, increment count by 1.)

Here are sample runs of the program:
Sample 1:
Enter numbers: 3 5 2 5 5 5 0
T
he largest number is 5 The occurrence count of the largest number is 4*/

package largest;

import java.util.Scanner; //import scanner into JAVA app


public class largestAvg {  //main class

    private static final int SIZE = 5;  //set SIZE to a final var

    public static void main(String[] args) {  // main method begins execution of Java application

        Scanner enter = new Scanner(System.in);

        double[] num = new double[SIZE];
        double sum = 0;

        System.out.print("Please enter " + SIZE + " numbers : ");   //asked the user to enter the 5 numbers

        // Allow the user to enter in the values.

        for (int count = 0; count < SIZE; count++) {
            num[count] = enter.nextDouble();
            sum += num[count];
        }
        System.out.print("The average of ");
        for (int count = 0; count < SIZE - 1; count++) {
            System.out.print(num[count] + " , ");
        }

        // No comma following last element
        System.out.println(num[SIZE - 1] + " is " + sum / SIZE);
    }

}




This is the file i have created ( i think something is wrong with the file but im not sure):

package largest;

/**
 *
 * @author _
 */
public class largestAvg {

    int largestAvg;


}


I dont think i have declared it right but im not sure how to declare it.

Please help!

Thank you so much in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays in JAVA

#2 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Arrays in JAVA

Posted 17 November 2010 - 12:29 AM

Please don't double-post: http://www.dreaminco...arrays-in-java/

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1