Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Exception Handling
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Andrew Meikle
Exception Handling
2 Answer(s)      4 years and a month ago
Posted in : Java Beginners


Hi, I really appreciate the help I have gotten,I have 3 more problems to go by Monday.I will need help with all three. I hope someone is willing to help. So far I have found this web site to be the best for help, other sites I have tried only look down at you and give you attitude.I got sick mid way through this course and have found it impossible to catch up with all my other classes and the semester closes Monday.In this problem I have to write an application that displays a series of at least 10 student ID numbers (that are to be stored in an array) and asks the user to enter a test letter grade for the student. Create an Exception class named GradeException that contains a static public array of valid letter grades('A''B','C','D','F',and 'I'), which you can use to determine whether a grade entered from the application is valid. In the application, throw a Grade Exception if the user does not enter a valid letter grade. Catch the GradeException, and then display an appropriate message.In addition, store an 'I' for (incomplete)for any student whom an exception is caught. At the end of the application, display all the student IDs and grades. Save them as GradeException.java and TestGrade.java.I have got the code for 1 student, but I'm not sure about array and the rest.I think I have figured out the other two questions, but I am still stuck on this one. Here is the code I have:

public class GradeException extends Exception

{

public GradeException(String s)

{

super(s);

}

}


public class TestGrade

{

public static void main(String args[]) throws Exception

{

int id;

int grade;

String grades = new String();

try{

id = 123;

System.out.println("Student id number: " + id);

System.out.println("Enter the grade for the above student id number.");

grade = (char)System.in.read();

grades = String.valueOf((char)grade);

if(grade != 'A' && grade != 'B' && grade != 'C' &&

grade != 'D' && grade != 'F'&& grade !='I')

throw (new GradeException(grades));

System.out.println("The id and grade for this student is:");

System.out.println(" " + id + " " + (char)grade);

}

catch(GradeException e)

{

System.out.println("Invalid grade - " + e.getMessage());

}

}

}

Advertisement
View Answers

December 16, 2010 at 11:23 AM


Hi Friend,

Try the following code:

import java.util.*;
class GradeException extends Exception{
public GradeException(String s){
System.out.println(s);
}
}
public class TestGrade{
int id;

String grade;
TestGrade(int id,String grade){
    this.id=id;
    this.grade=grade;
}
public int getId(){
    return id;
}

public String getGrade(){
    return grade;
}
public static void main(String args[]) throws Exception{
ArrayList<TestGrade> list = new ArrayList<TestGrade>();
Scanner input=new Scanner(System.in);
int ide;
String grade="";
String grades[] ={"A","B","C","D","F","I"};
List l=Arrays.asList(grades);
for(int i=0;i<2;i++){

System.out.println("Enter Student id: ");
ide=input.nextInt();
System.out.println("Enter the grade for the above student id number:");
grade = input.next();
if(!l.contains(grade)){
    new GradeException("You have entered invalid grade!Re-enter Grade:");
    grade = input.next();
}
list.add(new TestGrade(ide,grade));
}
System.out.println("ID       Marks");
for (TestGrade test : list){
System.out.println(test.getId()+"        "+test.getGrade());
}
}
}

Thanks



December 18, 2010 at 1:05 PM


Hi Friend, I am truly indebted to you. I had tried running the application and saving the files as GradeException and TestGrade as you suggested the first time around. You were absolutely correct, the problem I had was that I pivked up something when I copied and pasted. When I retyped the code and ran it as such it worked perfectly. Nonetheless, I got the assignment in on time along with my others and passed the class. I thank you very much.



Related Tutorials/Questions & Answers:
exception handling
exception handling  explain about exception handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
Advertisements
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  could you please tell me detail the concept of exception handling
exception handling
exception handling   Give the exception hierarchy of Java.   Please visit the following links: http://www.roseindia.net/java/java-exception/exception-java.shtml http://www.roseindia.net/java/exceptions/exception
exception handling
exception handling  What is an exception and what are the advantages of exception handling mechanism.   Java Exception Exception are the runtime errors. Advantages: 1)Exception provides the means to separate
Exception handling
Exception handling  how to resolve file not found error exception
Exception Handling
Exception Handling  create an exception class, which thros an exception if operand is non-numeric in claculating modules. ( Use command line arguments
exception handling
exception handling  can you explain how to handle the exception in java with picture representation
Exception handling
Exception handling  exception are occur at run time but when we connect to the database errors will come at compile time why
Exception Handling
Exception Handling  wrtie a java program to enable the user to handle any change of divide by zero exception.   Please visit the following link: http://www.roseindia.net/java/exceptions/how-to-throw-exceptions.shtml
exception handling
exception handling  a program to accept only three integer values as command line arguments. Print the values entered by the user. Handle ArrayIndexOutOfBoundsException and NumberFormatException by providing the applied messages
exception Handling
exception Handling  write a program in Java to display the names and roll numbrs of students. Initialize respective array variables for 10 students. Handle ArrayIndexOutOfBoundsException, so that any such problem doesn't cause
Exception Handling
Exception Handling  Hi, I really appreciate the help I have gotten,I... for the student. Create an Exception class named GradeException that contains.... In the application, throw a Grade Exception if the user does not enter a valid letter
Exception Handling
Exception Handling  Hi, I really appreciate the help I have gotten,I... for the student. Create an Exception class named GradeException that contains.... In the application, throw a Grade Exception if the user does not enter a valid letter
exception handling in php - PHP
exception handling in php  i need few examples on exception handling in php
Exception Handling in Struts.
Exception Handling in Struts.  How you can do Exception Handling in Struts
Java exception handling
Java exception handling  what are the constraints imposed by overriding on exception handling
exception handling mechanism
exception handling mechanism  Explain Java's exception handling mechanism in detail.   Please visit the following links: http.../exceptions/catching-and-handling-exceptions.shtml
Try and catch in Exception Handling.
Try and catch in Exception Handling.  How can we use try and catch block in exception handling?   Here is an example of Exception handling.... Then to perform exception handling, we have used try and catch block where we have
Exception Handling in java
Exception Handling in java  what is advantage to catch smaller exception first and then normal exception. I mean we normally catch SQLException first and then Exception.   Hi, The exceptions should be from
array in exception handling
array in exception handling  write java program to declare and initialize an array with the number given, 4,6,3,0,5,5,12 then the program will ask user to enter indexs number to display its value .using exception handling
Exception Handling - Java Beginners
Exception Handling  hi,can u pls make me understand d clear cut difference between throw n throws keyword... n how can we make our own exception..i... want to force an exception then we use throw keyword. the throw keyword is used
Advertisements
 

 

 

DMCA.com