最初の従業員の詳細を表示するようにコードを取得しましたが、他の2つの従業員の詳細が表示されています。私はそれらを追加する方法がわかりません。私は、printWriterがコードを置くのに適切なものであるかどうか、そしてそれ以外の場合、何が最善であるかは分かりません。ユーザー入力をテキストファイルに保存してすべてのデータを表示する方法は?
は、コードは以下の通りです:)
public static void main(String[] args) throws IOException{
Scanner scan = new Scanner(System.in);
File employeeDetails = new File("Employees.txt");
PrintWriter pw = new PrintWriter(new FileWriter(employeeDetails, true));
for(int i=0; i<3; i++){
FileWriter fw = new FileWriter("Employees.txt", true);
try{
boolean repeat = false;
System.out.println("Enter name: ");
String name = scan.next();
pw.println("name: " + name);
System.out.println("Enter job title: ");
String jobTitle = scan.next();
pw.println("Job title: " + jobTitle);
do{
try{
System.out.println("Enter age: ");
int age = scan.nextInt();
pw.println("Age: " + age);
repeat = true;
}
catch(InputMismatchException ex){
System.err.println("Invalid age please enter a whole number.");
scan.next();
continue;
}
}while(repeat==false);
do{
try{
System.out.println("Enter salary per year: ");
double salary = scan.nextDouble();
pw.println("Salary: " + salary);
repeat = false;
}
catch(InputMismatchException ex){
System.err.println("Invalid salary please enter a decimal.");
scan.next();
continue;
}
catch(MissingFormatArgumentException ex){
System.err.println("Invalid salary please enter a decimal.");
scan.next();
continue;
}
}while(repeat);
}finally{
pw.close();
fw.close();
}
}
scan.close();
}
}
'fw'はどのように便利ですか?あなたはそれを使用しません... コードを実行するとどうなりますか? – ItamarG3
あなたはそれを修正しましたか? – ItamarG3
申し訳ありません私はしばらくの間にスタックのオーバーフローにされていない、私はそれを修正するために管理して、あなたの助けてくれてありがとう。私はファイルライターを持っていて、最後に真実があると思って、私が書いていたファイルに追加すると思った。 – Sarah