在main方法和构造函数java中使用scanner问题?

时间:2018-03-07 23:07:35

标签: java exception java.util.scanner

解决

我在类构造函数中使用Scanners nextInt()方法时遇到问题。 如果在主方法中使用它,如下面的代码,它工作正常,但是当在构造函数中执行相同的操作时,我得到一个inputmismatchexception, 可能出现什么问题?

find

班级版

public class QuickTest{

 public static void main(String[] args) throws Exception{


  java.io.File myFile = new java.io.File("tograph.txt");
  java.util.Scanner input = new java.util.Scanner(myFile);

  int numberOfPoints = input.nextInt();

  String[] myArray = new String[numberOfPoints];

   //need to use nextLine once after reading in number of points to get  to next line

  input.nextLine();

  int count = 0;
  while(input.hasNext() == true){

     myArray[count] = input.nextLine();
     count++;
  }

  input.close();

  for(int i = 0; i < myArray.length; i++){

     System.out.println(myArray[i]);
  }

当我使用调试器时,main方法版本将从文件中获取点数,然后使用文件中每个后续行的字符串填充数组,但类版本会抛出异常

0 个答案:

没有答案