读取.txt文件时获取InputMismatchException

问题描述 投票:0回答:1

我正在尝试读取city.txt文件并将其存储到数组列表中。但是我在第28行中得到了InputMismatchException(在读取填充时)。我阅读了所有类似的问题,但无法弄清楚。我尝试使用hasNext代替hasNextLine,它不起作用。这是错误的屏幕截图:Error Screenshot

这里是city.txt

城市;纬度;经度;国家;人口 Kandahar;316100;656949;Afghanistan;715542 Jalalabad;344415;704361;Afghanistan;597971 Herat;343300;621700;Afghanistan;481009 Mazar-e Sharif;367000;671000;Afghanistan;458151 Kondoz;367280;688725;Afghanistan;259809 Baghlan;361393;686993;Afghanistan;218748 Lashkar Gah;315830;643600;Afghanistan;201546 Meymaneh;359302;647701;Afghanistan;199795

主代码

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

// this program reads the city.txt and stores it into an array list.
public class CityReader {

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

    try {
        ArrayList<City> cities = new ArrayList<>(); //an array list to store the cities

        File file = new File("city.txt"); //File object to read the city.txt

        Scanner input = new Scanner(file); //Scanner object to read the file
        input.useDelimiter(";"); //values are separated by a ;

        while (input.hasNextLine()) { 
            String city = input.next(); //read city

            int latitude = input.nextInt(); //read latitude

            int longitude = input.nextInt(); //read longitude

            String country = input.next(); //read country

            int population = input.nextInt(); //read population

            City citiesss = new City(city, latitude, longitude, country, population); //adding values into the City object
            cities.add(citiesss); //adding the City object into the array list
        }
        input.close(); //close Scanner object

    } catch (FileNotFoundException e) { //if there is no file then print this
        System.out.println("Input file can not be found.");
    }

}
}

城市等级

public class City {
//data fields
private String name; //name
private int latitude;
private int longitude;
private String country;
private int population;

City() {} //default constructor

City(String name, int latitude, int longitude, String country, int population ) { //constructor
    this.name = name;
    this.latitude = latitude;
    this.longitude = longitude;
    this.country = country;
    this.population = population;
}
}
java oop arraylist java.util.scanner inputmismatchexception
1个回答
0
投票

city.txt应该是这样的:(请参阅每行结尾的分号)

Kandahar;316100;656949;Afghanistan;715542;
Jalalabad;344415;704361;Afghanistan;597971;
Herat;343300;621700;Afghanistan;481009;
Mazar-e Sharif;367000;671000;Afghanistan;458151;
Kondoz;367280;688725;Afghanistan;259809;
Baghlan;361393;686993;Afghanistan;218748;
Lashkar Gah;315830;643600;Afghanistan;201546;
Meymaneh;359302;647701;Afghanistan;199795

测试代码:

public static void main(String[] args) throws IOException {
    File output = null;
    ArrayList<City> cities = new ArrayList<>(); //an array list to store the cities
    try {

        File file = new File("city.txt"); //File object to read the city.txt

        output = new File("output.txt"); //File object to read the city.txt
        if(!output.exists()) 
            output.createNewFile();

        Scanner input = new Scanner(file); //Scanner object to read the file
        input.useDelimiter(";"); //values are separated by a ;

        while (input.hasNextLine()) { 
            String city = input.next(); //read city
            int latitude = input.nextInt(); //read latitude
            int longitude = input.nextInt(); //read longitude
            String country = input.next(); //read country
            int population = input.nextInt(); //read population

            City citiesss = new City(city, latitude, longitude, country, population); //adding values into the City object
            cities.add(citiesss); //adding the City object into the array list



        }
        input.close(); //close Scanner object

    } 
catch (FileNotFoundException e) { //if there is no file then print this
        System.out.println("Input file can not be found.");
    }

    //test
    FileWriter fw = new FileWriter(output);


    for (City city: cities)
    {
        fw.write(city.name);
    }
    fw.flush();
    fw.close();
}
© www.soinside.com 2019 - 2024. All rights reserved.