简体   繁体   English

使用java中的扫描器类从文件中获取输入

[英]taking input from file using scanner class in java

I am trying store a list of cities in a class using two co-ordinates(x,y) in java 我正在尝试使用Java中的两个坐标(x,y)在类中存储城市列表

My class is as follows: 我的课如下:

public class City {
    int x;
    int y;

    // Constructs a city at chosen x, y location
    public City(int x, int y){
        this.x = x;
        this.y = y;
    }
}

I have made another class to store the cities in an ArrayList : 我做了另一个类来将城市存储在ArrayList

public class TourManager {

    // Holds our cities
    private static ArrayList destinationCities = new ArrayList<City>();

    // Adds a destination city
    public static void addCity(City city) {
        destinationCities.add(city);
    }
}

My main class is as follows: 我的主要课程如下:

public class Final {
    public static void main(String[] args) {
        // Create and add our cities
        City city = new City(60, 200);
        TourManager.addCity(city);
        City city2 = new City(180, 200);
        TourManager.addCity(city2);
        City city3 = new City(80, 180);
        TourManager.addCity(city3);
    }
}

Here, I have stored three cities in the ArrayList inside the main function. 在这里,我在主函数内的ArrayList存储了三个城市。 But now I want to take the input from a file in the following format. 但是现在我想以以下格式从文件中获取输入。 I just want to take the co-ordinates ignoring all other lines. 我只想使坐标忽略所有其他行。

NAME: sample.tsp 名称:sample.tsp

TYPE: TSP 类型:TSP

COMMENT: Odyssey of Ulysses (Groetschel/Padberg) 评论:尤利西斯奥德赛(Groetschel / Padberg)

DIMENSION: 3 尺寸:3

EDGE_WEIGHT_TYPE: GEO EDGE_WEIGHT_TYPE:GEO

NODE_COORD_SECTION NODE_COORD_SECTION

1 60 200 1 60 200

2 180 200 2180200

3 80 180 3 80 180

I want to use the scanner class to take the input but getting confused in using it. 我想使用扫描仪类接收输入,但在使用它时感到困惑。 i have made a partial code fragment to take input but it isn't working: 我做了一个部分代码片段以接受输入,但是它不起作用:

Scanner in = new Scanner(new File("sample.tsp"));
String line = "";
int n;
in.nextLine();
in.nextLine();
in.nextLine();
line = in.nextLine();
line = line.substring(11).trim();
n = Integer.parseInt(line);
in.nextLine();
in.nextLine();

But how will I take the coordinates using the City class object as I have done before from this file? 但是如何像以前一样从该文件中使用City类对象获取坐标?

You need to skip the first few lines, which you don't need. 您需要跳过不需要的前几行。

Then split the lines to get the various numbers. 然后分割线以获取各种数字。 Index 0 contains the serial no., 1 and 2 contain the coordinates. 索引0包含序列号,索引1和2包含坐标。 Then parse them to int . 然后将它们解析为int eg: 例如:

in.nextLine();// multiple times.
//...
String cs = in.nextLine(); // get the line
City city = new City(Integer.parseInt(cs.split(" ")[1]), Integer.parseInt(cs.split(" ")[2]));
TourManager.addCity(city);

String cs2 = in.nextLine();
City city2 = new City(Integer.parseInt(cs2.split(" ")[1]), Integer.parseInt(cs2.split(" ")[2]));
TourManager.addCity(city2);

String cs3 = in.nextLine();
City city3 = new City(Integer.parseInt(cs3.split(" ")[1]), Integer.parseInt(cs3.split(" ")[2]));
TourManager.addCity(city3);

Whenever you use in.nextLine() , actually it takes the string that you entered. 每当您使用in.nextLine() ,实际上它将采用您输入的字符串。 So try to assign it to some string variable. 因此,尝试将其分配给某些字符串变量。 For example: 例如:

String s =in.nextLine();

Also, just using in.nextLine(); 另外,仅使用in.nextLine(); which you did in 2nd,3rd 4th,8th and 9th line is not needed. 不需要您在第二,第三,第四,第八和第九行中所做的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM