一、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
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]