猿问

扫描仪出现问题,无法读取 TSP 文件

我目前正在尝试从 TSP 文件中读取坐标,它们通常看起来像这样:


NAME: berlin52

TYPE: TSP

COMMENT: 52 locations in Berlin (Groetschel)

DIMENSION: 52

EDGE_WEIGHT_TYPE: EUC_2D

NODE_COORD_SECTION

1 565.0 575.0

2 25.0 185.0

3 345.0 750.0

4 945.0 685.0

5 845.0 655.0

6 880.0 660.0

7 25.0 230.0

8 525.0 1000.0

9 580.0 1175.0

10 650.0 1130.0

11 1605.0 620.0 

12 1220.0 580.0

13 1465.0 200.0

14 1530.0 5.0

15 845.0 680.0

16 725.0 370.0

17 145.0 665.0

18 415.0 635.0

19 510.0 875.0  

20 560.0 365.0

21 300.0 465.0

22 520.0 585.0

23 480.0 415.0

24 835.0 625.0

25 975.0 580.0

26 1215.0 245.0

27 1320.0 315.0

28 1250.0 400.0

29 660.0 180.0

30 410.0 250.0

31 420.0 555.0

32 575.0 665.0

33 1150.0 1160.0

34 700.0 580.0

35 685.0 595.0

36 685.0 610.0

37 770.0 610.0

38 795.0 645.0

39 720.0 635.0

40 760.0 650.0

41 475.0 960.0

42 95.0 260.0

43 875.0 920.0

44 700.0 500.0

45 555.0 815.0

46 830.0 485.0

47 1170.0 65.0

48 830.0 610.0

49 605.0 625.0

50 595.0 360.0

51 1340.0 725.0

52 1740.0 245.0

EOF

我想要做的是读取所有节点,它们的两个坐标并从中创建一个节点。我想将它们存储在一个 arraylist 存储列表中,例如:


ArrayList<String[]>

我的代码目前看起来像这样:


package group12.TSP.tree;

import java.io.File;

import java.util.*;


public class Tree {

    ArrayList<String[]> storing = new ArrayList<String[]>();


    public Tree() throws Exception{

    File file = new File("C:/Users/joaki/Desktop/burma14.tsp");

    Scanner sc = new Scanner(file);

    storing = new ArrayList<String[]>();

    String nextValue = null;

    //sc.reset();

    sc.useDelimiter("  ");

    while (sc.hasNextLine()) {

        sc.nextLine();

        while(sc.hasNextDouble()) {

            nextValue = sc.nextLine();

            //st.replaceAll("\\s+","")

            //nextValue = nextValue.replace(" ", "");

            storing.add(nextValue.split(""));   

            continue;

        }


    }

    sc.close();

}


这并没有使我想要它做的事情,但我不明白如何实现它,我想它可以只是将坐标复制到文本文件,但我希望它适用于各种 TSPS。提前致谢!


慕森王
浏览 199回答 2
2回答

慕虎7371278

在这里做了一些改变。我读到“NODE_COORD_SECION”然后开始解析存储行的ans。我不是在“”上拆分,而是在“”上拆分并存储值。public class Tree {&nbsp; &nbsp; ArrayList<String[]> storing;&nbsp; &nbsp; public Tree() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; File file = new File("C:/Users/joaki/Desktop/burma14.tsp");&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(file);&nbsp; &nbsp; &nbsp; &nbsp; storing = new ArrayList<String[]>();&nbsp; &nbsp; &nbsp; &nbsp; String nextValue = null;&nbsp; &nbsp; &nbsp; &nbsp; while (sc.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if("NODE_COORD_SECTION".equals(line)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (sc.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextValue = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; storing.add(nextValue.trim().split(" "));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sc.close();&nbsp; &nbsp; }&nbsp; &nbsp; public static ArrayList<String[]> returnScanner() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; Tree tree = new Tree();&nbsp; &nbsp; &nbsp; &nbsp; return tree.storing;&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String[]> storedValues = returnScanner();&nbsp; &nbsp; &nbsp; &nbsp; String[] firstLine = storedValues.get(0);&nbsp; &nbsp; &nbsp; &nbsp; String[] secondLine = storedValues.get(1);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < firstLine.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(firstLine[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我的输出:1565.0575.0

繁星淼淼

使用扫描仪移动到下一行,直到遇到短语“NODE_COORD_SECTION”。然后接下来的几行是你的数据线。它们都符合格式,因此您可以使用 split 来获取第二个和第三个元素。当您到达标有“EOF”的行时,停止读取和存储在您的数组中。你有多关心 TSP 文件的标题?如果要存储此信息并根据文件中的数据检查它是否正确,而不是仅运行到“NODE_COORD_SECTION”行,则需要查找行:“DIMENSION”并将值存储为 int。然后根据您的 ArrayList“存储”中的最终总数检查此值
随时随地看视频慕课网APP

相关分类

Java
我要回答