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
  • I don't really understand how to make the scores show up and how to make it...

    I don't really understand how to make the scores show up and how to make it start over. I honestly only half understand the things I've used to piece this together to make this work so far. but this is what I was asked to do..... read a user's first and last name read three integer scores, calculate the total and average determine the letter grade based on the following criteria - Average 90-100 grade is A, 80-89.99 grade is...

  • Trying to make an autocomplete java program that given a prefix, find all strings in set...

    Trying to make an autocomplete java program that given a prefix, find all strings in set that start with said prefix, in descending weight order. I created the classes Term.java, BinarySearchDeluxe.java, and Autocomplete.java posted below. I believe Term.java is correct, but where you see "//?" needs added code. I will also list before the code, the descriptions of said methods with "//?" to explain their purpose. DO NOT add more import packages or change the main methods as that is...

  • 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     ...

  • In this exercise, we will be refactoring a working program. Code refactoring is a process to...

    In this exercise, we will be refactoring a working program. Code refactoring is a process to improve an existing code in term of its internal structure without changing its external behavior. In this particular example, we have three classes Course, Student, Instructor in addition to Main. All classes are well documented. Read through them to understand their structure. In brief, Course models a course that has a title, max capacity an instructor and students. Instructor models a course instructor who...

  • 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...

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