TRY/CATCH ODDNESS

O

Obi Wan Shinobi

Hello people,

Would someone please tell me why the following code (which I pretty
much copied out word for word from Wrox' Beginning Java 1.4 book) is
giving me a 'cannot resolve sybol error' referring to the first
character inside the catch condition: (IOException e):

public class AverageFruit {
public static void main(String[] args) {
double numOranges = 50.0E-1;
double numApples = 1.0E1;
double averageFruit = 0;

averageFruit = (numOranges + numApples) / 2.0;
System.out.println("A totlly fruity program");
System.out.println("Average fruit is " + averageFruit);
System.out.println("(Press Enter to exit)");

try {
System.in.read();

} catch (IOException e) {
return;
}
}
}


As you might have guessed i'm just starting out Java, so any help
would be greatly appreciated.

- Obi Wan
 
L

Lee Weiner

Hello people,

Would someone please tell me why the following code (which I pretty
much copied out word for word from Wrox' Beginning Java 1.4 book) is
giving me a 'cannot resolve sybol error' referring to the first
character inside the catch condition: (IOException e):

public class AverageFruit {
public static void main(String[] args) {
double numOranges = 50.0E-1;
double numApples = 1.0E1;
double averageFruit = 0;

averageFruit = (numOranges + numApples) / 2.0;
System.out.println("A totlly fruity program");
System.out.println("Average fruit is " + averageFruit);
System.out.println("(Press Enter to exit)");

try {
System.in.read();

} catch (IOException e) {
return;
}
}
}
Did you import the IOException class into your program?

Lee Weiner
lee AT leeweiner DOT org
 
S

Sudsy

Obi Wan Shinobi wrote:
As you might have guessed i'm just starting out Java, so any help
would be greatly appreciated.

- Obi Wan

Use the force!
Seriously, Lee gave you the answer. You must have this line at the
top of the source file (after any package declaration):

import java.io.IOException;

Only java.lang is available by default.
 
D

Dave

Sudsy said:
Only java.lang is available by default.

Both java.lang and that crazy 'un-named' package (the package with no
name) are available when you do not include a Package statement in your
source code file...

--Dave
 
R

Roedy Green

Both java.lang and that crazy 'un-named' package (the package with no
name) are available when you do not include a Package statement in your
source code file...

avoid packageless classes except for toy experiments.

They get into trouble with import.
 
D

Dale King

Hello, Dave !
You said:
Both java.lang and that crazy 'un-named' package (the package with no
name) are available when you do not include a Package statement in your
source code file...

That's a rather imprecise way of saying it. In reality it is that
java.lang and the package that the source file is in are visible
without qualifying the name.

Leaving off the package statement means that the source file is
in the unnamed packageso the unnamed package wouldbe accessible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top