Create a program called RunStudent.java. My program would be ConwayRunStudent.ja
ID: 3857270 • Letter: C
Question
Create a program called RunStudent.java. My program would be ConwayRunStudent.java and my class name would be ConwayRunStudent. All the input from the user should be from the command line (use the Scanner object). You only need to create one Scanner object to handle all the input. Do not use JOptionPane. Write a program that reads a set of student input from the user and writes this input to a file. The file created should be called Student.txt and be created in the same folder as the program. Your program will reflect a simple form. The user will be asked for a first name, last name, id number, year, and major. Store the names and major as Strings, the id number as an int, and the year as a char (user will be prompted for F/O/J/S where F is for freshman, O is for sophomore, J is for junior, S is for senior). The input requested from the user will be obtained via the command line and a sample is shown below on the right. After you get all of the user's input, use a PrintWriter object and the methods in the PrintWriter class to print this input to a file called Student.txt. The format of the file is shown below on the left. First and last name should be on one line and year should be Freshman, Sophomore, Junior or Senior. Make sure to follow this format with respect to line breaks. If your input is identical to that shown on the right, the contents of the file should be identical to the one shown below. conwayStudent.t xt: Student Name: Rascal Conway ID Number: 12345 Year: Freshman Major: Veterinary science > rn ConwayRunStudent Enter your first name: Rascal Enter your last name: Conway Enter your ID number: 12345 Enter your year: (F/O/J/S) F Enter your major: Veterinary scienceExplanation / Answer
please find the code
import java.util.*;
import java.io.*;
public class HelloWorld{
public static void main(String []args) throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your first name:" );
String fName = sc.nextLine();
System.out.println("Enter your Last name:" );
String lName = sc.nextLine();
System.out.println("Enter your ID number:" );
String id = sc.nextLine();
System.out.println("Enter your Year (F/O/J/S):" );
String year = sc.nextLine();
System.out.println("Enter your major:" );
String major = sc.nextLine();
String filename = lName+"Student.txt";
PrintWriter writer =new PrintWriter(new File(filename));
if(year.equals("F"))
writer.write("Fresher");
else if(year.equals("O"))
writer.write("Sophomore");
else if(year.equals("J"))
writer.write("Junior");
else
writer.write("Senior");
writer.write("Firstname: "+fName);
writer.write("Lastname: "+lName);
writer.write("Year: "+year);
writer.write("ID number: "+id);
writer.write("Major: "+major);
writer.flush();
writer.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.