In: Computer Science
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Students
{
public static void main (String[] args) throws IOException
{ String first_name, last_name;
int grade, count=0;
Scanner fileInput = new Scanner(new File("students.txt"));
//Object st;
ArrayList<Student> st = new ArrayList<Student>();
while (fileInput.hasNext())
{
first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();
if (grade>89)
st.add(new Excellent(first_name, last_name, grade));
else
st.add(new Ok(first_name, last_name, grade));
count++;
}
for (int i=0; i<st.size(); i++)
{
if (st.get(i) instanceof Excellent)
st.get(i).info();
else
st.get(i).info();
}
System.out.println("There are " + count + " students");
}
}
public class Ok implements Student
{
private String fname, lname;
private int grade;
public Ok(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
}
public void info()
{
System.out.println(fname + " " + lname + " "+ grade + "\t" +
"ok");
}
}
public class Excellent implements Student
{
private String fname, lname;
private int grade;
public Excellent(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
}
public void info()
{
System.out.println(fname + " " + lname + " "+ grade + "\t" +
"excellent");
}
}
public interface Student
{
void info();
}
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. I have assumed the text file is a comma separated file, incase it is space separated file replace the below line as String[] tokens = line.split(","); to String[] tokens = line.split("\\s+"); Thank You !! =========================================================================== public class Student { private String firstName; private String lastName; private int grade; public Student(String firstName, String lastName, int grade) { this.firstName = firstName; this.lastName = lastName; this.grade = grade; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } @Override public String toString() { return "Name: " + firstName + " " + lastName + ", Grade: " + grade; } }
=============================================================
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class Students { public static void main(String[] args) { String fileName = "students.txt"; ArrayList<Student> records = null; try { records = getDataFromFile(fileName); } catch (FileNotFoundException e) { System.out.println("Unable to read data from file: " + fileName); System.exit(1); } String input; Scanner sc = new Scanner(System.in); System.out.println("\"all\" - prints all students"); System.out.println("\"excellent\" - prints all students grade>89"); System.out.println("\"ok\" - prints all students grade<=89"); System.out.println("\"end\" - exit program"); while (true) { System.out.println("Enter your command here > "); input = sc.nextLine(); if (input.equals("all")) { for (Student student : records) System.out.println(student); }else if(input.equals("excellent")){ for(Student student:records){ if(student.getGrade()>89) System.out.println(student); } }else if(input.equals("ok")){ for(Student student:records){ if(student.getGrade()<=89) System.out.println(student); } }else if(input.equals("end"))break; else System.out.println("Invalid command please try again."); } } private static ArrayList<Student> getDataFromFile(String fileName) throws FileNotFoundException { Scanner fileScanner = new Scanner(new File(fileName)); ArrayList<Student> students = new ArrayList<Student>(); while (fileScanner.hasNext()) { String line = fileScanner.nextLine(); //System.out.println(line); // assummed the data a separated by comma in the file String[] tokens = line.split(","); int grade = Integer.parseInt(tokens[2].trim()); students.add(new Student(tokens[0].trim(), tokens[1].trim(), grade)); } fileScanner.close(); return students; } }