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