我正在使用扫描仪类将文本扫描到程序中, 并且文本文件的每一行都是表格的预订
(id) (month) (date) (numdays) (type1) (number1) (type2) (number2) . . .
预订数据的前四个要素始终是 相同的数字,即4,但(类型)和(数字)数据可以有 任意数量的参数,因此我想检测一行的结尾 扫描仪类有什么办法吗?
我想找到一行的结尾,所以我的代码不会读取 下一行是预订的延续。
}否则if(input.equals(“Booking”)){
int bookingId=inputStream.nextInt();
System.out.printf("%d ",bookingId);
String month = inputStream.next();
System.out.printf("%s ", month);
int date = inputStream.nextInt();
System.out.printf("%d ", date);
int numDays= inputStream.nextInt();
System.out.printf("%d ", numDays);
while(!(inputStream.findInLine("\n").equals("\n"))){
String nextType = inputStream.next();
System.out.printf("%s ",nextType);
int nextCapacity= inputStream.nextInt();
System.out.printf("%d ", nextCapacity);
Map<String, Integer> requests=new HashMap<String, Integer>();
requests.put(nextType,nextCapacity);
}
答案 0 :(得分:0)
你可以检查EOL:
charAt(i)!='\n' && charAt(i+1)!='\r'
或者您可以一次只阅读整行:
Scanner sc = new Scanner(file);
line = sc.nextLine();
答案 1 :(得分:0)
根据您的代码,我建议使用两个扫描仪:一个用于读取整行,另一个用于读取符号。这样第一台扫描仪就会关注EOL。代码如下所示:
Scanner sc = new Scanner(new File("input"));
while(sc.hasNextLine()) {
Scanner scLine = new Scanner(sc.nextLine());
int bookingId=scLine.nextInt();
String month = scLine.next();
int date = scLine.nextInt();
int numDays= scLine.nextInt();
System.out.printf("%d %s %d %d ", bookingId, month, date, numDays);
Map<String, Integer> requests=new HashMap<>();
while(scLine.hasNext()) {
String type = scLine.next();
int capacity= scLine.nextInt();
requests.put(type, capacity);
System.out.printf("%s %d", type, capacity);
}
System.out.println();
}