[Solved] Java Incompatible types: inputstream cannot be converted to scanner


    private ArrayList<Student> readFile() throws FileNotFoundException {

        String fName = "p02-students.txt";

        Scanner scan = new Scanner(new File(fName));

        ArrayList<Student> studentList = new ArrayList<Student>();

        while (scan.hasNext()) {

             String studentType = scan.next();

            if (studentType.equals("C")) {
                studentList.add(readOnCampusStudent(scan));
            } else {
                studentList.add(readOnlineStudent(scan));
            }

            scan.nextLine();
        }

        scan.close();
        return studentList;
    }

I didn’t get the error you mentioned earlier, but I was getting input mismatch error because you weren’t reading the data correctly.

For example, you were trying to store “8230123345450” in to an int variable. As per java standards, the value of int variable should be between 2147483647 and -2147483648. Therefore, I would suggest you to use a long type variable or another String type variable to store it.

private onCampusStudent readOnCampusStudent(Scanner pIn) {

        String id = pIn.next();

        String lname = pIn.next();

        String fname = pIn.next();

        System.out.println(id + " " + lname + " " + fname);
    }

I printed out the values, and it worked fine for every student. Don’t forget to change the data type of mid variable in student class.

2

solved Java Incompatible types: inputstream cannot be converted to scanner