Home Answers Viewqa Java-Beginners Exception Handling-Error Messages in Program

 
 


Andrew Meikle
Exception Handling-Error Messages in Program
1 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners

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 Pages:
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
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   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
package. The NullPointerException is thrown by the Java program at the time... in Java program: * If you are calling some method on the null object... to call the length method the Java program will though NullPointerException
Catch an Exception using EL
the exception.    The code of the program is given below:    ...Catch an Exception using EL     ... an exception by using the EL. We can't prevent errors, but of course we can
Write Log Records to Standard Error in Java
to standard error in Java. Java provides the facility for handling an error through the help of ConsoleHandler class. This program constructs a logger and prints... Write Log Records to Standard Error in Java   
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
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
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  write a program in Java to display the names and roll numbrs of students. Initialize respective array variables for 10 students... illegal terminations of program
exception handling
of exception handling mechanism.   Java Exception Exception... an exception whenever a calling method encounters an error providing... the working code and the error-handling code can be disintegrated. It also gives us
Jfree exception
void main(String arg[])throws Exception{ Class.forName...); frame1.setVisible(true); frame1.setSize(300,300); } } while running the program....... Exception in thread "main" java.lang.ClassNotFoundException
Jfree exception
void main(String arg[])throws Exception{ Class.forName...); frame1.setVisible(true); frame1.setSize(300,300); } } while running the program....... Exception in thread "main" java.lang.ClassNotFoundException
Java Exception
. You must have encountered the most common exception in your program i.e.... exception in your program i.e. the ArithmeticException. I am sure you must... during the compile time of the program. The compiler checks at the compile time
exception in java
exception in java  StringIndexOutOfBounds Exception in flames program   Hi Friend, We have providing you a program. Try it: import... are same"; else return "Error in finding flame
exception - MobileApplications
exception  Hi friend, again i am getting exception.Entire code of my program i am sending.Plz suggest me. import javax.microedition.midlet...."); } // System.out.println("hai"); catch (Exception e
Updating variables in a loop? and only printing certain messages after the first iteration?!
Updating variables in a loop? and only printing certain messages after the first iteration?!  I've written a program that reads two values. I then want it to repeatedly read a further two values until someone enters 0
program
;/html>"); } //try catch(Exception e) { e.printStackTrace
Java Exception - Exception Handling in Java
code you will see that how the exception handling can be done in java program... Java Exception - Exception Handling in Java  ... the program execution. During the program execution if any error occurs and you
exception in java - Java Beginners
exception in java  exception in java  Hi Friend, Exceptions are the errors that occur during the execution of the program. Exceptions... of the program's instructions. For more information, visit the following link
Java Exception - Java Beginners
as throw use to make the related exception in our program. where as throws used... thatr one must inform that exception using throws.see below program class..." it leads to compiletime error: unreported exception. main use of try/catch
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
Java Mail exception - JavaMail
) program using JSP technology. Till now i dont have any problem. From last 3 days while i am connecting to the Gmail, I am getting the following exception... Whats wrong... I dont know.. How to avoid this exception. The exception
Get the error messages displayed
Get the error messages displayed  How to get the error messages displayed
wlinitialcontextfactory instantiation exception - EJB
to execute Client program of simple EJB program its giving me exception like..._CLASSES then I am not getting any exception and also not getting the result
Exception Classes
of Error family. The exception classes can be explained as well seeing... Exception only and not the Throwable or Error class... Exception Classes      
bean creation exception
bean creation exception  hi i am getting exception while running simple spring ioc program Exception in thread "main... path resource [beans.xml]; nested exception is java.io.FileNotFoundException
Exception - Java Interview Questions
Exception in Java programming  What is Exception?  Chained Exception in JavaChained Exception in Java, this is a new in release 1.4... of exception. In simple words chain exception means chain of exceptions
Out Of Memory Exception Erorr
Out Of Memory Exception Erorr  hello how are you?I hope you are fine I'm using jgrasp program my problem is in java programming my program compiled with no problem but when I run it this exception appears: Exception
Exception in RMI - RMI
("HelloClient exception: " + e);} } } This is my Client program...Exception in RMI  package ems; import java.io.IOException; import... is connected and ready for operation."); } catch (Exception e
Java Stream Write Exception
was written successfully and sent to the server program try { //my socket..."; System.out.println(data1); out.writeUTF(data1); this line causes the error } catch(Exception e) { System.out.println("Exception :: "+e); } When the line
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
Nullpointer Exception - JSP-Servlet
of the servlet program? how can i solve this exception ? HTTP Status 500... Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception
Exception - Java Beginners
Exception  HI its me again:)can i ask for a program in which you... (inclusive), you want to throw your own exception. This means you will have to create your own exception class called NotCorrectAgeException and The main
exception
exception  wHEN RUNTIME EXCEPTION CLASS IS SUB OF EXCEPTION CLASS HOW CAN'T HANDLE UNCHECKED EXCEPTION
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
throws Exception - RMI
throws Exception  I downloaded your RMI Hello program it works properly. With that same program I replaced for my application. But if run the Server... an exception."RMI not bound Exception". What can I do for this? Whether I have to change
Exception - Java Interview Questions
Java Exception  My java program is throwing an Exception ..can anyone tell me how to track those exceptions?  An error will occur showing... after try.If its not aviliable the above error ill be returned by jvm(which
List of Java Exception
the program error during compilation. For Example when you divide a number by zero... List of Java Exception       Exception in Java are classified on the basis of 
List of Java Exception
. This exception is thrown when there is an error in input-output operation. In this case... not check the program error during compilation. For Example when you divide a number... List of Java Exception     
userdefined exception - Java Beginners
userdefined exception  Write a simple program to create user defined exception?  hi import java.io.*;import java.io.IOException;import java.io.File.*;public class ExceptionExam{ public static void main(String[] args
exception
exception  chek in and check out exception in java   Please visit the following link: Checked and Unchecked Exception
java program - Java Beginners
they were entered. Proper error messages should be displayed if: i) command... = Integer.parseInt(args[i]); } catch(Exception e) { System.out.println
Constructor Exception in Java
as a parameters. The main reason of exception occurring in the program is ...;} } output of the program Exception in thread "main... Constructor Exception in Java   
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 - JSP-Servlet
Exception  Hi team, This is Ragavendran.R. I am getting java.lang.StringIndexOutOfBoundsException: String out of range : 6 error while compiling the following program in JSP charting application: Plz reply
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: class NumberFormatExceptionEx{ public static void...); } 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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.