英文:
Reading 3 digits string numbers to an ArrayList?
问题
I get a line in the following block:
while ((line = reader.readLine()) != null) {
System.out.println(line); // 000,050,007
List<Integer> list = List.of(Integer.parseInt(line))
}
However, I cannot cast the 3 digit values (string) to Integer. So, how can I do this in an elegant way?
Update: I use the following approach, but I am not sure if there is a better way than using map() 2 times?
List<Integer> items= Stream.of(line.split(","))
.map(String::trim)
.map(Integer::parseInt)
.toList();
英文:
I am reading values from a text files as shown below:
000,050,007
059,000,157
002,038,000
I get a line in the following block:
while ((line = reader.readLine()) != null) {
System.out.println(line); // 000,050,007
List<Integer> list = List.of(Integer.parseInt(line))
}
However, I cannot cast the 3 digit values (string) to Integer. So, how can I do this in an elegant way?
Update: I use the following approach, but I am not sure if there is a better way than using map() 2 times?
List<Integer> items= Stream.of(line.split(","))
.map(String::trim)
.map(Integer::parseInt)
.toList();
答案1
得分: 2
以下是翻译好的代码部分:
一个 `Scanner` 也是可能的:
import java.util.List;
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Path;
public class NScan {
public static void main(String[] args) {
try (Scanner s = new Scanner(Files.newBufferedReader(Path.of(args[0]))).useDelimiter("[\r\n,]")) {
List<Integer> ints = s.tokens()
.map(String::trim)
.map(Integer::valueOf)
.toList();
System.out.println(ints);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
英文:
A Scanner is possible too:
import java.util.List;
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Path;
public class NScan {
public static void main(String[] args) {
try (Scanner s = new Scanner(Files.newBufferedReader(Path.of(args[0]))).useDelimiter("[\r\n,]")) {
List<Integer> ints = s.tokens().
map(String::trim).
map(Integer::valueOf).
toList();
System.out.println(ints);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
答案2
得分: 0
在将其转换为整数列表之前,您需要在逗号上拆分该行。
List<List<Integer>> list = new ArrayList<>();
while ((line = reader.readLine()) != null) {
System.out.println(line); // 000,050,007
List<Integer> integers = Arrays.stream(line.split(","))
.map(s -> Integer.parseInt(s.trim()))
.toList();
list.add(integers);
}
请注意,具有多个 map 调用不会影响性能,您只需根据可读性来决定。
由于您使用了 BufferedReader,您可以使用流替换 while 循环:
List<List<Integer>> list = reader.lines().map(line ->
Arrays.stream(line.split(","))
.map(s -> Integer.parseInt(s.trim()))
.toList()
).toList();
英文:
You need to split the line on comma before converting it to List of integers
List<List<Integer>> list = new ArrayList<>();
while ((line = reader.readLine()) != null) {
System.out.println(line); // 000,050,007
List<Integer> integers = Arrays.stream(line.split(","))
.map(s -> Integer.parseInt(s.trim()))
.toList();
list.add(integers);
}
Note that having multiple map calls doesn't affect the performance, you just need to decide based on readability here.
As you are using BufferedReader, you can replace while loop with stream
List<List<Integer>> list = reader.lines().map(line ->
Arrays.stream(line.split(","))
.map(s -> Integer.parseInt(s.trim()))
.toList()
).toList();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论