Java不兼容类型:inputstream无法转换为扫描程序

时间:2017-02-05 04:48:49

标签: java arrays arraylist java.util.scanner inputstream

当我尝试使用此代码执行程序时,我得到的不兼容类型inputstream无法转换为扫描程序错误。问题出在哪里?

private ArrayList<Student> readFile() throws FileNotFoundException {
            readOnCampusStudent (in) ArrayList<Student> studentList = new ArrayList<Student>();
            //info.add(type, id, lname, fname, resstat, prfee, credits);
            String fName = "p02-students.txt";
            Scanner in = new Scanner(new File(fName));
            String studentType;
            while (in.hasNext()) {
               // studentList.add(type, id, lname, fname, resstat, prfee, credits);
                if ("C".equals(in.next())) {
                    studentList.add(readOnCampusStudent(in));
                }
                studentList.add(readOnlineStudent(in));
                in.nextLine();
            }
            in.close();
            return studentList;
        }

该计划有四个班级

MainClass

    public class Main {
        public static void main(String[] args) {
            new Main().run();
        }

        private onlineStudent readOnlineStudent(Scanner pIn) {
            int id;
            String lname, fname;
            id = pIn.nextInt();
            lname = pIn.next();
            fname = pIn.next();
            onlineStudent OnlineStudent = new onlineStudent(id, fname, lname) {
                @Override
                public void calcTuition()
                {
                }           
            };
            return OnlineStudent;
        }

        private void calcTuition(ArrayList<Student> pStudentList) {
            for (Student s : pStudentList) {
                s.calcTuition();
            }
        }

        private void run() {
            ArrayList<Student> studentList = new ArrayList<Student>();
            try {
                studentList = readFile();
                calcTuition(studentList);
                Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING);
                writeFile(studentList);
            } catch (FileNotFoundException e)
            {
                System.err.println("Sorry, could not open 'p02-students.txt' for reading.");
                System.exit(-1);
            }
        }

        private onCampusStudent readOnCampusStudent(Scanner pIn) {
            int id = pIn.nextInt();
            String lname = pIn.next();
            String fname = pIn.next();
            onCampusStudent OnCampusStudent = new onCampusStudent(id, fname, lname) {
                @Override
                public void calcTuition() {
                }
            };
            String res = pIn.next();
            double fee = pIn.nextDouble();
            int credits = pIn.nextInt();
            if (res.equals("R")) {
                OnCampusStudent.setResidency(true);
            } else {
                OnCampusStudent.setResidency(false);
            }
            OnCampusStudent.setProgramFee(fee);
            OnCampusStudent.setCredits(credits);
            return OnCampusStudent;
        }

        private ArrayList<Student> readFile() throws FileNotFoundException {
            readOnCampusStudent (in) ArrayList<Student> studentList = new ArrayList<Student>();
            //info.add(type, id, lname, fname, resstat, prfee, credits);
            String fName = "p02-students.txt";
            Scanner in = new Scanner(new File(fName));
            String studentType;
            while (in.hasNext()) {
               // studentList.add(type, id, lname, fname, resstat, prfee, credits);
                if ("C".equals(in.next())) {
                    studentList.add(readOnCampusStudent(in));
                }
                studentList.add(readOnlineStudent(in));
                in.nextLine();
            }
            in.close();
            return studentList;
        }

        private void writeFile(ArrayList<Student> pStudentList) throws FileNotFoundException {
            File file = new File("p02-tuition.txt");
            PrintWriter out = new PrintWriter(file);
            for (Student s : pStudentList) {
                out.print(s.getId() + " " + s.getLName() + " " + s.getFName());
                out.printf("%.2f%n", s.getTuition());
            }
            out.close();
        }
    }

具有2个子类的学生班

public abstract class Student implements Comparable<Student> {
        private int mCredits;
        private String mFname;
        private String mLname;
        private int mId;
        private double mTuition;

        public Student(int pId, String pFname, String pLname) {
            mId=pId;
            mFname = pFname;
            mLname = pLname;
        }

    //Implement this method in the subclass
    //of student class
        public abstract void calcTuition();

    //Set credits of student
        public void setCredits(int pCredits) {
            mCredits = pCredits;
        }

    //Returns the credits of student
        public int getCredits() {
            return mCredits;
        }

    //Set first name
        public void setFname(String pFname) {
            mFname = pFname;
        }

    //Returns first name
        public String getFirstName() {
            return mFname;
        }

    //Set id
        public void setid(int pId) {
            mId = pId;
        }

    //Retunrns id
        public int getid() {
            return mId;
        }

    //Set last name
        public void setLname(String pLname) {
            mLname = pLname;
        }

    //Returns last name
        public String getLastName() {
            return mLname;
        }

    //Returns the tuition fee
        public void setTuition(double pTuition) {
            mTuition = pTuition;
        }

    //Returns the tuition fee
        public double getTuition() {
            return mTuition;
        }

        @Override
        public int compareTo(Student pStudnet) {
            if (getid() < pStudnet.getid()) {
                return -1;
            } else if (getid() > pStudnet.getid()) {
                return 1;
            } else {
                return 0;
            }
        }

        int getId() {
            return mId;
        }

        String getLName() {
            return mLname;
        }

        String getFName() {
            return mFname;
        }

        void setResidency(boolean b) {
        }

        void setProgramFee(double fee) {
        }
    }

    abstract class onCampusStudent extends Student
    {
        boolean mResident;
        double mProgramFee;

        onCampusStudent(int pId, String pFname, String pLname)
        {
            super(pId,pFname,pLname);
        }
    }

    abstract class onlineStudent extends Student
    {
        boolean mResident;
        double mProgramFee;

        onlineStudent(int pId, String pFname, String pLname)
        {
             super(pId,pFname,pLname);
        }
    }

分拣机类

public class Sorter {

    public static final int SORT_ASCENDING = 0;
    public static final int SORT_DESCENDING = 1;

    /**
    * Sorts pList into ascending (pOrder = SORT_ASCENDING) or descending
    * (pOrder = SORT_DESCENDING) order using the insertion sort algorithm.
    */
    public static void insertionSort(ArrayList<Student> pList, int pOrder) {
        for (int i = 1; i < pList.size(); ++i) {
            for (int j = i; keepMoving(pList, j, pOrder); --j) {
                swap(pList, j, j - 1);
            }
        }
    }

    /**
    * Returns true if we need to continue moving the element at pIndex until it
    * reaches its proper location.
    */
    private static boolean keepMoving(ArrayList<Student> pList, int pIndex, int pOrder) {
        if (pIndex < 1) {
            return false;
        }
        Student after = pList.get(pIndex);
        Student before = pList.get(pIndex - 1);
        return (pOrder == SORT_ASCENDING) ? after.compareTo(before) < 0 : after.compareTo(before) > 0;
    }

    /**
    * Swaps the elements in pList at pIndex1 and pIndex2.
    */
    private static void swap(ArrayList<Student> pList, int pIndex1, int pIndex2) {
        Student temp = pList.get(pIndex1);
        pList.set(pIndex1, pList.get(pIndex2));
        pList.set(pIndex2, temp);
    }
}

TuitionConstants Class

public class TuitionConstants {
    public static final int ONCAMP_ADD_CREDITS = 350;
    public static final int MAX_CREDITS = 18;
    public static final int ONCAMP_NONRES_BASE = 12200;
    public static final int ONCAMP_RES_BASE = 5500;
    public static final int ONLINE_CREDIT_RATE = 875;
    public static final int ONLINE_TECH_FEE = 125;
}

P02-students.txt

C 8230123345450 Flintstone Fred R 0 12 
C 3873472785863 Simpson Lisa N 750 18 
C 4834324308675 Jetson George R 0 20 
O 1384349045225 Szyslak Moe - 6
O 5627238253456 Flanders Ned T 3

2 个答案:

答案 0 :(得分:0)

这些代码行都在你的主要类中:

import static java.lang.System.in;

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

你有两个叫做“in”的东西。这可能会导致错误 - 它是否被导入的“in”(这是一个InputStream)或变量“in”(它是一个Scanner)感到困惑。我建议你不要导入java.lang.System .in并且只要在想要使用那个时输入System.in。然后它不会与你的变量发生冲突。

答案 1 :(得分:0)

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

我没有收到您之前提到的错误,但由于您没有正确读取数据,我收到输入不匹配错误。

例如,您试图存储&#34; 8230123345450&#34;进入 int 变量。根据java标准, int 变量的值应介于2147483647和-2147483648之间。因此,我建议您使用 long 类型变量或其他String类型变量来存储它。

private onCampusStudent readOnCampusStudent(Scanner pIn) {

        String id = pIn.next();

        String lname = pIn.next();

        String fname = pIn.next();

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

我打印出了价值观,并且对每个学生都很好。不要忘记更改学生班级中 mid 变量的数据类型。