error at readLine
Author |
Message |
agnivohneb
|
Posted: Wed Oct 21, 2009 12:30 pm Post subject: error at readLine |
|
|
I have been taking some classes for java and I have a problem that i cant seem to solve
the error is
The method "java.lang.String readLine() throws java.io.IOException;" can throw the checked exception "java.io.IOException", so its invocation must be enclosed in a try statement that catches the exception, or else this method must be declared to throw the exception.
i get this error at every instance of in.readLine()
here is the code
Java: | import java.util.*;
import java.io.*;
public class CalcuGator
{
public static void main (String[] args )
{
Calc newCalc = new Calc ();
BufferedReader in = new BufferedReader (new InputStreamReader (System. in));
boolean exit = false;
String exitString = "";
while (!exit )
{
System. out. print ("Please enter your first number: ");
newCalc. num1 = Double. valueOf (in. readLine ()). doubleValue ();
System. out. print ("Please enter your second number: ");
newCalc. num2 = Double. valueOf (in. readLine ()). doubleValue ();
System. out. println (newCalc. divide ());
System. out. println (newCalc. multiply ());
System. out. println (newCalc. add ());
System. out. println (newCalc. subtract ());
while (exitString == "")
{
System. out. print ("Would you like to exit? (y or n): ");
exitString = in. readLine ();
if (exitString == "y")
{
exit = true;
}
else if (exitString == "n")
{
exit = false;
}
else
{
System. out. println ("Invalid Input!");
exitString = "";
}
}
}
}
} |
Java: | public class Calc
{
public static double num1;
public static double num2;
public static String divide ()
{
if (num2 == 0)
{
return "Cannot divide by 0";
}
else
{
return Double. toString(divideNoErr ());
}
}
public static double divideNoErr ()
{
return num1 / num2;
}
public static double multiply ()
{
return num1 * num2;
}
public static double add ()
{
return num1 + num2;
}
public static double subtract ()
{
return num1 - num2;
}
}
|
Could anyone tell me what that error means and how i can solve this problem? thanks |
|
|
|
|
|
Sponsor Sponsor
|
|
|
agnivohneb
|
Posted: Wed Oct 21, 2009 12:34 pm Post subject: RE:error at readLine |
|
|
never mind i figured it out... i forgot to add throws IOException to main |
|
|
|
|
|
|
|