![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 10
Rep Power: 0
![]() |
Try without catch
Hi there,
Having some problems with this Student Menu i've been doing. The menu works now, but on the final method ( Report on Courses ), i get a try without catch error, when both are stated and initilised. import java.io.*;
public class StudentMenu {
public static void main (String args[]) throws IOException
{
PrintWriter Student = new PrintWriter (new FileWriter("F:/Students.txt"));
String choicemenu;
int choice;
boolean Finished = false;
DataInputStream kbd = new DataInputStream(System.in);
while (Finished == false) {
System.out.println(" ******Student Data******");
System.out.println(" Please make a choice ");
System.out.println(" 1 to 4 ");
System.out.println(" 1. Add Data ");
System.out.println(" 2. Find Data ");
System.out.println(" 3. Report On Courses ");
System.out.println(" 4. Quit ");
choicemenu = kbd.readLine();
choice = Integer.parseInt(choicemenu);
switch (choice) {
case 1: {
AddData ();
};break;
case 2: {
FindData ();
};break;
case 3: {
Report ();
};break;
case 4: {
Finished = true;
}
;break;
default: System.out.println("That's not a valid option");
;break ;
}
}
}
public static void AddData () throws java.io.IOException{
String StudentName,temp;
String StudentNameTemp;
int StudentID;
String StudentIDTemp;
int x,mark;
String marktemp;
int coursecode;
String coursecodeTemp;
int Finished = 1;
String f = "F:/Students.txt";
PrintWriter out = new PrintWriter(new FileWriter(f));
DataInputStream kbd = new DataInputStream(System.in);
while (Finished == 1) {
System.out.println("Add Data Selected");
System.out.println(" Enter Student Name ");
StudentName = kbd.readLine();
System.out.println(" Enter mark ");
marktemp = kbd.readLine();
mark = Integer.parseInt(marktemp);
System.out.println("Enter Course Code");
coursecodeTemp = kbd.readLine();
coursecode = Integer.parseInt(coursecodeTemp);
System.out.println("Enter Student ID");
StudentIDTemp = kbd.readLine();
StudentID = Integer.parseInt(StudentIDTemp);
out.println(StudentName);
out.println(mark);
out.println(coursecode);
out.println(StudentID);
System.out.println("Continue Y =1 N = 2");
temp = kbd.readLine();
x = Integer.parseInt(temp) ;
if (x == 2)
Finished = -1;
}
out.close();
}
public static void FindData () {
System.out.println("Find Data");
String filenames = "F:/Students.txt";
String StudentName,coursecode;
int mark, StudentID;
int code;
int count = 0;
String course;
DataInputStream kbd = new DataInputStream(System.in);
try {
System.out.println("Enter Course Code");
course = kbd.readLine();
code = Integer.parseInt(course);
FileReader file = new FileReader("F:/Students.txt");
BufferedReader in = new BufferedReader(file);
String line;
while((line = in.readLine()) !=null) {
if (count % 4 == 0) StudentName = line;
if (count % 4 == 1) mark = Integer.parseInt(line);
if (count % 4 == 2) coursecode = line;
if (count % 4 == 3) StudentID = Integer.parseInt(line);
if (coursecode.compareTo(course) == 0) {
System.out.println(StudentName);
System.out.println(mark);
System.out.println(StudentID);
}
}
in.close();
}
catch (IOException e) {
System.out.println("Input file not found!");
}
}
public static void Report () {
String StudentName = null;
int coursecode = 0;
int StudentID = 0;
int mark = 0;
try {
System.out.println("Print report on Courses");
FileReader file = new FileReader("F:/Students.txt");
BufferedReader in = new BufferedReader(file);
String line;
while((line = in.readLine()) !=null) {
System.out.print("coursecode ");
System.out.print("StudentName ");
System.out.print("StudentID ");
System.out.print("mark ");
System.out.print(coursecode);
System.out.print(StudentName);
System.out.print(StudentID);
System.out.print(mark);
}
in.close();
catch (IOException e) {
System.out.println("Input file not found!");
}
}
}
}This is the error I get: F:\HND Computing\Unit 8 - Introduction to Programming\StudentMenu.java:149: 'catch' without 'try' catch (IOException e) { ^ F:\HND Computing\Unit 8 - Introduction to Programming\StudentMenu.java:132: 'try' without 'catch' or 'finally' try { ^ 2 errors Tool completed with exit code 1 - I can't seem to eradicate the error and it's stopping the whole thing from working. Any ideas? Steve |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jun 2005
Posts: 10
Rep Power: 0
![]() |
This is my code now, slightly different:
import java.io.*;
public class StudentMenu {
public static void main (String args[]) throws IOException
{
PrintWriter Student = new PrintWriter (new FileWriter("F:/Students.txt"));
String choicemenu;
int choice;
boolean Finished = false;
DataInputStream kbd = new DataInputStream(System.in);
while (Finished == false) {
System.out.println(" ******Student Data******");
System.out.println(" Please make a choice ");
System.out.println(" 1 to 4 ");
System.out.println(" 1. Add Data ");
System.out.println(" 2. Find Data ");
System.out.println(" 3. Report On Courses ");
System.out.println(" 4. Quit ");
choicemenu = kbd.readLine();
choice = Integer.parseInt(choicemenu);
switch (choice) {
case 1: {
AddData ();
};break;
case 2: {
FindData ();
};break;
case 3: {
Report ();
};break;
case 4: {
Finished = true;
}
;break;
default: System.out.println("That's not a valid option");
;break ;
}
}
}
public static void AddData () throws java.io.IOException{
String StudentName,temp;
String StudentNameTemp;
int StudentID;
String StudentIDTemp;
int x,mark;
String marktemp;
int coursecode;
String coursecodeTemp;
int Finished = 1;
String f = "F:/Students.txt";
PrintWriter out = new PrintWriter(new FileWriter(f));
DataInputStream kbd = new DataInputStream(System.in);
while (Finished == 1) {
System.out.println("Add Data Selected");
System.out.println(" Enter Student Name ");
StudentName = kbd.readLine();
System.out.println(" Enter mark ");
marktemp = kbd.readLine();
mark = Integer.parseInt(marktemp);
System.out.println("Enter Course Code");
coursecodeTemp = kbd.readLine();
coursecode = Integer.parseInt(coursecodeTemp);
System.out.println("Enter Student ID");
StudentIDTemp = kbd.readLine();
StudentID = Integer.parseInt(StudentIDTemp);
out.println(StudentName);
out.println(mark);
out.println(coursecode);
out.println(StudentID);
System.out.println("Continue Y =1 N = 2");
temp = kbd.readLine();
x = Integer.parseInt(temp) ;
if (x == 2)
Finished = -1;
}
out.close();
}
public static void FindData () {
System.out.println("Find Data");
String filenames = "F:/Students.txt";
String StudentName,coursecode;
int mark, StudentID;
int code;
int count = 0;
String course;
DataInputStream kbd = new DataInputStream(System.in);
try {
System.out.println("Enter Course Code");
course = kbd.readLine();
code = Integer.parseInt(course);
FileReader file = new FileReader("F:/Students.txt");
BufferedReader in = new BufferedReader(file);
String line;
while((line = in.readLine()) !=null) {
if (count % 4 == 0) StudentName = line;
if (count % 4 == 1) mark = Integer.parseInt(line);
if (count % 4 == 2) coursecode = line;
if (count % 4 == 3) StudentID = Integer.parseInt(line);
if (coursecode.compareTo(course) == 0) {
System.out.println(StudentName);
System.out.println(mark);
System.out.println(StudentID);
}
}
in.close();
}
catch (IOException e) {
System.out.println("Input file not found!");
}
}
public static void Report () {
String StudentName = null;
int coursecode = 0;
int StudentID = 0;
int mark = 0;
System.out.println("Print report on Courses");
try {
FileReader file = new FileReader("F:/Students.txt");
BufferedReader in = new BufferedReader(file);
String line;
while((line = in.readLine()) !=null) {
System.out.print("coursecode ");
System.out.print("StudentName ");
System.out.print("StudentID ");
System.out.print("mark ");
System.out.print(coursecode);
System.out.print(StudentName);
System.out.print(StudentID);
System.out.print(mark);
}
in.close();
}
catch (IOException e) {
System.out.println("Input file not found!");
}
}
}Now it tells me when i try and run it it tells me there's an exception in the main thread - the only other errors that occur are that it says some of the variables might not have been initiliased - which of course they have. I am getting very frustrated with this. Please help Steve |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Tried throwing a try/catch around the entire contents of the main function and echoing the exception details?
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
At line 92 and 92 :
String StudentName="",coursecode=""; int mark=0, StudentID=0; Oh, and please stop using those absolute paths it's anoying.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2005
Posts: 2
Rep Power: 0
![]() |
Try it like this, compiles just fine now.
Jeff http://www.heatonresearch.com import java.io.*;
public class StudentMenu {
public static void main (String args[]) throws IOException
{
PrintWriter Student = new PrintWriter (new FileWriter("F:/Students.txt"));
String choicemenu;
int choice;
boolean Finished = false;
DataInputStream kbd = new DataInputStream(System.in);
while (Finished == false) {
System.out.println(" ******Student Data******");
System.out.println(" Please make a choice ");
System.out.println(" 1 to 4 ");
System.out.println(" 1. Add Data ");
System.out.println(" 2. Find Data ");
System.out.println(" 3. Report On Courses ");
System.out.println(" 4. Quit ");
choicemenu = kbd.readLine();
choice = Integer.parseInt(choicemenu);
switch (choice) {
case 1: {
AddData ();
};break;
case 2: {
FindData ();
};break;
case 3: {
Report ();
};break;
case 4: {
Finished = true;
}
;break;
default: System.out.println("That's not a valid option");
;break ;
}
}
}
public static void AddData () throws java.io.IOException{
String StudentName,temp;
String StudentNameTemp;
int StudentID;
String StudentIDTemp;
int x,mark;
String marktemp;
int coursecode;
String coursecodeTemp;
int Finished = 1;
String f = "F:/Students.txt";
PrintWriter out = new PrintWriter(new FileWriter(f));
DataInputStream kbd = new DataInputStream(System.in);
while (Finished == 1) {
System.out.println("Add Data Selected");
System.out.println(" Enter Student Name ");
StudentName = kbd.readLine();
System.out.println(" Enter mark ");
marktemp = kbd.readLine();
mark = Integer.parseInt(marktemp);
System.out.println("Enter Course Code");
coursecodeTemp = kbd.readLine();
coursecode = Integer.parseInt(coursecodeTemp);
System.out.println("Enter Student ID");
StudentIDTemp = kbd.readLine();
StudentID = Integer.parseInt(StudentIDTemp);
out.println(StudentName);
out.println(mark);
out.println(coursecode);
out.println(StudentID);
System.out.println("Continue Y =1 N = 2");
temp = kbd.readLine();
x = Integer.parseInt(temp) ;
if (x == 2)
Finished = -1;
}
out.close();
}
public static void FindData () {
System.out.println("Find Data");
String filenames = "F:/Students.txt";
String StudentName,coursecode;
int mark, StudentID;
int code;
int count = 0;
String course;
DataInputStream kbd = new DataInputStream(System.in);
try {
System.out.println("Enter Course Code");
course = kbd.readLine();
code = Integer.parseInt(course);
FileReader file = new FileReader("F:/Students.txt");
BufferedReader in = new BufferedReader(file);
String line;
while((line = in.readLine()) !=null) {
if (count % 4 == 0) StudentName = line;
if (count % 4 == 1) mark = Integer.parseInt(line);
if (count % 4 == 2) coursecode = line;
if (count % 4 == 3) StudentID = Integer.parseInt(line);
if (coursecode.compareTo(course) == 0) {
System.out.println(StudentName);
System.out.println(mark);
System.out.println(StudentID);
}
}
in.close();
}
catch (IOException e) {
System.out.println("Input file not found!");
}
}
public static void Report () {
String StudentName = null;
int coursecode = 0;
int StudentID = 0;
int mark = 0;
System.out.println("Print report on Courses");
try {
FileReader file = new FileReader("F:/Students.txt");
BufferedReader in = new BufferedReader(file);
String line;
while((line = in.readLine()) !=null) {
System.out.print("coursecode ");
System.out.print("StudentName ");
System.out.print("StudentID ");
System.out.print("mark ");
System.out.print(coursecode);
System.out.print(StudentName);
System.out.print(StudentID);
System.out.print(mark);
}
in.close();
}
catch (IOException e) {
System.out.println("Input file not found!");
}
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|