Java, calling methods and throwing IOExceptions

Yussef

Fledgling Freddie
Joined
Dec 22, 2003
Messages
789
This is my method with the file output:

Code:
	public static void Stock() throws IOException
  	
  	{
  		FileWriter writer = new FileWriter("Stock.rtf");
  		PrintWriter printer = new PrintWriter(writer);
  
  		printer.print(Order);	
  		printer.close();
  	}

When I try calling it, by using Stock(); I get the following error:

unreported exception java.io.IOException; must be caught or declared to be thrown

I've tried Stock() throws IOException; etc, but no luck. Can anyone suggest some solutions?
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
Try:
Code:
//At the top
import java.io.FileNotFoundException;
import java.io.IOException;


//The method
public static void Stock()  {
    try {
        FileWriter writer = new FileWriter("Stock.rtf");
        PrintWriter printer = new PrintWriter(writer);
        printer.print(Order);	
        printer.close();
    } catch(FileNotFoundException fex) {
        System.out.println("File don't exist");
        System.exit(1);
    } catch(IOException ioe) {
        System.out.println("IO Error: "+ ioe.getMessage());
        System.exit(1);
    }
}
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,330
Jingle is the man.

Incidentally, do you actually need to import the exceptions? In my experience I've never needed to, just try-catch with the error declared in the catch section.
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
Yes, you could just use:
Code:
catch(java.io.IOException ioe) {
    System.out.println("IO Error: "+ ioe.getMessage());
    System.exit(1);
}

I just prefer to see what classes I use at the top in the import bit. I also declare each class rather than "import java.io.*" as I was told at work that it makes it more maintainable as someone else can instantly see what your classes are without having to hunt through the packages to see which one you used.
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
Alternatively, if you want to use throws IOException you would use:

Code:
public class FileIOTest {
	
	public static void main(String[] args) {
		//Enclose the method call in try catch block as it needs to catch the exception Stock() throws
		try {
			stock();
		} catch(FileNotFoundException fnfe) {
			System.out.println("An Error was thrown by Stock()");
			fnfe.printStackTrace();
		} catch(IOException ioe) {
			System.out.println("An Error was thrown by Stock()");
			ioe.printStackTrace();
		}
	}

	public static void stock() throws FileNotFoundException, IOException {
		FileWriter writer = new FileWriter("Stock.rtf");
		PrintWriter printer = new PrintWriter(writer);
		
		printer.print(Order);	
		printer.close();
		writer.close(); //I think this should be added for completeness
	}
}

BTW, i'd recommend you rename Stock() to stock() as recommended in the Sun Coding Conventions.
 

Yussef

Fledgling Freddie
Joined
Dec 22, 2003
Messages
789
Thanks, will try some of the suggest methods soon. I haven't covered try/catch, just started on file I/O for a project. Guess I'll buy a fatter textbook :)
 

Yussef

Fledgling Freddie
Joined
Dec 22, 2003
Messages
789
I used the method that Jingles provided and it works, thanks for the help everyone!
 
N

noname

Guest
I need help on some code as well

My class was given the following code and told to split it up into other classes::::



=================================================
// Lab07st


import java.io.*;

public class Lab07st
{

public static void main (String args[]) throws IOException

{

System.out.println("\nLAB07 90 POINT VERSION\n\n");

BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

System.out.println("\nLAB07 100 POINT VERSION\n\n");

System.out.print("Enter Square Side ===>> ");

String strSqSide = keyboard.readLine();

System.out.print("Enter Rectangle Width ===>> ");

String strRectWidth = keyboard.readLine();

System.out.print("Enter Rectangle Height ===>> ");

String strRectHeight = keyboard.readLine();

System.out.print("Enter Triangle Base ===>> ");

String strTriBase = keyboard.readLine();

System.out.print("Enter Triangle Height ===>> ");

String strTriHeight = keyboard.readLine();

System.out.print("Enter Trapezoid Base1 ===>> ");

String strTrapBase1 = keyboard.readLine();

System.out.print("Enter Trapezoid Base2 ===>> ");

String strTrapBase2 = keyboard.readLine();

System.out.print("Enter Trapezoid Height ===>> ");

String strTrapHeight = keyboard.readLine();

System.out.print("Enter Circle Radius ===>> ");

String strCircRadius = keyboard.readLine();

double sqSide = Double.parseDouble(strSqSide);

double rectWidth = Double.parseDouble(strRectWidth);

double rectHeight = Double.parseDouble(strRectHeight);

double triBase = Double.parseDouble(strTriBase);

double triHeight = Double.parseDouble(strTriHeight);

double trapBase1 = Double.parseDouble(strTrapBase1);

double trapBase2 = Double.parseDouble(strTrapBase2);

double trapHeight = Double.parseDouble(strTrapHeight);

double circRadius = Double.parseDouble(strCircRadius);

double areaSquare = sqSide * sqSide;

double areaRectangle = rectWidth * rectHeight;

double areaTriangle = triBase / 2 * triHeight;

double areaTrapezoid = trapHeight * (trapBase1 + trapBase2) / 2;

double areaCircle = Math.PI * circRadius * circRadius;

System.out.println();

System.out.println("Square Area: " + areaSquare);

System.out.println("Rectangle Area: " + areaRectangle);

System.out.println("Triangle Area: " + areaTriangle);

System.out.println("Trapezoid Area: " + areaTrapezoid);

System.out.println("Circle Area: " + areaCircle);

System.out.println();
}



}




===================================================

I have the following code for it but I can't seem to make the program compile. I was hoping Jingles might help me.
I was able to get everything to work except the reader.


====================================================
// Lab07student

import java.io.*;

public class Lab07student
{

static double sqSide;
static double rectWidth;
static double rectHeight;
static double triBase;
static double triHeight;
static double trapBase1;
static double trapBase2;
static double trapHeight;
static double circRadius;
static double areaSquare;
static double areaRectangle;
static double areaTriangle;
static double areaTrapezoid;
static double areaCircle;
static String strSqSide;
static String strRectWidth;
static String strRectHeight;
static String strTriBase;
static String strTriHeight;
static String strTrapBase1;
static String strTrapBase2;
static String strTrapHeight;
static String strCircRadius;



public static void main (String args[]) throws IOException
{

System.out.println("\nLAB07 90 POINT VERSION\n\n");
enterData();
stringToDouble();
computeAreas();
displayAreas();

}

//THIS IS ALL THAT IS ALLOWED IN THE MAIN METHOD

public static void enterData()
{
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter Square Side ===>> ");

String strSqSide = keyboard.readLine();

System.out.print("Enter Rectangle Width ===>> ");

String strRectWidth = keyboard.readLine();

System.out.print("Enter Rectangle Height ===>> ");

String strRectHeight = keyboard.readLine();

System.out.print("Enter Triangle Base ===>> ");

String strTriBase = keyboard.readLine();

System.out.print("Enter Triangle Height ===>> ");

String strTriHeight = keyboard.readLine();

System.out.print("Enter Trapezoid Base1 ===>> ");

String strTrapBase1 = keyboard.readLine();

System.out.print("Enter Trapezoid Base2 ===>> ");

String strTrapBase2 = keyboard.readLine();

System.out.print("Enter Trapezoid Height ===>> ");

String strTrapHeight = keyboard.readLine();

System.out.print("Enter Circle Radius ===>> ");

String strCircRadius = keyboard.readLine();
}






public static void stringToDouble()
{
double sqSide = Double.parseDouble(strSqSide);

double rectWidth = Double.parseDouble(strRectWidth);

double rectHeight = Double.parseDouble(strRectHeight);

double triBase = Double.parseDouble(strTriBase);

double triHeight = Double.parseDouble(strTriHeight);

double trapBase1 = Double.parseDouble(strTrapBase1);

double trapBase2 = Double.parseDouble(strTrapBase2);

double trapHeight = Double.parseDouble(strTrapHeight);

double circRadius = Double.parseDouble(strCircRadius);

}

public static void computeAreas()
{

double areaSquare = sqSide * sqSide;

double areaRectangle = rectWidth * rectHeight;

double areaTriangle = triBase / 2 * triHeight;

double areaTrapezoid = trapHeight * (trapBase1 + trapBase2) / 2;

double areaCircle = Math.PI * circRadius * circRadius;

}

public static void displayAreas()
{

System.out.println();

System.out.println("Square Area: " + areaSquare);

System.out.println("Rectangle Area: " + areaRectangle);

System.out.println("Triangle Area: " + areaTriangle);

System.out.println("Trapezoid Area: " + areaTrapezoid);

System.out.println("Circle Area: " + areaCircle);

System.out.println();


}
}


=====================================================

I'm not really sure of what to do. Everytime I compile Lab07student it shows the following error:


C:\Lab07student.java:59: unreported exception java.io.IOException; must be caught or declared to be thrown
String strSqSide = keyboard.readLine();


HELP PLEASE!
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
Well, the error basically says that you are calling a function [readline()], that may cause an exception, so you need to enclose it in a try/catch block.

Make it so:
Code:
public static void main (String args[]) throws IOException {
	System.out.println("\nLAB07 90 POINT VERSION\n\n");
	try {
		enterData();
		stringToDouble();
		computeAreas();
		displayAreas();
	} catch(IOException e) {
		System.out.println("Error");
		}
}

//THIS IS ALL THAT IS ALLOWED IN THE MAIN METHOD

public static void enterData() throws IOException {
	BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	System.out.print("Enter Square Side ===>> ");
	strSqSide = keyboard.readLine();
	System.out.print("Enter Rectangle Width ===>> ");
	strRectWidth = keyboard.readLine();
	System.out.print("Enter Rectangle Height ===>> ");
	strRectHeight = keyboard.readLine();
	System.out.print("Enter Triangle Base ===>> ");
	strTriBase = keyboard.readLine();
	System.out.print("Enter Triangle Height ===>> ");
	strTriHeight = keyboard.readLine();
	System.out.print("Enter Trapezoid Base1 ===>> ");
	strTrapBase1 = keyboard.readLine();
	System.out.print("Enter Trapezoid Base2 ===>> ");
	strTrapBase2 = keyboard.readLine();
	System.out.print("Enter Trapezoid Height ===>> ");
	strTrapHeight = keyboard.readLine();
	System.out.print("Enter Circle Radius ===>> ");
	strCircRadius = keyboard.readLine();
}
I've altered part as you were declaring the strings in the enterData() function in a different scope to the globals, you should do the same with all the other variables you need to be global in each function. Also it might be worth making all the static vars at the top private too.
I would also recommend that you convert each string to a double one at a time, and catch a NumberFormatException when the user doesn't type in a valid number.
 

Users who are viewing this thread

Top Bottom