我目前正在开发一个需要计算公司薪水的Java程序。 Java程序需要导入一个带有工作列表的.txt文件和另一个带有他们已经完成的交付量的.txt。
这是workers.txt的示例文件,其中第一个是工人的ID,第二个是工人的名字,第三个是经验水平
1 : Pedro Reis : 5
2 : Miguel Teles : 5
3 : Nuno Reis : 4
4 : Rafael Carrilho : 3
5 : Nuno Nunes : 3
6 : Osvaldo Pires : 2
7 : Tiago Santos : 2
8 : J. Almeida : 2
9 : Rui Almondegas : 2
10 : Anacleto : 1
11 : José Manuel : 3
12 : Rudolfo Bento : 3
13 : Victor Valente : 1
14 : Lúcia Portugal : 3
15 : Vanessa Santos : 1
以下是deliveries.txt的示例文件,其中第一个是工作人员的ID,第二个是传递的区域,第三个是包的重量
1 : A : 250
2 : B : 610
3 : C : 1250
2 : D : 350
3 : A : 250
4 : A : 500
1 : B : 200
5 : C : 300
5 : C : 250
这是一个学校项目,我很难让Java程序读取两个.txt文件,并将.txt的每个空格分配给一个变量。
这是我建立的代码
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class project {
public static void main(String[] args) {
File file = new File("workers.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
System.out.println(dis.readLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在问题是,如何将工人的ID,工人的姓名等分配给另一个变量?
对于长篇文章我感到抱歉
由于
答案 0 :(得分:0)
分手后你可以写一个这样的课。
class Worker {
public int id;
public String name;
public Worker( int id, String name ) {
this.id = id;
this.name = name;
}
}
在主要班级:
Map<Integer, Worker> workers = new HashMap<>();
workers.put(*id*, new Worker(*id*, *name*));
示例:如果要获取工作人员的姓名,可以使用以下命令:
workers.get(*id*).name;