试图从 txt 文件中存储变量
Trying to store varibles from a txt file
我正在尝试读取文件并将文件中的每个变量存储到正确的位置我想从文件中保存为 Firstname、Lastname、DollarPerHour。然后在那之后分别打印出每个人以及一个方程式来解决他们本周的支票,但我不知道如何存储然后从每个人那里调用正确
public class Workers {
private String FirstName;
private String LastNAme;
private int HoursWorked;
private int DollarPerHour;
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastNAme() {
return LastNAme;
}
public void setLastNAme(String lastNAme) {
LastNAme = lastNAme;
}
public int getHoursWorked() {
return HoursWorked;
}
public void setHoursWorked(int hoursWorked) {
HoursWorked = hoursWorked;
}
public int getDollarPerHour() {
return DollarPerHour;
}
public void setDollarPerHour(int dollarPerHour) {
DollarPerHour = dollarPerHour;
}
@Override
public String toString() {
return "Workers{" +
FirstName + LastNAme +
", Has Worked " + HoursWorked + "hours this week"+
"and makes " + DollarPerHour +
"a hour";
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Main extends Workers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try{
BufferedReader reader = new BufferedReader(new FileReader("src/WorkersAndPay.txt"));
String file = reader.readLine();
while (file != null){
System.out.println(file);
file = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的txt文件设置为喜欢
Michael,Zargosa,14
MAx,SMith,15
Judith,Smith,16
如果你所有的行都被格式化为
[firstName],[lastName],[hoursWorked](我假设你的数值是你的工作时间而不是“每小时美元”)
例如,您可以简单地在行对象(奇怪地称为文件)上使用 split 方法:
String[] data = file.split(",");
现在,您有一个这样的字符串数组:["Michael", "Zargosa", "14"]
你仍然需要将你的“14”字符串转换成一个整数,这可以通过这种方式完成:
Integer.parseInt(...)
最终,您可以编写一个方法将您的线路转换为 Worker 对象:
private static Workers createFromLine(final String line) {
Workers worker = null;
if(line != null) {
worker = new Workers();
String[] data = line.split(",");
int workedHours = Integer.parseInt(data[2]);
worker.setFirstName(data[0]);
worker.setLastNAme(data[1]);
worker.setHoursWorked(workedHours);
}
return worker;
}
一种更优雅、更安全的方法可能是基于 正则表达式 的数据提取。最小的实现可能涉及检查您的行是否匹配此正则表达式:\w+,\w+,\d+
您的 class“Main”不应继承(扩展)Workers class,因为它没有意义。我真的不知道你想做什么,但如果你愿意你的主要方法使用或“包含”工人 class(的实例),则不需要 class从 Workers 继承。
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(new File("./WorkersAndPay.txt")));
while (br.ready()) {
String line = br.readLine();
if (line == null){continue;}
String[] words = line.split(",");
Workers worker = new Workers();
worker.setFirstName(words[0]);
worker.setLastName(words[1]);
worker.setHoursWorked(Integer.parseInt(words[2]));
System.out.println(worker.toString());
}
br.close();
}
catch (IOException e) {e.printStackTrace();}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
BufferedReader reader = new BufferedReader(new FileReader("src/WorkersAndPay.txt"));
String file = reader.readLine();
while (file != null){
file = reader.readLine();
String[] data = file.split(",");
Workers worker = new Workers();
worker.setFirstName(data[0]);
worker.setLastName(data[1]);
worker.setHoursWorked(Integer.parseInt(data[2]));
worker.setDollarPerHour(Integer.parseInt(data[3]));
System.out.println(worker.toString());
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//I did this and it prints out also change my txt to be see up like
//firstname,lastname,hourswroked,dallorPerhour
/* change to string to
@Override
public String toString() {
return "Workers{" +
firstName + " "+ lastName +
", Has Worked " + hoursWorked + " hours this week Check will be: "+
(dollarPerHour * hoursWorked) + " $ ";
}
*/
/*
Workers{Michael Murillo, Has Worked 50 hours this week Check will be: 550 $
Workers{Max Murillo, Has Worked 49 hours this week Check will be: 637 $
Workers{Eddie Murillo, Has Worked 50 hours this week Check will be: 700 $
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.split(String)" because "file" is null
at Main.main(Main.java:19)
*/
我正在尝试读取文件并将文件中的每个变量存储到正确的位置我想从文件中保存为 Firstname、Lastname、DollarPerHour。然后在那之后分别打印出每个人以及一个方程式来解决他们本周的支票,但我不知道如何存储然后从每个人那里调用正确
public class Workers {
private String FirstName;
private String LastNAme;
private int HoursWorked;
private int DollarPerHour;
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastNAme() {
return LastNAme;
}
public void setLastNAme(String lastNAme) {
LastNAme = lastNAme;
}
public int getHoursWorked() {
return HoursWorked;
}
public void setHoursWorked(int hoursWorked) {
HoursWorked = hoursWorked;
}
public int getDollarPerHour() {
return DollarPerHour;
}
public void setDollarPerHour(int dollarPerHour) {
DollarPerHour = dollarPerHour;
}
@Override
public String toString() {
return "Workers{" +
FirstName + LastNAme +
", Has Worked " + HoursWorked + "hours this week"+
"and makes " + DollarPerHour +
"a hour";
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Main extends Workers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try{
BufferedReader reader = new BufferedReader(new FileReader("src/WorkersAndPay.txt"));
String file = reader.readLine();
while (file != null){
System.out.println(file);
file = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的txt文件设置为喜欢
Michael,Zargosa,14
MAx,SMith,15
Judith,Smith,16
如果你所有的行都被格式化为 [firstName],[lastName],[hoursWorked](我假设你的数值是你的工作时间而不是“每小时美元”) 例如,您可以简单地在行对象(奇怪地称为文件)上使用 split 方法:
String[] data = file.split(",");
现在,您有一个这样的字符串数组:["Michael", "Zargosa", "14"] 你仍然需要将你的“14”字符串转换成一个整数,这可以通过这种方式完成:
Integer.parseInt(...)
最终,您可以编写一个方法将您的线路转换为 Worker 对象:
private static Workers createFromLine(final String line) {
Workers worker = null;
if(line != null) {
worker = new Workers();
String[] data = line.split(",");
int workedHours = Integer.parseInt(data[2]);
worker.setFirstName(data[0]);
worker.setLastNAme(data[1]);
worker.setHoursWorked(workedHours);
}
return worker;
}
一种更优雅、更安全的方法可能是基于 正则表达式 的数据提取。最小的实现可能涉及检查您的行是否匹配此正则表达式:\w+,\w+,\d+
您的 class“Main”不应继承(扩展)Workers class,因为它没有意义。我真的不知道你想做什么,但如果你愿意你的主要方法使用或“包含”工人 class(的实例),则不需要 class从 Workers 继承。
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(new File("./WorkersAndPay.txt")));
while (br.ready()) {
String line = br.readLine();
if (line == null){continue;}
String[] words = line.split(",");
Workers worker = new Workers();
worker.setFirstName(words[0]);
worker.setLastName(words[1]);
worker.setHoursWorked(Integer.parseInt(words[2]));
System.out.println(worker.toString());
}
br.close();
}
catch (IOException e) {e.printStackTrace();}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
BufferedReader reader = new BufferedReader(new FileReader("src/WorkersAndPay.txt"));
String file = reader.readLine();
while (file != null){
file = reader.readLine();
String[] data = file.split(",");
Workers worker = new Workers();
worker.setFirstName(data[0]);
worker.setLastName(data[1]);
worker.setHoursWorked(Integer.parseInt(data[2]));
worker.setDollarPerHour(Integer.parseInt(data[3]));
System.out.println(worker.toString());
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//I did this and it prints out also change my txt to be see up like
//firstname,lastname,hourswroked,dallorPerhour
/* change to string to
@Override
public String toString() {
return "Workers{" +
firstName + " "+ lastName +
", Has Worked " + hoursWorked + " hours this week Check will be: "+
(dollarPerHour * hoursWorked) + " $ ";
}
*/
/*
Workers{Michael Murillo, Has Worked 50 hours this week Check will be: 550 $
Workers{Max Murillo, Has Worked 49 hours this week Check will be: 637 $
Workers{Eddie Murillo, Has Worked 50 hours this week Check will be: 700 $
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.split(String)" because "file" is null
at Main.main(Main.java:19)
*/