Void-type not allowed here error

advertisements

I am trying to add these data I have read from a file into my map. My map is a treemap TreeMap<String, Student>, where Student in another class. I am trying to use the code map.put(formatSNumber, student.setCourses(courses)); to add the read file elements to my map, but I keep encountering that void type not allowed here error.

sNumber = Integer.parseInt(Breader.readLine());
formatSNumber = String.format("%03d", sNumber);
hours = Integer.parseInt(Breader.readLine());
grade = Double.parseDouble(Breader.readLine());
Student student = map.get(formatSNumber);
Course course = new Course(hours, grade);
List<Course> courses = student.getCourses();
courses.add(course);
map.put(formatSNumber, student.setCourses(courses));
end = Breader.ready();

Here is my full code:

import java.io.*;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.List;
public class FinalProgram {
    public static void main(String[] args) throws IOException {
        String nameFile = " ";
        String classFile = " ";
        TreeMap<String, Student> map = new TreeMap<>();
        Scanner input = new Scanner(System.in);

        try {
            System.out.print("Enter the Name file(c:filename.txt): ");
            nameFile = input.nextLine();
        } catch(IllegalArgumentException e) {
            System.out.printf("Invalid input. Please enter"
                    + " filename in the form of "
                    + "c:filename.txt\n", e.getMessage());
        }
        nameReader(nameFile, map);

        try {
            System.out.print("Enter the Class file(c:filename.txt): ");
            classFile = input.nextLine();
        } catch(IllegalArgumentException e) {
            System.out.printf("Invalid input. Please enter"
                    + " filename in the form of "
                    + "c:filename.txt\n", e.getMessage());
        }
        classReader(classFile, map);
    }

    private static void nameReader(String file, TreeMap<String, Student> map)
            throws IOException {

        String nameFile = file;
        int sNumber = 0;
        String formatSNumber = " ";
        String sName = " ";

        //Instantiate FileReader and BufferedReader
        FileReader freader = new FileReader(nameFile);
        BufferedReader Breader = new BufferedReader(freader);
        boolean end = Breader.ready();

        do {
            sNumber = Integer.parseInt(Breader.readLine());
            formatSNumber = String.format("%03d", sNumber);
            sName = Breader.readLine();
            Student student = new Student(sName);
            map.put(formatSNumber, student);
            end = Breader.ready();
        } while(end);
        Iterator<String> keySetIterator = map.keySet().iterator();
        while(keySetIterator.hasNext()) {
            String key = keySetIterator.next();
            System.out.println("key: " + key + " value: " + map.get(key).getName());
        }
    }
    private static void classReader(String file, TreeMap<String, Student> map)
            throws IOException {
        String classFile = file;
        int sNumber = 0;
        String formatSNumber = " ";
        int hours = 0;
        double grade = 0.0;
        double points = grade * hours;
        double GPA = points / hours;

        //Instantiate FileReader and BufferedReader
        FileReader freader = new FileReader(classFile);
        BufferedReader Breader = new BufferedReader(freader);
        boolean end = Breader.ready();

        do {
            sNumber = Integer.parseInt(Breader.readLine());
            formatSNumber = String.format("%03d", sNumber);
            hours = Integer.parseInt(Breader.readLine());
            grade = Double.parseDouble(Breader.readLine());
            Student student = map.get(formatSNumber);
            Course course = new Course(hours, grade);
            List<Course> courses = student.getCourses();
            courses.add(course);
            map.put(formatSNumber, student.setCourses(courses));
            end = Breader.ready();
        } while(end);

        points = grade * hours;
        GPA = points / hours;
    }
}

Student class:

import java.util.ArrayList;
import java.util.List;
public class Student {
    private String name = " ";
    private List<Course> courses = new ArrayList<>();
    public Student(String name) {
        this.name = name;
    }
    public Student(String name, List courses) {
        this.name = name;
        this.courses = courses;
    }
    public List getCourses() {
        return courses;
    }
    public void setCourses(List courses) {
        this.courses = courses;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

Course class:

public class Course {
    private int hours = 0;
    private double grade = 0.0;
    public Course(int hours, double grade) {
        this.hours = hours;
        this.grade = grade;
    }
    public void setHours(int hours) {
        this.hours = hours;
    }
    public int getHours() {
        return hours;
    }
    public void setGrade(double grade) {
        this.grade = grade;
    }
    public double getGrade() {
        return grade;
    }
}


The idea of put is about putting something into that Map.

More precisely, you typically provide (non-null) key and a value objects.

You are using student.setCourses(courses) as argument for that "value" parameter that put() expects.

That argument is an expression. And the result of that expression would be the result of the method call.

That method is defined to not return anything (void that is).

Obviously nothing is not the same as something. And that is what the compiler tries to tell you.

Two solutions:

  1. pass a Student object
  2. change that method setCourses()

Like this:

Student setCourses(... {
....
  return this;
}

( you better go for option 1; 2 is more of a dirty hack, bad practice in essence )