I need to to write a Java program that will write entered information in the console to a .txt file. If the .txt file already exists it will have to open it and write the additional information on another line. If the .txt file doesn't exist for a new "Lifter" it will create the .txt and write it in. Basically I don't know how to create a new .txt for each name entered and how to edit it if the person's .txt already exists. How would I go about doing that?
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class MeetPrinter {
public static void getInfo() {
Scanner scanner = new Scanner(System.in);
int question;
String firstName;
String lastName;
int squat;
int bench;
int lift;
String meetName;
do {
System.out.println("Enter first name: ");
firstName = scanner.next();
System.out.println("Enter Last Name: ");
lastName = scanner.next();
System.out.println("Enter new Meet Name: ");
meetName = scanner.next();
System.out.println("Enter max squat weight in kg: ");
squat = scanner.nextInt();
System.out.println("Enter max bench press in kg: ");
bench = scanner.nextInt();
System.out.println("Enter max deadlift in kg: ");
lift = scanner.nextInt();
System.out
.println("Enter '1' to enter more lifters or '2' if you are done entering.");
question = scanner.nextInt();
} while (question == 1);
try{
PrintWriter out = new PrintWriter("output.txt");
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100000);
out.println(lastName + ", " + firstName + " Record #: " + randomInt);
out.println("");
String meet = "Meet Name";
String sq = "SQ Max";
String bp = "BP Max";
String sub = "SUB Total";
String dl = "DL Max";
String tot = "Total";
out.printf("%20s %15s %18s %19s %18s %18s",meet ,sq,bp,sub,dl,tot);
out.println("");
out.printf("%20s", meetName);
out.printf("%10d (%6.2f)", squat, squat * 2.2);
out.printf("%10d (%6.2f)", bench, bench * 2.2);
float subPounds = (float) ((float)(squat + bench) * 2.2);
out.printf("%10d (%6.2f)", squat + bench, subPounds);
out.printf("%10d (%6.2f)", lift, lift * 2.2);
float tPounds = (float) ((float)(squat + bench + lift) * 2.2);
out.printf("%10d (%6.2f)", squat + bench + lift, tPounds);
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
No comments:
Post a Comment