在Mac上但不在Win10上使用Eclipse时,Java Scanner出现InputMismatchException错误

时间:2019-03-08 22:44:57

标签: java eclipse macos-mojave

我的行为我无法理解。该代码在Win10上可以完美运行,但在MacOS上会引发InputMismatchException错误。尝试调用nextDouble()或nextInt()时会中断。

调用是通过以下方法进行的:

public void readVehicleData() 
{
    Frame frame = null;
    FileDialog fileBox = new FileDialog(frame, "Open", FileDialog.LOAD);
    fileBox.setDirectory(".");
    fileBox.setVisible(true);

    String fileName = fileBox.getDirectory()+fileBox.getFile(); 

    try
    {
        File dataFile = new File(fileName);  
        Scanner scanner = new Scanner(dataFile);

        while(scanner.hasNextLine())
        {
            String lineOfText = scanner.nextLine().trim(); 

            if (!lineOfText.startsWith("//") && !lineOfText.isEmpty() && !lineOfText.startsWith("["))
            {
                Scanner strScanner = new Scanner(lineOfText).useDelimiter("\\s*[,\n]\\s*");

                Vehicle vehicle = new Vehicle(); 

                if(strScanner.hasNext())
                {
                     vehicle.readData(strScanner);
                     vehicleList.add(vehicle);

                     strScanner.nextLine();
                }

                strScanner.close();  
            }                                
        }

        if(scanner.hasNextLine())
        {
            scanner.nextLine();
        }   

        scanner.close();
    }

    catch(FileNotFoundException ex)
    {
        System.err.println(ex.getMessage());
        System.err.println(ex.getCause());
        ex.printStackTrace();
        System.err.println("File " + fileName + " not found"); 
        fileName = null;
    }

读取“ vehicle.readData(strScanner)”时发生错误,如下所示:

public void readData(Scanner strScanner) 
{
   group = strScanner.next();
   vehID = strScanner.next(); 
   regNo = strScanner.next();
   make = strScanner.next();    
   model = strScanner.next();    

   String strAirCon = strScanner.next();                          
   airCon = strAirCon.equalsIgnoreCase("Yes") == true ? true : false; 

   // It throws an InputMismatchException error here 
   engineSize = strScanner.nextDouble();   

   fuelType = strScanner.next(); 
   gearbox = strScanner.next();
   transmission = strScanner.next();

   // It also throws an InputMismatchException error here 
   // if the previous variables are commented
   mileage = strScanner.nextInt();


   dateFirstRegistered = strScanner.next();
}

这些是我得到的错误:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at vehicleproject.Vehicle.readData(Vehicle.java:121)
at vehicleproject.ReservationSystem.readVehicleData(ReservationSystem.java:65)
at vehicleproject.Main.main(Main.java:10)

我显然已经完成了研究,但找不到任何解决方案。它在Win10上可以正常运行,但在MacOS上却没有。

有人在MacOS上遇到过相同类型的错误吗?

这是我需要读取的数据的示例:

[汽车数据]

//数据是组,VehID,RegNo,品牌,型号,AirCon或气候控制,发动机尺寸(升),燃料类型,变速箱,变速器,里程,首次登记日期,车身类型,门数,座位

AA,TF-63403,MJ09TFE,菲亚特,熊猫Active Eco,否,1.1,无铅,五速手册,前轮驱动,13584、2009年7月29日,掀背车,5、5 A,TF-61273,MJ09TFD,菲亚特,格兰德·蓬托Active,否,1.4,无铅,五速手册,前轮驱动,14278、2009年12月5日,掀背车,3、5

这是课程之一:

import java.util.*;
import java.awt.*;
import java.io.*;

public class Vehicle
{
    private String group, vehID, regNo, make;
    private String model, fuelType, gearbox, transmission, dateFirstRegistered;
    private boolean airCon;
    private double engineSize;
    private int mileage;


    public Vehicle()
    {
        group = null;
        vehID = null;
        regNo = null;
        make = null;
        model = null;
        airCon = false;
        engineSize = 0.0;
        fuelType = null;
        gearbox = null;
        transmission = null;
        mileage = 0;
        dateFirstRegistered = null;      
    }

    public String getGroup()
    {
        return group;
    }

    public void setGroup(String newGroup)
    {
        group = newGroup;
    }

    public String getVehicleID()
    {
        return vehID;
    }

    public void setVehicleID(String newVehID)
    {
        vehID = newVehID;
    }

    public String getRegistrationNo()
    {
        return regNo;
    }

    public void setRegistrationNo(String newRegNo)
    {
        regNo = newRegNo;
    }

    public String getMake()
    {
        return make;
    }

    public void setMake(String newMake)
    {
        make = newMake;
    }

    public String getModel()
    {
        return model;
    }

    public void setModel(String newModel)
    {
        model = newModel;
    }

    public boolean getAirCon()
    {
        return airCon;
    }

    private String checkAirCon()
    {
        String output;              
        return output = getAirCon() == true ? "Yes" : "No";
    }

    public void printDetails()
    {
        System.out.println(make + " " + model + "  Group: " + group + "  Vehicle Id: " + vehID);
        System.out.println("Air conditioning / Climate control: " + checkAirCon());
        System.out.println("Engine Size: " + engineSize + "    Fuel: " + fuelType);
        System.out.println("Gearbox: " + gearbox + "  Transmission: " + transmission);
        System.out.println("Mileage: " + mileage + "  Date first registered: " + dateFirstRegistered);
        System.out.println();
    }

    public void readData(Scanner strScanner) 
    {
       group = strScanner.next();
       vehID = strScanner.next(); 
       regNo = strScanner.next();
       make = strScanner.next();    
       model = strScanner.next();    
       //System.out.println(model); 

       String strAirCon = strScanner.next();                          
       airCon = strAirCon.equalsIgnoreCase("Yes") == true ? true : false; 

       engineSize = strScanner.nextDouble();
       //System.out.println(engineSize);    

       fuelType = strScanner.next(); 
       gearbox = strScanner.next();
       transmission = strScanner.next();

       mileage = strScanner.nextInt();
       //System.out.println(mileage); 

       dateFirstRegistered = strScanner.next();
    }
}

0 个答案:

没有答案