import java.util.InputMismatchException;
import java.io.*;
import java.util.Scanner;
public class WriteStudentData
{
public static void main(String[] args) throws Exception
{
Scanner keyIn=new Scanner (System.in);
Student s = new Student();
//Create a file object called student.txt
java.io.File file = new java.io.File("student.txt");
// Create file
java.io.PrintWriter output = new java.io.PrintWriter(file);
// Write to the student textfile
output.println("L00012322 Mary Smith 99.1 47.5 66.5 Female");
output.println("L00022211 Paul O Donnell 17.5 99.1 47.5 Male");
output.println("L00033333 Anne Marie McLaughlin 100 100 100 Female");
output.println("L00033345 Sean Dunne 99.1 99.1 0 Male");
// Close the file
output.close();
try{//open a named file (Student)
FileInputStream fstream = new FileInputStream("student.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
try
{
String data = "S123456 John Paul Smith, 100, 100, 100, Male\n";
if(!file.exists())
{
file.createNewFile();//if file doesnt exists, then create it. if it exists write to it/overwrite it
}
//true = append file
FileWriter fileWriter = new FileWriter(file.getName(),true);
BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
bufferWriter.write(data);
bufferWriter.close();
System.out.println("\n You have added a new student to file \n" +data +"\n\n");
output.close();
}
catch(IOException e){
e.printStackTrace();
}
System.out.println("\tPlease select option: ");
int option=0;
do
{
//display menu
System.out.println("\n\t\t----Options ----");//maybe add lno here
System.out.println("\t1. Add Student" );
System.out.println("\t2. Find Average ");
System.out.println("\t3. Find female");
System.out.println("\t4. Find Male");
System.out.println("\t5. Information on file setup");//maybe view files
System.out.println("\tQ. Quit");
option = keyIn.nextInt();
switch(option)
{
case 1:
int method=0;
do
{
try
{
System.out.print ("\tENTER STUDENT DETAILS\n\n");
System.out.print ("\tEnter Student L Number\n\n");
String Lno = keyIn.next();
//while(!keyIn.hasNextLine ([A-Z a_z]);
// if (!keyIn.hasNextLine())
//throw new BadDataException();
System.out.print ("\t Eenter Student first Name\n\n");
String FName = keyIn.next();
// if (!keyIn.hasNextLine())
//throw new BadDataException("Student first name only");*/
System.out.print ("\t Enter Student Middle Name\n\n");
String MiddleN = keyIn.next();
// if (!keyIn.hasNextLine())
//throw new BadDataException("Student Middle name if he has one");*/
System.out.print ("\t Enter Student Last Name\n\n");
String LastN = keyIn.next();
// if (!keyIn.hasNextLine())
//throw new BadDataException("Student Last name only");*/
System.out.print ("\t Enter Students Mark1 result\n\n");
double Mark1 = keyIn.nextDouble();
System.out.print ("\t Enter Students Mark2 result\n\n");
double Mark2 = keyIn.nextDouble();
System.out.print ("\t Enter Student Mark3 result\n\n");
double Mark3 = keyIn.nextDouble();
System.out.printf("\t Enter Students Gender\n\n");
String data = keyIn.next();
//if file doesnt exists, then create it
if(!file.exists())
{// if it exists write to it else write a new file
file.createNewFile();
}
//true = append file
FileWriter fileWriter = new FileWriter(file.getName(),true);
PrintWriter p = new PrintWriter(fileWriter);
// p.write(Lno);
// p.write(" "+FName);
// p.write(" "+MiddleN);
// p.write(" "+LastN);
// p.write(" "+Mark1);
// p.write(" "+Mark2);
// p.write(" "+Mark3);
// p.write(" "+data);
// p.close();
System.out.println(" new student added to record");
}
catch(Exception e)
{
System.out.print("The information you have entered is incorrect\n");
}
}while(method != 0);
//System.exit(0);
break;
case 2:
// Create a file object called Average.txt
java.io.File file2 = new java.io.File("Average.txt");
// Create file
java.io.PrintWriter output2 = new java.io.PrintWriter(file2);
output2.println("45"); //p.write(Lno);
output2.close();
break;
case 3:
// Create a file object called Male.tx
java.io.File file3 = new java.io.File("Male.txt");
// Create file
java.io.PrintWriter output3 = new java.io.PrintWriter(file3);
output3.println("45");
output3.close();
break;
case 4:
// Create a file object called Female.txt
java.io.File file4 = new java.io.File("Female.txt");
// Create file
java.io.PrintWriter output4 = new java.io.PrintWriter(file4);
output4.println("45");
output.close();
break;
// case 5://this can be done without/ maybe
// System.out.println("\t File created\n");
// System.out.println("\t File Readable " + file.canRead());
// System.out.println("\t File Writeable " + file.canWrite());
// System.out.println("\t Is a File " + file.isFile());
// System.out.println("\t Is Hidden " + file.isHidden());
// System.out.println("\t Last Modified " + file.lastModified());
// System.out.println("\t File Size " + file.length()+"\n");
// file.renameTo(new File("studentFile"));
// break;
default:
}//end switch
}while(option != 0);
boolean done = false;
// System.out.print("Your information is incorrect");
// PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter(s)));
// printwriter is used to write characters to a file used for a while ie println print in system.out.print to screen
// BufferedWriter gathers a bunch of charaters and write them all at one time
//FileWriter is used to write stream of charaters to a file thats all it does"writes to the file that has been created
}
}