Java 不兼容的类型:输入流无法转换为扫描仪
Posted
技术标签:
【中文标题】Java 不兼容的类型:输入流无法转换为扫描仪【英文标题】:Java Incompatible types: inputstream cannot be converted to scanner 【发布时间】:2017-06-22 06:22:23 【问题描述】:当我尝试使用此代码执行程序时,我得到不兼容类型输入流无法转换为扫描仪错误。问题可能出在哪里?
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;
程序有四个类
主类
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 类
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
【问题讨论】:
问题不清楚,请补充详细信息。 您的代码中的问题不仅仅是不兼容的类型...readOnCampusStudent (in) ArrayList<Student> studentList = new ArrayList<Student>();
- 这条线到底是什么?
@CRazyProgrammer 该程序有四个类,但这就是我得到错误的地方。如果你愿意,我可以放四门课的代码。程序应该读取 p02-students.txt 的内容并计算每个学生的学费。然后程序应该将学费结果写入一个名为 p02-tuition.txt 的输出文件格式化。
@D M 那是什么带来了错误哥们..我该怎么说?
【参考方案1】:
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;
我没有收到您之前提到的错误,但我收到了输入不匹配错误,因为您没有正确读取数据。
例如,您试图将“8230123345450”存储到 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 变量的数据类型。
【讨论】:
如果您仍然遇到问题,请告诉我。 非常感谢。这解决了错误。我也收到了输入不匹配错误,我按照你的建议做了。现在准备好了。【参考方案2】:这些代码行都在你的主类中:
import static java.lang.System.in;
Scanner in = new Scanner(new File(fName));
你有两个叫做“in”的东西。这可能会导致有问题的错误 - 它是导入的“in”(这是一个 InputStream)还是变量“in”(这是一个 Scanner)。我建议你不要导入 java.lang.System .in 并在你想使用那个时输入 System.in。这样就不会和你的变量冲突了。
【讨论】:
或者,您可以重命名变量,使其不被称为“in”。 感谢您的帮助。以上是关于Java 不兼容的类型:输入流无法转换为扫描仪的主要内容,如果未能解决你的问题,请参考以下文章
BlueJ 错误:“不兼容的类型:int 无法转换为 java.lang.String”和“不兼容的类型:java.lang.String 无法转换为 int”