In: Computer Science
Hello,
How can I make the program print out "Invalid data!!" if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero.
The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student grade.
Smith Kelly 438975 98.6 A
Johnson Gus 210498 72.4 C
Reges Stu 098736 88.2 B
Smith Marty 346282 84.1 B
Reges Abe 298575 78.3 C
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is the program.
DRIVER CLASS:
package pkg13_3;
import java.util.*;
import java.io.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws
FileNotFoundException,
InputMismatchException{
try
{
Scanner scan = new Scanner(new File("files.txt"));
ArrayList <Student> elements = new
ArrayList<Student>();
while(scan.hasNext())
{
String last = scan.next();
String first = scan.next();
int idNum = scan.nextInt();
double perecent = scan.nextDouble();
String grade = scan.next();
elements.add(new Student(last, first, idNum, perecent,
grade));
}
Student[]element = new Student[elements.size()];
for(int i = 0; i<element.length;i++)
{
element[i] = elements.get(i);
}
System.out.println("Student data by last name: ");
Arrays.sort(element, new LastNameComparator());
for(int i = 0; i<element.length; i++)
{
System.out.println(element[i].toString());
}
System.out.println("Student data by first name: ");
Arrays.sort(element, new FirstNameComparator());
for(int i = 0; i<element.length; i++)
{
System.out.println(element[i].toString());
}
System.out.println("Student data by student id: ");
Arrays.sort(element, new IDComparator());
for(int i = 0; i<element.length; i++)
{
System.out.println(element[i].toString());
}
System.out.println("Student data by percentage: ");
Arrays.sort(element, new PercentageComparator());
for(int i = 0; i<element.length; i++)
{
System.out.println(element[i].toString());
}
System.out.println("Student data by percentage: ");
for(int i = 0; i<element.length; i++)
{
System.out.println(element[i].toString());
}
System.out.println("Student data by grade: ");
Arrays.sort(element, new GradeComparator());
for(int i = 0; i<element.length; i++)
{
System.out.println(element[(element.length - 1)
-i].toString());
}
break;
}
catch(FileNotFoundException e)
{
System.out.println(e.toString());
}
catch(InputMismatchException f)
{
System.out.println(f.toString());
}
}
}
STUDENT CLASS:
package pkg13_3;
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user
*/
public class Student {
String firstName, lastName;
int idNum;
double percent;
String grade;
public Student(String myFirstName, String myLastName, int id,
double pct,
String myGrade)
{
lastName = myFirstName;
firstName = myLastName;
idNum = id;
percent = pct;
grade = myGrade;
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public int getID()
{
return idNum;
}
public double getPercentage()
{
return percent;
}
public String getGrade()
{
return grade;
}
public String toString()
{
return lastName + "\t" + firstName + "\t" + idNum + "\t" +
percent + "\t" + grade;
}
}
-------------------------------------------------------------------------------------
LastNameComparator:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class LastNameComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return
student1.getLastName().compareTo(student2.getLastName());
}
}
-----------------------------------------------------------------------------------------------------------
First name comparator
--------------------------------------------------------------------------------------------------------------
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class FirstNameComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return
student1.getFirstName().compareTo(student2.getFirstName());
}
}
---------------------------------------------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class IDComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return student1.getID() - (student2.getID());
}
}
-------------------------------------------------------------------------------------------------
Percentage Comparator
_____________________________________________________________
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class PercentageComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return (int)(student1.getPercentage() -
(student2.getPercentage()));
}
}
------------------------------------------------------------------------------------------------------
Grade Comparator:
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class GradeComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return student1.getGrade().compareTo(student2.getGrade());
}
}
_____________________________________________________________
Thank you
If you have any doubts, please give me comment...
DRIVER CLASS:
package pkg13_3;
import java.util.*;
import java.io.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, InputMismatchException {
try {
Scanner scan = new Scanner(new File("files.txt"));
ArrayList<Student> elements = new ArrayList<Student>();
while (scan.hasNext()) {
String last = scan.next();
String first = scan.next();
int idNum = scan.nextInt();
double perecent = scan.nextDouble();
String grade = scan.next().toUpperCase();
if ((grade.equals("A") || grade.equals("B") || grade.equals("C") || grade.equals("D")
|| grade.equals("E") || grade.equals("F")) && perecent >= 0)
elements.add(new Student(last, first, idNum, perecent, grade));
else
System.out.println("Invalid data!");
}
Student[] element = new Student[elements.size()];
for (int i = 0; i < element.length; i++) {
element[i] = elements.get(i);
}
System.out.println("Student data by last name: ");
Arrays.sort(element, new LastNameComparator());
for (int i = 0; i < element.length; i++) {
System.out.println(element[i].toString());
}
System.out.println("Student data by first name: ");
Arrays.sort(element, new FirstNameComparator());
for (int i = 0; i < element.length; i++) {
System.out.println(element[i].toString());
}
System.out.println("Student data by student id: ");
Arrays.sort(element, new IDComparator());
for (int i = 0; i < element.length; i++) {
System.out.println(element[i].toString());
}
System.out.println("Student data by percentage: ");
Arrays.sort(element, new PercentageComparator());
for (int i = 0; i < element.length; i++) {
System.out.println(element[i].toString());
}
System.out.println("Student data by percentage: ");
for (int i = 0; i < element.length; i++) {
System.out.println(element[i].toString());
}
System.out.println("Student data by grade: ");
Arrays.sort(element, new GradeComparator());
for (int i = 0; i < element.length; i++) {
System.out.println(element[(element.length - 1) - i].toString());
}
} catch (FileNotFoundException e) {
System.out.println(e.toString());
}
catch (InputMismatchException f) {
System.out.println(f.toString());
}
}
}
STUDENT CLASS:
package pkg13_3;
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user
*/
public class Student {
String firstName, lastName;
int idNum;
double percent;
String grade;
public Student(String myFirstName, String myLastName, int id,
double pct,
String myGrade)
{
lastName = myFirstName;
firstName = myLastName;
idNum = id;
percent = pct;
grade = myGrade;
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public int getID()
{
return idNum;
}
public double getPercentage()
{
return percent;
}
public String getGrade()
{
return grade;
}
public String toString()
{
return lastName + "\t" + firstName + "\t" + idNum + "\t" +
percent + "\t" + grade;
}
}
-------------------------------------------------------------------------------------
LastNameComparator:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class LastNameComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return
student1.getLastName().compareTo(student2.getLastName());
}
}
-----------------------------------------------------------------------------------------------------------
First name comparator
--------------------------------------------------------------------------------------------------------------
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class FirstNameComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return
student1.getFirstName().compareTo(student2.getFirstName());
}
}
---------------------------------------------------------------------------------------------------------------
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class IDComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return student1.getID() - (student2.getID());
}
}
-------------------------------------------------------------------------------------------------
Percentage Comparator
_____________________________________________________________
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class PercentageComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return (int)(student1.getPercentage() -
(student2.getPercentage()));
}
}
------------------------------------------------------------------------------------------------------
Grade Comparator:
package pkg13_3;
import java.util.*;
/**
*
* @author user
*/
public class GradeComparator implements Comparator<
Student>{
public int compare(Student student1, Student student2)
{
return student1.getGrade().compareTo(student2.getGrade());
}
}