Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 18th, 2005, 9:24 AM   #1
JavaDummy
Newbie
 
Join Date: Jun 2005
Posts: 10
Rep Power: 0 JavaDummy is on a distinguished road
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
JavaDummy is offline   Reply With Quote
Old Jul 18th, 2005, 9:55 AM   #2
JavaDummy
Newbie
 
Join Date: Jun 2005
Posts: 10
Rep Power: 0 JavaDummy is on a distinguished road
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
JavaDummy is offline   Reply With Quote
Old Jul 18th, 2005, 10:08 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Tried throwing a try/catch around the entire contents of the main function and echoing the exception details?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jul 18th, 2005, 3:46 PM   #4
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 398
Rep Power: 5 xavier is on a distinguished road
Send a message via Yahoo to xavier
At line 92 and 92 :
String StudentName="",coursecode="";
int mark=0, StudentID=0;
I initialised them for you.

Oh, and please stop using those absolute paths it's anoying.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jul 30th, 2005, 5:20 PM   #5
JeffHeaton
Newbie
 
Join Date: Jul 2005
Posts: 2
Rep Power: 0 JeffHeaton is on a distinguished road
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!");
}
}
}
JeffHeaton is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC