0

I have an arraylist of strings in which the last row of the csv file is random numbers. I need to sum all of them together and return it as long, but cant figure out how to do so because right now it is saying Cannot make a static reference to the non-static field randomNumber which I do not want to make randomNumber static because it then messes up gathering the randomNumbers from the data file.
My data is
andrew,Comma,comma1aj,83559,
Andrew,Karpenter,karpe1aj,966796,15.7
Brandon,Turnip,turni1bw,715855,
Brenda,Miso,miso1b,747747,
Chris,Shoe,shie1c,123784,
David,Cool,Cool1d,200100,
Django,,uncha1d,830700,
Erik,Tata,,400500,
Jakob,Doolittle,dooli1j,555777,
Thomas,Jefferson,jeff1t,951753,
Kaya,Coffee,coffee1k,789456,
Owen,Wilson,wilso1o,456123,
Test,student,seeli1p_s,,
,demo,demo1s,58578,

My main class is

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

My student class is

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

}
sadist45
  • 11
  • 3
  • 1
    You know, you're actually supposed to do some of your *own* homework. If you keep dumping your assignments here, you'll never learn how to debug or how to code. – Hovercraft Full Of Eels Feb 06 '18 at 03:34
  • 1
    `getSum` can't be `static`, it makes no sense. Instead, loop over the `Students` list – MadProgrammer Feb 06 '18 at 03:35
  • 1
    You may want to review cmu policy [15-150](http://www.cs.cmu.edu/~./15150/policy.html) (and the more general [link](https://www.cmu.edu/policies/student-and-student-life/academic-integrity.html) provided there). – Elliott Frisch Feb 06 '18 at 03:38

0 Answers0