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
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,...
I need original java code that completes this program and gets it to print out results...
I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only. Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times. Each time the search is conducted the number of checks (IE...
Create a program with the Calculator.java code RUN the program with the invalid data arguments 1,...
Create a program with the Calculator.java code RUN the program with the invalid data arguments 1, 3, 5x Take a screenshot of the output and describe what you think happened. MODIFY the code as in listing 14.2, NewCalculator.java , RUN it again with the same invalid input Take a screenshot of the new output and describe in your word document why you think the exception handling is a 'better' programming technique than just letting the program crash. __________________________________________________________________________ The Calculator.java...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT