Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Ahhhhhhhhh... I'ma hate'n da Java Maan
Closed Thread
Old 08-22-2006, 04:36 PM   #1 (permalink)
patonb's Avatar
 
Master Techie

Join Date: Jan 2005

Location: In Gov't Regulated Cubical

Posts: 2,710

patonb has a spectacular aura aboutpatonb has a spectacular aura about

Default Ahhhhhhhhh... I'ma hate'n da Java Maan

Okay, anyone know if there is a difference in Syntax between 1.02 and 1.5.0_7??

Reason, I've transcride verbatem code from a book and am getting odd errors of;
Quote:
C:\Prog\Java\Day 14\ButtonLink.java:43: 'class' or 'interface' expected
import java.net.URL;
^
C:\Prog\Java\Day 14\ButtonLink.java:44: 'class' or 'interface' expected
import java.net.MalformedURLException;
^
2 errors

Tool completed with exit code 1
Code:

Quote:
// ButtonLink.java starts here
import java.awt.*;
import java.net.*;

public class ButtonLink extends java.applet.Applet {

Bookmark bmlist[] =new Bookmark[3];

public void init(){
bmlist[0] = new Bookmark("Laura's",
"http://www.lne.com/lemay/");
bmlist[1]= new Bookmark("Gamelan",
"http://www.gamelan.com");
bmlist[2]=new Bookmark("Java Home Page",
"http://javasun.com");

setLayout(new GridLayout(bmlist.length, 1,10,10));
for(int i=0;i<bmlist.length; i++);{
add(new Button(bmlist[i].name));
}
}

public boolean action(Event evt, Object arg){
if (evt.target instanceof Button){
Linkto((String)arg);
return true;
}else return false;
}

void Linkto(String name){
URL theURL = null;
for(int i=0;i<bmlist.length;i++);{
if(name.equals(bmlist[i].name))
theURL = bmlist[i].url;
}
if(theURL !=null)
getAppletContext().showDocument(theURL);
}
}//ends Buttonlink.java

// starts Bookmark.java

import java.net.URL;
import java.net.MalformedURLException;

class Bookmark {

String name;
URL url;

Bookmark(String name, String theURL){

this.name = name;
try{this.url = new URL(theURL);}
catch(MalformedURLException e) {
System.out.println("Bad URL: " + theURL);
}
}
}// bookmark ends


patonb is offline  
Old 08-22-2006, 11:21 PM   #2 (permalink)
 
True Techie

Join Date: May 2006

Posts: 150

fjf314 is on a distinguished road

Send a message via AIM to fjf314
Default

Hmm... I'm running 1.5.0_6 right now. I just copied the section of your code that made up Bookmark.java and compiled it. I got no errors.
__________________
There are 10 kinds of people in the world, those who understand binary and those who don\'t.
fjf314 is offline  
Old 08-23-2006, 04:01 AM   #3 (permalink)
 
Newb Techie

Join Date: Jul 2006

Posts: 34

The Future

Send a message via AIM to The Future
Default

Are you compiling with a 1.02 java compiler? If so, it's best to just in general update to the latest 5.0
The Future is offline  
Old 08-23-2006, 11:52 AM   #4 (permalink)
patonb's Avatar
 
Master Techie

Join Date: Jan 2005

Location: In Gov't Regulated Cubical

Posts: 2,710

patonb has a spectacular aura aboutpatonb has a spectacular aura about

Default

FjF...... my issue is that it's all 1 java file, called ButtonLink, with Bookmark, being another class in it. I.e., Bookmark is incorperated in ButtonLink. (books showing how to write 1 file instead of 2 small ones).

Oddly enough, when I remove the Bookmark code, I loose my "i" variable, for assigning my array.

Future...... No, I running 1.5.0_7, my book is 1.0.2.

I've asked at the sun page, but tey've been a bunch of C#@ts.

Thanks guys/gals.......... But I'm still stuck.
__________________
Intel Q6600 g0 @ 3.2Ghz Turniq 120 Heatsink BFG 260 OC MaxCore (core 216) + xfx GTX 260 (core 216)
2x2gb OCZ Platinums XFX 680i motherboard Silverstone DA700 Antec 900
16,412 3dmark06 score


Foldie = e2180 Asus pq5-n SLI 8800gt T-rad cooler (710/1836/1010) 1Gig RAM
TOTAL
patonb is offline  
Old 08-23-2006, 12:40 PM   #5 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

First, regardless of what your book says, it is better Java programming style to define each class in its own file -- especially when first learning.

After you move each class to its own file, make sure that you've defined all the variables you'll need to use within that file. If you still have problems, post the code from both files and the error message(s).
jaeusm is offline  
Old 08-23-2006, 12:46 PM   #6 (permalink)
patonb's Avatar
 
Master Techie

Join Date: Jan 2005

Location: In Gov't Regulated Cubical

Posts: 2,710

patonb has a spectacular aura aboutpatonb has a spectacular aura about

Default

Yha, I've been screwing around withit, and I'm starting to think it should be 2 files, and i just thought it was 1.

The I variable is declared in all my "for" loops:

for(int i=0;i<bmlist.length; i++);{
add(new Button(bmlist[i].name));

Thanks
__________________
Intel Q6600 g0 @ 3.2Ghz Turniq 120 Heatsink BFG 260 OC MaxCore (core 216) + xfx GTX 260 (core 216)
2x2gb OCZ Platinums XFX 680i motherboard Silverstone DA700 Antec 900
16,412 3dmark06 score


Foldie = e2180 Asus pq5-n SLI 8800gt T-rad cooler (710/1836/1010) 1Gig RAM
TOTAL
patonb is offline  
Old 08-23-2006, 12:49 PM   #7 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

The 'i' variable should be fine, then.
jaeusm is offline  
Old 08-23-2006, 01:12 PM   #8 (permalink)
patonb's Avatar
 
Master Techie

Join Date: Jan 2005

Location: In Gov't Regulated Cubical

Posts: 2,710

patonb has a spectacular aura aboutpatonb has a spectacular aura about

Default

Sorry for beating this to death.........
Heres my error after seperating And compiling buttonlink:
Quote:
C:\Prog\Java\Day 14\ButtonLink.java:19: cannot find symbol
symbol : variable i
location: class ButtonLink
add(new Button(bmlist[i].name));
^
C:\Prog\Java\Day 14\ButtonLink.java:33: cannot find symbol
symbol : variable i
location: class ButtonLink
if(name.equals(bmlist[i].name))
^
C:\Prog\Java\Day 14\ButtonLink.java:34: cannot find symbol
symbol : variable i
location: class ButtonLink
theURL = bmlist[i].url;

__________________
Intel Q6600 g0 @ 3.2Ghz Turniq 120 Heatsink BFG 260 OC MaxCore (core 216) + xfx GTX 260 (core 216)
2x2gb OCZ Platinums XFX 680i motherboard Silverstone DA700 Antec 900
16,412 3dmark06 score


Foldie = e2180 Asus pq5-n SLI 8800gt T-rad cooler (710/1836/1010) 1Gig RAM
TOTAL
patonb is offline  
Old 08-23-2006, 01:42 PM   #9 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

You have a subtle syntax error -- a semicolon after your 'for' loops. Your 'i' variable is out of scope. You defined 'i' within the scope of the 'for' loop, meaning that it only exists for the duration of the 'for' loop. The semicolon immediately following the 'for' declaration essentially means that you're looping around an empty statement. So, for each iteration, you will execute the following statement:
Code:
;
After the for loop has exited, the variables defined in the 'for' declaration -- your 'i' variable, in this case -- are now out of scope.
Thus, when the compiler sees 'i' being used to index an array, it no longer exists.

The moral: remove the semicolons after your 'for' loop declarations.
jaeusm is offline  
Old 08-23-2006, 09:53 PM   #10 (permalink)
 
True Techie

Join Date: May 2006

Posts: 150

fjf314 is on a distinguished road

Send a message via AIM to fjf314
Default

Quote:
Originally posted by patonb
FjF...... my issue is that it's all 1 java file, called ButtonLink, with Bookmark, being another class in it. I.e., Bookmark is incorperated in ButtonLink. (books showing how to write 1 file instead of 2 small ones).
Oops, I just figured you put them together for the sake of keeping your code in one piece when posting it on the forum, and that each class was actually it's own file. I've definitely got to agree with jaeusm, though, it's a lot better to put each class in it's own file.
__________________
There are 10 kinds of people in the world, those who understand binary and those who don\'t.
fjf314 is offline  
 
Closed Thread

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
Trackbacks are On
Pingbacks are On
Refbacks are On