3 Replies - 2152 Views - Last Post: 17 November 2010 - 07:07 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:21 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 what it is):

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 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Arrays in JAVA

Posted 17 November 2010 - 12:27 AM

View PostMavado, on 17 November 2010 - 06:21 AM, said:


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);

// ...



This class should be a in a file called largestAvg.java in largest folder. It is also worth mentioning that class names are usually written with the first letter uppercased, so it should be LargestAvg in largest/LargestAvg.java.
Was This Post Helpful? 0
  • +
  • -

#3 Mavado  Icon User is offline

  • New D.I.C Head

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

Re: Arrays in JAVA

Posted 17 November 2010 - 12:59 AM

View PostEdwinNameless, on 16 November 2010 - 11:27 PM, said:

View PostMavado, on 17 November 2010 - 06:21 AM, said:


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);

// ...



This class should be a in a file called largestAvg.java in largest folder. It is also worth mentioning that class names are usually written with the first letter uppercased, so it should be LargestAvg in largest/LargestAvg.java.




Thank you.

Ok, i went ahead and i created the other files but im still getting an error in main from this line:
public class largest.LargestAvg {  //main class

The error said "Class largest is public, should be declared in a file named largest.java"
But i created a file name largest.java.

Here is the file:
package largest;

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

}


im not sure what is wrong now. Im not sure if there is something else i need to put in there.
Was This Post Helpful? 0
  • +
  • -

#4 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Arrays in JAVA

Posted 17 November 2010 - 07:07 AM

I think Mavado was clear:

Quote

This class should be a in a file called largestAvg.java in largest folder. It is also worth mentioning that class names are usually written with the first letter uppercased, so it should be LargestAvg in largest/LargestAvg.java.
.
In the top of your largestAvg class you have this line:
package largest;


Well, there is nothing wrong with this line. It is just that it means that your class file is supposed to be in a folder named 'largest'. If you have smething like:
package first.second.largest;


It would be the same. That is to say your class file is supposed to be in a folder named 'largest' and the 'largest' folder it suppose to be in a folder named second and the second folder it suppose to be in a folder named first.
In the other hand each public class should be in a file which name is the same as the name of the class itself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1