Question

In: Computer Science

Hello, How can I make the program print out "Invalid data!!" if the file has a...

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

Solutions

Expert Solution

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());
}
}


Related Solutions

How can I write a simple MIPS program to print out the following elements of an...
How can I write a simple MIPS program to print out the following elements of an array of the size of 10: 5,10,15,20,25,30,35,40,45,50
how to create file: vim hello<your name>.py print “Hello <your name>!” on ubuntu
how to create file: vim hello<your name>.py print “Hello <your name>!” on ubuntu
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
hello, i need to find chi square using statsgraphic. how can i upload the excel file...
hello, i need to find chi square using statsgraphic. how can i upload the excel file here. plz help....im on my last resort here. thank you.
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
Working on a program but nothing is being sent to the output file. How can I...
Working on a program but nothing is being sent to the output file. How can I fix this? import java.util.Scanner; import java.io.*; public class Topic7Hw {    static Scanner sc = new Scanner (System.in);       public static void main(String[] args) throws IOException {               PrintWriter outputFile = new PrintWriter ("output.txt");               outputFile.println("hello");               final int MAX_NUM = 10;    int[] acctNum = new int[MAX_NUM];    double[] balance = new double[MAX_NUM];...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
hello please correct this code print("The Miles Per Gallon program") print() Trips = [] trip =...
hello please correct this code print("The Miles Per Gallon program") print() Trips = [] trip = 0 while 1: print("Do you want to add a trip from a csv file or Enter it manually? 1 for csv 2 for entering it manually") method = int(input()) if method == 1: print("Enter the filename") fileName = input() try: with open(fileName, 'r') as myFile1: reader = csv.reader(myFile1) Trips = list(reader) print("Miles Driven Gallons Used \tMPG") for i in Trips: for j in i:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT