[英]Read text file and convert to 2d int array depending on input in Java
I read a text file that looks like this: 我读了一个文本文件,看起来像这样:
operationName1 Value
There is a variable number of lines, with different operations and corresponding value. 行数可变,具有不同的操作和相应的值。 I can read the file and get a 2D String array as the output. 我可以读取文件并获取2D String数组作为输出。 This is my code. 这是我的代码。
try{
Path path = Paths.get("./myFile.txt");
String[][] array = Files.lines(path)
.map(s -> s.split("\\s+", 2))
.map(a -> new String[]{a[0], a[1]})
.toArray(String[][]::new);
}catch(IOException e){
}
Question: How could I change my code to get a 2d int array instead, where operationName1 would be "0" and operationName2 would be "1" (there is only two possible operation, each defined by a specific string)? 问题:如何更改代码以获取2d int数组,其中operationName1将为“ 0”,而operationName2将为“ 1”(只有两个可能的操作,每个操作由一个特定的字符串定义)?
This text file: 该文本文件:
operationOne 5
OtherOp 999
operationOne 8
operationOne 2
Would become that 2d int array: 将成为2d int数组:
[[0,5],[1,999],[0,8],[0,2]]
The index is important too. 索引也很重要。 So the 1st line of the text file is the 1st line of my array. 因此,文本文件的第一行是数组的第一行。
PS: If there is a better syntax (a more recent one), I am open to suggestion. PS:如果有更好的语法(较新的语法),我可以提出建议。
Thank you very much. 非常感谢你。
If you need parallelism ? 如果需要并行处理 ? ... this could be one approach ...这可能是一种方法
AtomicInteger atomicInteger = new AtomicInteger(0);
Map<String, Integer> known = new ConcurrentHashMap<>();
Path path = Paths.get("./myFile.txt");
int[][] array = Files.lines(path)
.map(s -> s.split("\\s+", 2))
.map(a -> new int[]{
known.computeIfAbsent(a[0],
k -> atomicInteger.getAndIncrement()),
Integer.parseInt(a[1])
})
.toArray(int[][]::new);
I normally use the Scanner class for read text files 我通常使用Scanner类读取文本文件
ArrayList<String[]> array = new ArrayList();
Scanner scan= new Scanner(new File("./myFile.txt"));
String str;
String auxiliary= new String[2];
while(scan.hasNextLine()){
str= scan.readLine();
auxiliary=str.split(" "); // you can use also the \\s
for(int i=0; i<array.size();i++){
if(array.get(i)[0].equals(auxiliary[0])){
String aux2[]={i,auxiliary[1]};
break;
}
}
String aux2[]={array.size,auxiliary[1]};
}
I hope it helped. 希望对您有所帮助。
# EDIT: Corrected some issues #编辑:更正了一些问题
It is a bit long, but it allows you to go operationName1...operationNameN 它有点长,但是允许您进入operationName1 ... operationNameN
I always try to keep the code clean, so if you have any questions just ask 我总是尽量保持代码干净,因此,如果您有任何疑问,请问
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("test.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
ArrayList<String> operations = new ArrayList<String>();
ArrayList<Point> points = new ArrayList<Point>();
String line;
String[] array;
int index;
while((line = bufferedReader.readLine()) != null)
{
array = line.split(" ");
if((index = hasOperation(array[0], operations)) == -1)
{
operations.add(array[0]);
index = operations.size()-1;
points.add(new Point(index, Integer.parseInt(array[1])));
}
else
{
points.add(new Point(index, Integer.parseInt(array[1])));
}
}
System.out.print("[");
for(int i = 0; i < points.size()-1; i++)
{
System.out.print(points.get(i).toString() + ",");
}
System.out.println(points.get(points.size()-1).toString()+"]");
}
public static int hasOperation(String operation, ArrayList<String> operations ){
for(int index = 0; index < operations.size(); index++)
{
if(operation.equalsIgnoreCase(operations.get(index)))
return index;
}
return -1;
}
}
public class Point {
public int x;
public int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public String toString()
{
return "[" + x + "," + y + "]";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.