使用Java读取VRP问题标准测试集:C101.txt

本文介绍了Java中字符串处理的两种常见方法:使用`split()`函数分割带空格的字符串,以及使用`trim()`移除字符串两端的空白。同时展示了如何使用JDK1.5的`Scanner`类读取C101.txt文件并解析数据。
摘要由CSDN通过智能技术生成

一、split分割带空格的字符串(四种方法及其区别)

参考:https://blog.csdn.net/yezonghui/article/details/106455940

String str = "a b  c    d";
String[] arr1 = str.split(" "); //仅分割一个空格
String[] arr2 = str.split("s");
String[] arr3 = str.split("\t");  //空格
String[] arr4 = str.split("\\s+"); //分割一个或者多个空格,正则表达式\s表示匹配任何空白字符,+表示匹配一次或多次

运行结果:
在这里插入图片描述

二、使用trim()方法删除字符串的头尾空白符

String str = new String("    www.runoob.com    ");
System.out.println( str.trim() );

运行结果:

原始值 :    www.runoob.com    
删除头尾空白 :www.runoob.com

三、使用JDK1.5的Scanner类读取C101.txt

参考:Java读取txt文件:TSP问题测试算例att48.txt

public static void readFile(String pathname) throws IOException {
        try (Scanner scanner = new Scanner(new FileReader(pathname))) {
            // 跳过9行
            for (int i = 0; i < 9; i++) {
                scanner.nextLine();
            }

            while (scanner.hasNextLine()) {  //按行读取字符串

                String line = scanner.nextLine();
                line = line.trim();
                String[] data = line.split("\\s+");
                int id = Integer.parseInt(data[0]);
                int x = Integer.parseInt(data[1]);
                int y = Integer.parseInt(data[2]);
                int demand = Integer.parseInt(data[3]);
                int readyTime = Integer.parseInt(data[4]);
                int deliveryTime = Integer.parseInt(data[5]);
                int serviceTime = Integer.parseInt(data[6]);
                Customer customer = new Customer(id, x, y, demand, readyTime, deliveryTime, serviceTime);
                System.out.println(customer);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

public static void main(String[] args) throws IloException {
        try {
            String pathname = "src/main/resources/C101.txt";
            readFile(pathname);
        } catch (Exception e) {
            logger.warning(e.getMessage());
        }
    }

运行结果:

Customer[id=0, x=40, y=50, demand=0, readyTime=0, dueDate=1236, serviceTime=0]
Customer[id=1, x=45, y=68, demand=10, readyTime=912, dueDate=967, serviceTime=90]
Customer[id=2, x=45, y=70, demand=30, readyTime=825, dueDate=870, serviceTime=90]
Customer[id=3, x=42, y=66, demand=10, readyTime=65, dueDate=146, serviceTime=90]
Customer[id=4, x=42, y=68, demand=10, readyTime=727, dueDate=782, serviceTime=90]
Customer[id=5, x=42, y=65, demand=10, readyTime=15, dueDate=67, serviceTime=90]
Customer[id=6, x=40, y=69, demand=20, readyTime=621, dueDate=702, serviceTime=90]
Customer[id=7, x=40, y=66, demand=20, readyTime=170, dueDate=225, serviceTime=90]
Customer[id=8, x=38, y=68, demand=20, readyTime=255, dueDate=324, serviceTime=90]
Customer[id=9, x=38, y=70, demand=10, readyTime=534, dueDate=605, serviceTime=90]
Customer[id=10, x=35, y=66, demand=10, readyTime=357, dueDate=410, serviceTime=90]
Customer[id=11, x=35, y=69, demand=10, readyTime=448, dueDate=505, serviceTime=90]
Customer[id=12, x=25, y=85, demand=20, readyTime=652, dueDate=721, serviceTime=90]
Customer[id=13, x=22, y=75, demand=30, readyTime=30, dueDate=92, serviceTime=90]
Customer[id=14, x=22, y=85, demand=10, readyTime=567, dueDate=620, serviceTime=90]
Customer[id=15, x=20, y=80, demand=40, readyTime=384, dueDate=429, serviceTime=90]
Customer[id=16, x=20, y=85, demand=40, readyTime=475, dueDate=528, serviceTime=90]
Customer[id=17, x=18, y=75, demand=20, readyTime=99, dueDate=148, serviceTime=90]
Customer[id=18, x=15, y=75, demand=20, readyTime=179, dueDate=254, serviceTime=90]
Customer[id=19, x=15, y=80, demand=10, readyTime=278, dueDate=345, serviceTime=90]
Customer[id=20, x=30, y=50, demand=10, readyTime=10, dueDate=73, serviceTime=90]
Customer[id=21, x=30, y=52, demand=20, readyTime=914, dueDate=965, serviceTime=90]
Customer[id=22, x=28, y=52, demand=20, readyTime=812, dueDate=883, serviceTime=90]
Customer[id=23, x=28, y=55, demand=10, readyTime=732, dueDate=777, serviceTime=90]
Customer[id=24, x=25, y=50, demand=10, readyTime=65, dueDate=144, serviceTime=90]
Customer[id=25, x=25, y=52, demand=40, readyTime=169, dueDate=224, serviceTime=90]
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值