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

Status
Not open for further replies.

patonb

Golden Master
Messages
9,748
Location
In Gov't Regulated Cubical
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;
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:

// 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.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.name))
theURL = bmlist.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

 
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.
 
Are you compiling with a 1.02 java compiler? If so, it's best to just in general update to the latest 5.0
 
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.
 
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).
 
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.name));

Thanks
 
Sorry for beating this to death.........
Heres my error after seperating And compiling buttonlink:
C:\Prog\Java\Day 14\ButtonLink.java:19: cannot find symbol
symbol : variable i
location: class ButtonLink
add(new Button(bmlist.name));
^
C:\Prog\Java\Day 14\ButtonLink.java:33: cannot find symbol
symbol : variable i
location: class ButtonLink
if(name.equals(bmlist.name))
^
C:\Prog\Java\Day 14\ButtonLink.java:34: cannot find symbol
symbol : variable i
location: class ButtonLink
theURL = bmlist.url;
 
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.
 
patonb said:
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.
 
Status
Not open for further replies.
Back
Top Bottom