Chris Frank Chris Frank - 4 months ago 34
Java Question

Java class is public should be declared in a file named

I am writing a program to process a file with weather data. I Scan the file, take the data and place it into arrays, and then print the array. The problem I am having is that I am getting this compiler error:

enter image description here

I have checked my file names, and my public class is the same as my .java file. Besides checking that I have no idea where to go.

Here is my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class WeatherArray {

public static void main(String[] args)
throws FileNotFoundException{

Scanner input = new Scanner(new File("PortlandWeather2011.txt"));

String head = input.nextLine();
String head2 = input.nextLine();
System.out.println(head);
System.out.println(head2);

int count = 0;
while(input.hasNextLine()){
process(input);
count++;
}

double[] prcp = new double[count];
double[] snow = new double[count];
double[] snwd = new double[count];
double[] tmin = new double[count];
double[] tmax = new double[count];

input = new Scanner(new File("PortlandWeather2011.txt"));

head = input.nextLine();
head2 = input.nextLine();
System.out.println(head);
System.out.println(head2);

count = 0;
while(input.hasNextLine()){
input.next();
input.next();
prcp[count] = input.nextDouble();
snow[count] = input.nextDouble();
snwd[count] = input.nextDouble();
tmin[count] = input.nextDouble();
tmax[count] = input.nextDouble();
count++;
}
System.out.println(arrayAvg(prcp));
System.out.println(arrayAvg(snow));
System.out.println(arrayAvg(snwd));
System.out.println(arrayAvg(tmin));
System.out.println(arrayAvg(tmax));
}
public static void process(Scanner input){
while(input.hasNext()){
String station = input.next();

while(input.hasNextInt()){
String date = input.next();

while(input.hasNextInt()){
int prcp = input.nextInt();

while(input.hasNextInt()){
int snow = input.nextInt();

while(input.hasNextInt()){
int snwd = input.nextInt();

while(input.hasNextInt()){
int tmin = input.nextInt();

while(input.hasNextInt()){
int tmax = input.nextInt();
}
}
}
}
}
}
}
}
public static double arrayAvg(double array[]){
int count = 0;
double sum = 0;
for(int i = 0; i < array.length; i++){
count++;
if(array[i] != 393.7){
sum += array[i];
}
}
return sum/count;
}

}

Answer

You have to rename your file to from Main.java to WeatherArray.java or rename the class to public class Main {