Exception Handling-Error Messages in Program

Exception Handling-Error Messages in Program

Hi Friend, I am having trouble running this code as per my prior question on Exception handling of yesterday.I may be putting into the wrong files, I'm not sure, but could you help me with this. Here is the code with the error messages as Follows: import java.util.*; class GradeException extends Exception{ public GradeException(String s){ System.out.println(s); } } This displays O.K. when compiled:

----jGRASP exec: javac -g C:\Test\GradeException.java

----jGRASP: operation complete 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{

This is where I begin to see problems with error messages as follows:

----jGRASP exec: javac -g C:\Test\TestGrade.java

TestGrade.java:17: '}' expected ^ 1 error

----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.

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

**and continues here with these error messages:

ArrayList.java:1: 'class' or 'interface' expected
ArrayList<TestGrade> list = new ArrayList<TestGrade>();
^
ArrayList.java:2: 'class' or 'interface' expected
Scanner input=new Scanner(System.in);
^
ArrayList.java:3: 'class' or 'interface' expected
int ide;
^
ArrayList.java:4: 'class' or 'interface' expected
String grade="";
^
ArrayList.java:5: 'class' or 'interface' expected
String grades[] ={"A","B","C","D","F","I"};
^
ArrayList.java:6: 'class' or 'interface' expected
List l=Arrays.asList(grades);
^
ArrayList.java:7: 'class' or 'interface' expected
for(int i=0;i<2;i++){
^
ArrayList.java:20: 'class' or 'interface' expected
for (TestGrade test : list){
^
ArrayList.java:23: 'class' or 'interface' expected
}
^
ArrayList.java:25: 'class' or 'interface' expected
^
10 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

I don't know if I am breaking them into the wrong files or not.Should the ArrayList be put in a file called ArrayList,java? I couldn't get it to run when I put them into just two files as GradeException.java and TestGrade.java.Your help is deeply appreciated.

View Answers

December 17, 2010 at 10:44 AM

Hi Friend,

Create two Java classes:

1)GradeException.java

2)TestGrade.java

GradeException.java:

class GradeException extends Exception{
public GradeException(String s){
System.out.println(s);
}
}

TestGrade.java:

import java.util.*;
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<10;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       Grade");
for (TestGrade test : list){
System.out.println(test.getId()+"        "+test.getGrade());
}
}
}

Then compile your class with javac TestGrade.java

Thanks









Related Tutorials/Questions & Answers:
Exception Handling-Error Messages in Program
Exception Handling-Error Messages in Program  Sorry about the first code block: import java.util.*; class GradeException extends Exception{ public GradeException(String s){ System.out.println(s); } } ----jGRASP exec: javac -g C
Exception Handling-Error Messages in Program
Exception Handling-Error Messages in Program  Hi Friend, I am having trouble running this code as per my prior question on Exception handling...[]) throws Exception{ This is where I begin to see problems with error messages
Advertisements
String Exception Program - JSP-Servlet
String Exception Program  Respected Sir/Madam, I am R.Ragavendran.. I got your coding for file upload and download in JSP.. Thank you very... the JasperException StringIndexOutOfBound Exception- 54164.. I am sending
Unhandled Exception
; that are thrown  during the execution of program. These exception are never caught during the execution of program. This result in nasty Exception stack. Usually this type... to catch each and every exception in the program. There are some unavoidable exception
Getting Exception on UseDefinedException sample program - Please help me how to resolve
Getting Exception on UseDefinedException sample program - Please help me how to resolve  class UseDefinedException extends Exception{ String msg...); } } } Hi, I tried above code but getting Exception : MyException cannot
Out of bounce exception in the string reverse program - Java Beginners
Out of bounce exception in the string reverse program  In the given program below this is taken from ur site but when i am executing it.It is giving an exception like string out of bounce.So please give me the correct solution
exception
exception   Create a program that takes user name as input from user... defined checked exception ââ?¬Å?InvalidCharcterFoundExceptionââ?¬Â? and creater a block of codes that will handle the exception
exception
exception  wHEN RUNTIME EXCEPTION CLASS IS SUB OF EXCEPTION CLASS HOW CAN'T HANDLE UNCHECKED EXCEPTION
exception
exception  chek in and check out exception in java   Please visit the following link: Checked and Unchecked Exception
exception
arguments are not equalto two,throw a user defined exception "invalid parameter exception" ,otherwise display the two parameters.   Here is an example... is not equal to two then invalid parameter exception is thrown ,otherwise display the two
exception
exception  what is the use of catch block even though we have predefined exception object
exception
exception  example for numberformat exception   Hi Friend, Try the following code:ADS_TO_REPLACE_1 class NumberFormatExceptionEx...); } catch(Exception e){ System.out.println(e
Exception
Exception  whis is the Arithmetic Exception in java? or define Arithmetic Exception with exp?   Arithmetic Exception occurs, when you divide a number by zero. Example public class TryCatch { public static void main
Exception
Exception  public class FooException extends Exception { public..."); } public void calculate() throws FooException, Exception { try { int...(FooException ex) { // Print error and terminate application
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
Exception   I was creating a table dynamically but it shows exception i.e shown down Suplier created0 Suplier created0 Suplier created0 Product created0 Product created0 Product created0 Product created0 could not fetch initial
exception
user defined unchecked exception  can we create user defined unchecked exceptions? if so what is the exact use of it in real time?   Please visit the following links: http://www.roseindia.net/java/java-exception/user
exception
exception  Identify the reason for SQLException exception, which is encountered when the developer tries to run the following code snippet to insert..."); ps.executeUpdate(); } catch(Exception e
Exception
package.ADS_TO_REPLACE_1 The NullPointerException is thrown by the Java program... of NullPointerException in Java program: * If you are calling some method on the null... code if you try to call the length method the Java program will though
Exception handling in java
Exception handling in java We are going to discus about Exception handling in java. Java program many provides exception. We are handle of error in program when during execution in a program .we are generate of exception try() block
ModuleNotFoundError: No module named 'messages'
ModuleNotFoundError: No module named 'messages'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named... library, ModuleNotFoundError: No module named 'messages' error
Exception handling mechanism
Exception handling mechanism In this tutorial you will learn about exception handling mechanism in java. Exception means error in your program. Exception handling mechanism is a way to handle the exception in your program. Using three
Get the error messages displayed
Get the error messages displayed  How to get the error messages displayed
Catch an Exception using EL
error codes we should try to show some friendly messages.  In this example... to catch the exception.   ADS_TO_REPLACE_1 The code of the program...Catch an Exception using EL     
ModuleNotFoundError: No module named 'django-messages'
Python program is throwing following error: ModuleNotFoundError: No module... 'django-messages' error? Thanks   Hi, In your python... named 'django-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'schema-messages'
Python program is throwing following error: ModuleNotFoundError: No module... 'schema-messages' error? Thanks   Hi, In your python... named 'schema-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'user-messages'
Python program is throwing following error: ModuleNotFoundError: No module named... 'user-messages' error? Thanks   Hi, In your python...;user-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'bodhi-messages'
Python program is throwing following error: ModuleNotFoundError: No module... 'bodhi-messages' error? Thanks   Hi, In your python... 'bodhi-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'happinesspacket-messages'
, My Python program is throwing following error: ModuleNotFoundError... the ModuleNotFoundError: No module named 'happinesspacket-messages' error? Thanks  ...;happinesspacket-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'django-messages'
Python program is throwing following error: ModuleNotFoundError: No module... 'django-messages' error? Thanks   Hi, In your python... named 'django-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'messagemedia-messages'
, My Python program is throwing following error: ModuleNotFoundError...: No module named 'messagemedia-messages' error? Thanks   Hi... library, ModuleNotFoundError: No module named 'messagemedia-messages' error
ModuleNotFoundError: No module named 'pinax-messages'
Python program is throwing following error: ModuleNotFoundError: No module... 'pinax-messages' error? Thanks   Hi, In your python... 'pinax-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'purkinje-messages'
Python program is throwing following error: ModuleNotFoundError: No module... named 'purkinje-messages' error? Thanks   Hi, In your...: No module named 'purkinje-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'teamcity-messages'
Python program is throwing following error: ModuleNotFoundError: No module... named 'teamcity-messages' error? Thanks   Hi, In your...: No module named 'teamcity-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'user-messages'
Python program is throwing following error: ModuleNotFoundError: No module named... 'user-messages' error? Thanks   Hi, In your python...;user-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'vgs-messages'
program is throwing following error: ModuleNotFoundError: No module named...;vgs-messages' error? Thanks   Hi, In your python...-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'vk-messages'
ModuleNotFoundError: No module named 'vk-messages'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named...-messages' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'bioinformagic-messages'
ModuleNotFoundError: No module named 'bioinformagic-messages'  Hi, My Python program is throwing following error: ModuleNotFoundError...: No module named 'bioinformagic-messages' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-messages'
Python program is throwing following error: ModuleNotFoundError: No module... 'django-messages' error? Thanks   Hi, In your python... named 'django-messages' error will be solved. Thanks
Constructor Exception in Java
as a parameters. The main reason of exception occurring in the program is ...;} } output of the program Exception in thread... Constructor Exception in Java   
Exception Classes
of Error family. The exception classes can be explained as well seeing... Exception Classes       The hierarchy of exception classes commence from Throwable class which
Exception Handling in JSP
Exception Handling in JSP      ... in the program. An exception can occur if you trying to connect to a database... the run- time exception. To make a program on this we are using three pages.ADS
Exception Handling
the exception handling can be done in java program. This example reads two... Exception Handling      ... are used for handling errors in programs that occurs during the program execution
Version of com.mijecu25>messages dependency
List of Version of com.mijecu25>messages dependency
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
WhatsApp messages sent with Java
WhatsApp messages sent with Java  how can I send a WhatsApp message using Java code
WhatsApp messages sent with Java
WhatsApp messages sent with Java  how can I send a WhatsApp message using Java code
ModuleNotFoundError: No module named 'django-actionable-messages'
, My Python program is throwing following error: ModuleNotFoundError... the ModuleNotFoundError: No module named 'django-actionable-messages' error? Thanks...;django-actionable-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'django-actionable-messages'
, My Python program is throwing following error: ModuleNotFoundError... the ModuleNotFoundError: No module named 'django-actionable-messages' error? Thanks...;django-actionable-messages' error will be solved. Thanks
ModuleNotFoundError: No module named 'django-ajax-messages'
, My Python program is throwing following error: ModuleNotFoundError...: No module named 'django-ajax-messages' error? Thanks   Hi..., ModuleNotFoundError: No module named 'django-ajax-messages' error

Ads