Reading from file double value using Scanner - InputMismatchException?

后端 未结 3 1797
无人及你
无人及你 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:32

    Try this

    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.util.Scanner;
    
    public class MainClass{
    public static void main(String[] args)
            throws FileNotFoundException
    {
        Scanner in = new Scanner(new File("D:\\input.txt"));
        String largestNum=in.next().trim();
        double largest = Double.parseDouble(largestNum);
        while (in.hasNextDouble())
        {
            String Num=in.next().trim();
            double input = Double.parseDouble(Num);
            if (input > largest)
            {
                largest = input;
            }
        }
        in.close();
        System.out.println("Largest value: " + largest);
    } }
    

提交回复
热议问题