Question

Hello, How can I make the program print out "Invalid data!!" and nothing else if the...

Hello,

How can I make the program print out "Invalid data!!" and nothing else if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero? I need this done by tomorrow if possible please.

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Updated Driver Code

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

if((grade.equals("A")||grade.equals("B")||grade.equals("C")||grade.equals("D")||grade.equals("F"))&&perecent>0){}
else{System.out.println("Invalid data in input file!!");
System.exit(0);
}

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

Add a comment
Know the answer?
Add Answer to:
Hello, How can I make the program print out "Invalid data!!" and nothing else if the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • Hello, Can you please error check my javascript? It should do the following: Write a program...

    Hello, Can you please error check my javascript? It should do the following: Write a program that uses a class for storing student data. Build the class using the given information. The data should include the student’s ID number; grades on exams 1, 2, and 3; and average grade. Use appropriate assessor and mutator methods for the grades (new grades should be passed as parameters). Use a mutator method for changing the student ID. Use another method to calculate the...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHea

    I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHeap.Java package structures; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.NoSuchElementException; public abstract class AbstractArrayHeap<P, V> {   protected final ArrayList<Entry<P, V>> heap;   protected final Comparator<P> comparator; protected AbstractArrayHeap(Comparator<P> comparator) {     if (comparator == null) {       throw new NullPointerException();     }     this.comparator = comparator;     heap = new ArrayList<Entry<P, V>>();   } public final AbstractArrayHeap<P, V> add(P priority, V value) {     if (priority == null || value...

  • I receive an error illegal start of expression starting at "public class Ingredient {". How would...

    I receive an error illegal start of expression starting at "public class Ingredient {". How would I fix this error? * * 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 SteppingStones; /** * * * * @author jennifer.cook_snhu * */ public class SteppingStone2_IngredientCalculator {     /**      *      * @param args the command line arguments     ...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • Rewrite following code down below using Factory Pattern. -------------------------Staff.java--------------------------- import java.util.*; public class Staff extends Employee...

    Rewrite following code down below using Factory Pattern. -------------------------Staff.java--------------------------- import java.util.*; public class Staff extends Employee {    private int HourlyRate;    /**Constructor Staff which initiates the values*/    public Staff() {    super();    HourlyRate=0;    }    /**Overloaded constructor method    * @param ln last name    * @param fn first name    * @param ID Employee ID    * @param sex Sex    * @param hireDate Hired Date    * @param hourlyRate Hourly rate for the work...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT