Java类是市民应在一个名为文件中声明 [英] Java class is public should be declared in a file named

查看:106
本文介绍了Java类是市民应在一个名为文件中声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个程序来处理与气象数据的文件。我扫描的文件,获取数据并将其放入数组,然后打印数组。我遇到的问题是,我得到这个编译器错误:

我已经检查了我的文件名,而我的公共类是一样的我的.java文件。除了检查,我不知道哪里去了。

下面是我的code:

 进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.util.Scanner中;
公共类WeatherArray {公共静态无效的主要(字串[] args)
        抛出FileNotFoundException异常{    扫描仪输入=新的扫描仪(新的文件(PortlandWeather2011.txt));    线头= input.nextLine();
    串HEAD2 = input.nextLine();
    的System.out.println(头);
    的System.out.println(HEAD2);    诠释计数= 0;
    而(input.hasNextLine()){
        进程(输入);
        算上++;
    }    双[] = PRCP新的双[计数]
    双[] =雪新的双[计数]
    双[] = snwd新的双[计数]
    双[] = TMIN新的双[计数]
    双[] Tmax为新的双[计数]    输入=新的扫描仪(新文件(PortlandWeather2011.txt));    头= input.nextLine();
    HEAD2 = input.nextLine();
    的System.out.println(头);
    的System.out.println(HEAD2);    计数= 0;
    而(input.hasNextLine()){
        input.next();
        input.next();
        PRCP [计数] = input.nextDouble();
        雪[计数] = input.nextDouble();
        snwd [统计] = input.nextDouble();
        TMIN [计数] = input.nextDouble();
        TMAX [计数] = input.nextDouble();
        算上++;
    }
    的System.out.println(arrayAvg(PRCP));
    的System.out.println(arrayAvg(雪));
    的System.out.println(arrayAvg(snwd));
    的System.out.println(arrayAvg(TMIN));
    的System.out.println(arrayAvg(TMAX));
}
公共静态无效的过程(扫描仪输入){
    而(input.hasNext()){
        串站= input.next();        而(input.hasNextInt()){
            字符串日期= input.next();            而(input.hasNextInt()){
                INT PRCP = input.nextInt();                而(input.hasNextInt()){
                    INT雪= input.nextInt();                    而(input.hasNextInt()){
                        INT snwd = input.nextInt();                        而(input.hasNextInt()){
                            INT TMIN = input.nextInt();                            而(input.hasNextInt()){
                                INT TMAX = input.nextInt();
                            }
                        }
                    }
                }
            }
        }
    }
}
公共静态双arrayAvg(双数组[]){
    诠释计数= 0;
    双总和= 0;
    的for(int i = 0; I< array.length,我++){
        算上++;
        如果(阵列[I]!= 393.7){
            总和+ =阵列[我]
        }
    }
    返回总和/计数;
}}


解决方案

您必须将文件从 Main.java 重命名为为 WeatherArray。 java的或类重命名为公共类主要{

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:

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

}

解决方案

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

这篇关于Java类是市民应在一个名为文件中声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆