Reading from file double value using Scanner - InputMismatchException?

后端 未结 3 1795
无人及你
无人及你 2020-12-22 06:21

I tried read from file double values and using Scanner with this aim.

It throws InputMismatchException :

\"input.txt\"  java.util.InputM         


        
3条回答
  •  自闭症患者
    2020-12-22 06:28

    • Initialise largest to the first double (after checking the type)
    • Use the correct delimiter to parse your input (\n = newline)

      String filename = "input.txt"; 
      Scanner in = new Scanner(filename).useDelimiter("\\n");
      
      double largest;
      if (in.hasNextDouble())
          largest = in.nextDouble();
      
      while (in.hasNextDouble())
      {
          double input = in.nextDouble();
          if (input > largest)
          {
              largest = input;
          }
      }
      

提交回复
热议问题