Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Last active August 29, 2015 14:00
Show Gist options
  • Save JorgeOlvera/7b1590504e0ec13f75a0 to your computer and use it in GitHub Desktop.
Save JorgeOlvera/7b1590504e0ec13f75a0 to your computer and use it in GitHub Desktop.
Binary output
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class StudentAppBinary {
public static void main(String [] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.println("Introduce ID of the student");
String id = input.nextLine();
System.out.println("Introduce name of the student");
String name = input.nextLine();
System.out.println("Introduce the gpa of the student");
double gpa = input.nextDouble();
System.out.println("Introduce the courses taken of the student");
int coursesTaken = input.nextInt();
input.nextLine();
Student alumni = new Student(id, name, gpa, coursesTaken);
System.out.println("What would you like to do?");
System.out.println("0 to Exit Application");
System.out.println("1 to Read File");
System.out.println("2 to Modify Information");
System.out.println("3 to Store Information");
String estring = input.nextLine();
char operation = estring.charAt(0);
switch(op) {
case '0':
System.out.println("See you later.");
break;
case '1':
try{
FileInputStream inputFile = new FileInputStream("studentbinary.bin");
ObjectInputStream file = new ObjectInputStream(inputFile);
String datid = file.readLine();
String datname = file.readLine();
double datgpa = file.readDouble();
int datcoursestaken = file.readInt();
System.out.println(theid);
System.out.println(thename);
System.out.println(thegpa);
System.out.println(thecoursesTaken);
} catch (FileNotFoundException e ) {
System.err.println("A FileNotFoundException happened" + e.getMessage());
}
break;
case '2':
System.out.println("What would you like to modify?");
System.out.println("1 to id");
System.out.println("2 to name");
System.out.println("3 to gpa");
System.out.println("4 to courses taken");
int mod = input.nextInt();
input.nextLine();
if(modify==1){
datid = input.nextLine();
} else if(modify==2) {
datname = input.nextLine();
} else if(modify==3){
datgpa = input.nextDouble();
} else {
datcoursestaken = input.nextInt();
}
break;
case '3':
try{
FileOutputStream fstream = new FileOutputStream("studentbinary.bin");
DataOutputStream outputFile = new DataOutputStream(fstream);
outputFile.write(coursesTaken);
outputFile.close();
} catch (FileNotFoundException e) {
System.err.println("Some FileNotFoundException happened: " + e.getMessage());
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment