得到一个arraylist的总和 - java

时间:2018-02-06 03:33:55

标签: java arraylist

我有一个字符串的arraylist,其中csv文件的最后一行是随机数。我需要将它们全部加在一起并返回它,但是无法弄清楚如何这样做,因为现在正在说Cannot make a static reference to the non-static field randomNumber我不想让randomNumber静态,因为它会混淆收集randomNumbers来自数据文件。
我的数据
安德鲁,逗号,comma1aj,83559,
安德鲁,Karpenter,karpe1aj,966796,15.7
布兰登,萝卜,turni1bw,715855,
布伦达,味噌,miso1b,747747,
克里斯,擦鞋,shie1c,123784,
大卫,酷派,Cool1d,200100,
Django的,, uncha1d,830700,
埃里克,塔塔,, 400500,
雅各布,杜利特尔,dooli1j,555777,
托马斯·杰斐逊,jeff1t,951753,
卡亚,咖啡,coffee1k,789456,
欧文·威尔逊,wilso1o,456123,
测试,学生,seeli1p_s ,,
,演示,demo1s,58578,

我的主要班级是

public class Main {
static ArrayList<Student> Students = new ArrayList<Student>();

public static void main(String[] args) throws IOException {
    String fileLocation = getFile();
    File file = new File(fileLocation);
    while(!file.exists()) {
        fileLocation = getFile();
    }

    Scanner scnr = new Scanner(new File(fileLocation));
    while (scnr.hasNextLine()) {
        String text = scnr.nextLine();
        //System.out.println("text = " + text);
        String[] data = text.split(",");
        if(data.length !=4 || data[0].isEmpty() || data[1].isEmpty() || data[2].isEmpty()) {
            continue;
        }
        Students.add(new Student(data[0], data[1], data[2], data[3]));
    }

    for (Student tmp : Students) {
        System.out.println(tmp);
    }

    Student.getSum();

}

public static String getFile() throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter file location: ");
    String fileLocation = reader.readLine();
    //System.out.println(fileLocation);
    return fileLocation;
}
}

我的学生班是

public class Student {
public String fName = "";
public String lName = "";
public String logonID = "";
public String randomNumber = "";

public Student (String fName, String lName, String logonID, String randomNumber) {
    this.setName(fName, lName);
    this.setID(logonID);
    this.setNumber(randomNumber);
}

public Student() {
    fName = "";
    lName = "";
    logonID = "";
    randomNumber = "";
}

public void setName(String firstName, String lastName) {
    this.fName = firstName;
    this.lName = lastName;
}

public void setID(String studentID) {
    this.logonID = studentID;
    logonID.toLowerCase();
}

public void setNumber(String randomNumber) {
    this.randomNumber = randomNumber;
}

public static long getSum() {
    long sum = 0;
    sum = Long.parseLong(randomNumber);
    return sum;
}

@Override
public String toString() {
    return this.fName + " " + this.lName;
}

}

0 个答案:

没有答案