Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java输入不匹配异常与专用txt文件_Java_Type Mismatch_Inputmismatchexception - Fatal编程技术网

Java输入不匹配异常与专用txt文件

Java输入不匹配异常与专用txt文件,java,type-mismatch,inputmismatchexception,Java,Type Mismatch,Inputmismatchexception,我正在编写一个程序,需要扫描一个txt文件。txt文件保证在不同类型出现的位置和时间遵循特定格式。我试着在我的程序中利用这一点,并使用扫描仪将我知道的是int的部分以及double和string放入int中。当我运行我的程序时,它告诉我有一个类型不匹配异常,我知道由于txt文件的格式,我的所有类型都匹配,所以我如何使IDE认为这是可以的。下面是一段有问题的代码,这很有帮助 ArrayList<Student>studentList=new ArrayList<Student&g

我正在编写一个程序,需要扫描一个txt文件。txt文件保证在不同类型出现的位置和时间遵循特定格式。我试着在我的程序中利用这一点,并使用扫描仪将我知道的是int的部分以及double和string放入int中。当我运行我的程序时,它告诉我有一个类型不匹配异常,我知道由于txt文件的格式,我的所有类型都匹配,所以我如何使IDE认为这是可以的。下面是一段有问题的代码,这很有帮助

ArrayList<Student>studentList=new ArrayList<Student>();//makes a new Array list that we can fill with students.
    FileInputStream in=new FileInputStream("studentList.txt");//inputs the text file we want into a File Input Stream
    Scanner scnr=new Scanner(in);//Scanner using the Input Stream
    for(int i=0;i<scnr.nextInt();i++)//we know the first number is the number of minor students so we read in a new minor that number of times
    {
        Undergrad j=new Undergrad();//make a new undergrad
        j.setDegreeType("MINOR");//make the degree type minor because we know everyone in this loop is a minor.
        j.setFirstName(scnr.next());//we know the next thing is the student's first name
        j.setLastName(scnr.next());//we know the next thing is the student's last name
        j.setID(scnr.nextInt());//we know the next thing is the student's ID
        j.setGPA(scnr.nextDouble());//we know the next thing is the student's GPA
        j.setCreditHours(scnr.nextDouble());//we know the next thing is the student's credit hours
        studentList.add(j);//Finally, we add j to the arraylist, once it has all the elements it needs
    }
ArrayListstudentList=new ArrayList()//制作一个新的数组列表,我们可以用学生填充。
FileInputStream in=newfileinputstream(“studentList.txt”)//将我们想要的文本文件输入到文件输入流中
扫描仪scnr=新扫描仪(英寸)//使用输入流的扫描仪

对于(inti=0;i计算机程序完全按照您的要求执行

如果您创建了一个需要特定输入的程序,并且该程序告诉您“意外输入”;那么有两种逻辑解释:

  • 你对输入布局的假设(你放在你的程序中)是错误的
  • 这些假设是正确的,但不幸的是,输入数据并不关心这一点
  • 长话短说:并不是IDE在这里出错

    因此,这里的“战略”是:

  • 在编辑器中打开文本文件
  • 在IDE中打开源代码
  • 运行调试器;或“手动运行代码”;意思是:逐个浏览说明;对于每个扫描仪操作,检查扫描仪应返回的内容;以及文件在该位置实际包含的内容

  • 计算机程序完全按照你告诉他们的去做

    如果您创建了一个需要特定输入的程序,并且该程序告诉您“意外输入”;那么有两种逻辑解释:

  • 你对输入布局的假设(你放在你的程序中)是错误的
  • 这些假设是正确的,但不幸的是,输入数据并不关心这一点
  • 长话短说:并不是IDE在这里出错

    因此,这里的“战略”是:

  • 在编辑器中打开文本文件
  • 在IDE中打开源代码
  • 运行调试器;或“手动运行代码”;意思是:逐个浏览说明;对于每个扫描仪操作,检查扫描仪应返回的内容;以及文件在该位置实际包含的内容

  • 哪一行导致错误?错误发生在列表中的每个学生身上,还是只发生在特定的学生身上?您可以发布
    studentList.txt
    ?或者至少发布导致问题的部分吗?哪一行导致错误?错误发生在列表中的每个学生身上,还是只发生在特定的学生身上?您可以发布
    studentList.txt
    ?或者至少发布
    导致问题的部分?谢谢,每次循环运行时,我都会调用nextInt()循环控制条件,认为这是导致问题的原因:)不客气;很高兴它帮助解决了你的问题!谢谢,事实证明我的循环控制条件在每次循环运行时调用nextInt(),认为这是导致问题的原因:)不客气;很高兴它帮助解决了你的问题!