Linked List program

  • Thread starter DeAndrea Monroe
  • Start date
D

DeAndrea Monroe

I am constructing a program that stores names and numbers in a
directory. I have constructed a portion of the code, that can be found
here http://code.google.com/p/cschelp/source/checkout, but I keep
getting the same error when I try to run or build the program.
avac *.java
The system cannot find the file specified.
java phonedir
The system cannot find the file specified.

I'm using the application SciTe.
 
E

Eric Sosman

I am constructing a program that stores names and numbers in a
directory. I have constructed a portion of the code, that can be found
here http://code.google.com/p/cschelp/source/checkout, but I keep
getting the same error when I try to run or build the program.
avac *.java

Copy/paste error? The command to run the Java compiler is
usually "javac", not "avac".

I suspect you don't have Java and the Java Development Kit (JDK,
which includes javac and other essentials) installed on your system,
or if they're installed you haven't properly introduced them to your
command-line environment (e.g., by putting them on your "path"). What
happens if you just type

java
or
javac

at the command prompt, with no other parameters? Perhaps the system
is telling you that it can't find those programs, rather than telling
you "You've written bad Java."
I'm using the application SciTe.

Sorry; I have no idea what SciTe might be.

Incidentally, you'd do well to acquaint yourself with the usual
conventions for naming things in Java: Your class should probably be
PhoneDir or Phonedir (in PhoneDir.java or Phonedir.java). You'll
also want to break the bad habit of putting things in the default
package, but perhaps you're just beginning to learn the language and
haven't encountered the "package" statement yet. Also, there's the
wide and wonderful world of "generics" yet to open before you; those
undecorated Lists and LinkedLists are widely frowned upon -- but again,
perhaps that's a topic your class hasn't reached yet.
 
D

DeAndrea Monroe

Thanks for the help. I'm supposed to be using NetBeans but I can't
seem to get it to download correctly. Have you checked out any of the
code I have written?
 
E

Eric Sosman

Thanks for the help. I'm supposed to be using NetBeans but I can't
seem to get it to download correctly. Have you checked out any of the
code I have written?

(When replying to a message, it's considered good form to quote
enough of the original to establish context. Not the whole thing,
mind you, "just enough.")

Since you didn't describe what sort of troubles you're having with
NetBeans, I don't know what advice to give to solve them. All I can
offer is that "It works for me," which isn't much comfort. Perhaps
your classmates or instructor can help you get started.

As for your code, I looked at it briefly (as my earlier remarks
indicate). It's clearly a beginner's effort and looks like a class
assignment, which is why I surmise you're in an instructional setting
of some kind. There is much that could be done to spiff up your code,
but it would probably just increase a starting student's confusion.
I imagine your class will get around to the niftier stuff once the
basics are behind you.
 
A

August Karlstrom

Thanks for the help. I'm supposed to be using NetBeans but I can't
seem to get it to download correctly. Have you checked out any of the
code I have written?

I have. The output below shows what the Java compiler has to say about
it. Remember that Java is an explicitly typed language. You have to
provide a type when you introduce a new variable.

$ javac anagrams.java
anagrams.java:23: cannot find symbol
symbol : variable word
location: class anagrams
word = new String();
^
anagrams.java:24: cannot find symbol
symbol : variable signature
location: class anagrams
signature = new String();
^

[snipped]

45 errors


/August
 
A

Arved Sandstrom

DeAndrea said:
Thanks for the help. I'm supposed to be using NetBeans but I can't
seem to get it to download correctly. Have you checked out any of the
code I have written?

I'm with Eric, I have no idea what the problem is, however I'd have to
assume you at least get to the http://netbeans.org/downloads/index.html
page and can click a button for a download selection. Is the download itself
failing, or is it when you start the installation of the download? Did you
go for Netbeans 6.9; in which case do you have a JDK 1.6 installed (as the
download/install instructions require)?

Might as well get Netbeans up and running first before discussing code.
Although if you do have a JDK installed (don't worry about it being 1.6 if
you're not installing Netbeans, 1.5 is OK too at this stage if you already
have a JDK installed; I haven't used SciTE for a long time but I seem to
recall it's a pure text editor) then

java -version

on the command line will tell you. If you're convinced you have one but the
above doesn't show the version details then check your path.

If you do have a JDK then (this follows on others' suggestions) modify your
classes to have a package statement at the top of each source file. As an
example, if you put in

package org.deandrea.assignments;

then also create a directory structure like

org/
deandrea/
assignments/
PhoneDir.java
Anagrams.java
Count.java

(Note the case changes to the class names; also make them in the source).

Once you have a directory structure like this, and you have a javac and java
in your path, if you have a comamnd line open in the directory that contains
"org",

javac org/deandrea/assignments/*.java
(or one by one)

and

java org.deandrea.assignments.PhoneDir

should all work (in the sense of compiling and running). Once you're at that
stage we can help out with the code itself.

AHS
 
D

DeAndrea Monroe

"I'm with Eric, I have no idea what the problem is, however I'd have to
assume you at least get to thehttp://netbeans.org/downloads/index.html
page and can click a button for a download selection. Is the download itself
failing, or is it when you start the installation of the download? Did you
go for Netbeans 6.9; in which case do you have a JDK 1.6 installed (as the
download/install instructions require)?"

I have downloaded it now and I'm not having problems using it.
However, I keep getting the same error saying cannot find symbol and
it all starts with me declaring the linkedlist name as phone. The
program isn't recognizing the name and anytime it is used throughout
the program it doesn't work. It tells me to creat a class named phone
but that doesn't make sense to me because all of the code is supposed
to be included in a linkedlist.


"If you do have a JDK then (this follows on others' suggestions) modify your
classes to have a package statement at the top of each source file. As an
example, if you put in

package org.deandrea.assignments;

then also create a directory structure like

org/
    deandrea/
        assignments/
            PhoneDir.java
            Anagrams.java
            Count.java

(Note the case changes to the class names; also make them in the source).

Once you have a directory structure like this, and you have a javac and java
in your path, if you have a comamnd line open in the directory that contains
"org",

javac org/deandrea/assignments/*.java
(or one by one)

and

java org.deandrea.assignments.PhoneDir

should all work (in the sense of compiling and running). Once you're at that
stage we can help out with the code itself."


I'm kind of confused as to what's supposed to be done here. I've used
Java in previous classes but never this intense. I'm a lot more
comfortable with Ruby but I don't have an option to use it here.
 
A

Arved Sandstrom

DeAndrea said:
I have downloaded it now and I'm not having problems using it.
However, I keep getting the same error saying cannot find symbol and
it all starts with me declaring the linkedlist name as phone. The
program isn't recognizing the name and anytime it is used throughout
the program it doesn't work. It tells me to creat a class named phone
but that doesn't make sense to me because all of the code is supposed
to be included in a linkedlist.

See comments below.

[ SNIP ]
I'm kind of confused as to what's supposed to be done here. I've used
Java in previous classes but never this intense. I'm a lot more
comfortable with Ruby but I don't have an option to use it here.

Now that you're in NetBeans (abbrev. NB) don't worry about the command-line
stuff I described. However, certain immediate points come to mind:

1) I still strongly recommend putting your classes in a package;

2) method definitions don't have a ; right after the signature. It's

public void foo() {
// stuff
}

not

public void foo();
{
// stuff
}

3) I wouldn't be using static methods here but this may be where you guys
have gotten to in instruction; I'll leave it at that. However, it does mean
if you want to have the "phone" variable, a LinkedList, visible to your
static "operations" methods, that you need to have a class variable instead,
basically a

private static LinkedList phone;

declaration at the top-level of the class (not inside a method, including
main()).

4) There are a number of other syntax issues, like erroneous method
signatures, but once you've cleared up the above you should find those
straightforward to solve.

As to your design question, think of it like this: your "phone directory" is
stored as a LinkedList which is referenced by the "phone" variable in your
PhoneDir class. Down the road, if you move away from statics, you'd most
likely have an *instance* of the PhoneDir class, which internally has a
reference still to a LinkedList.

For what it's worth, given the stage of Java that you are at, I commend you
on the clarity and structure of your code. I have to *work* with people,
intermediate and senior developers some of them, that could take some
pointers from you.

AHS
 
D

DeAndrea Monroe

"1) I still strongly recommend putting your classes in a package;"
Are you referring to all of the classes I'm using? Because now I'm
getting the error for the SimpleIO saying that it can't find the
symbol as well as DirectoryRecord giving the same problem? Should I
make each of those seperate classes in the package as well?


" 3) I wouldn't be using static methods here but this may be where you guys
have gotten to in instruction; I'll leave it at that. However, it does mean
if you want to have the "phone" variable, a LinkedList, visible to your
static "operations" methods, that you need to have a class variable instead,
basically a

private static LinkedList phone;

declaration at the top-level of the class (not inside a method, including
main())."

This really helped get the other problems I was having taken away.
Thanks.
 
M

markspace

Are you referring to all of the classes I'm using? Because now I'm
getting the error for the SimpleIO saying that it can't find the
symbol as well as DirectoryRecord giving the same problem? Should I
make each of those seperate classes in the package as well?


There's no simple, intuitive answer to "I'm getting an error," other
than perhaps "RTFM" or maybe "l34rn 2 c0de". I realize you are new at
this, but understanding how to describe problems to your colleagues is
part of your education too. I didn't notice anyone point you at
SSCCE.org yet so here goes:

<http://sscce.org/>

I'm betting however that you fubar'd the package command somehow. Can
you describe how you added it? There's additional refactoring (read:
sed/grep style search and replace) to be done when you move a class into
a new package.


Incidentally, this sort of stuff that works much better with someone
actually looking at your screen. When you are just learning and just
started with a new IDE, it will be much much faster you to head down to
the school computer lab and sit where a lab tech can answer questions,
and you have a couple of other students there who also can see directly
what is going on and fix errors much much faster. You (and your
parents' tax dollars) are paying for that lab so why not use it?
This really helped get the other problems I was having taken away.
Thanks.


Yeah, all your problems are basic problems with the fundamentals of the
language. This goes much faster if you can get someone to look over
your shoulder and make suggestions.
 
D

DeAndrea Monroe

"There's no simple, intuitive answer to "I'm getting an error," other
than perhaps "RTFM" or maybe "l34rn 2 c0de".  I realize you are new at
this, but understanding how to describe problems to your colleagues is
part of your education too.  I didn't notice anyone point you at
SSCCE.org yet so here goes:

<http://sscce.org/>"

Thanks for the site, I will be sure to use it. The reason I didn't go
into too much detail about my errors was because the person I was
talking to earlier knew what program I was using to compile my
program.

"Yeah, all your problems are basic problems with the fundamentals of the
language.  This goes much faster if you can get someone to look over
your shoulder and make suggestions."

I don't really have an option to have someone looking at my code
directly. We have a lab at the school I attend and when I tried to set
an appointment but I never got a response. This was an option for me
to try to use to get a better understanding at what I'm doing from
people who have done these forms of assignments.
 
M

markspace

The reason I didn't go
into too much detail about my errors was because the person I was
talking to earlier knew what program I was using to compile my
program.


I saw your original post and your link to code.google.com. I didn't see
SimpleIO there. Did I miss it? In other words, your example was not
complete, as SSCCE requests.

I tried to set
an appointment but I never got a response.


This is odd to me. When I attended college, we had several labs, all
with posted hours (granted, few were open all day, esp. on Sunday). All
labs were walk-in: no appointment, you just wandered in the door.

What school are you attending, out of curiosity?
 
A

Arved Sandstrom

DeAndrea said:
Are you referring to all of the classes I'm using? Because now I'm
getting the error for the SimpleIO saying that it can't find the
symbol as well as DirectoryRecord giving the same problem? Should I
make each of those seperate classes in the package as well?
[ SNIP ]

That's right. Java packaging is mostly just that - packaging. Source files
are set up in a directory hierarchy, in some arbitrary root folder, and as
source files are placed in subdirectories their package declaration (at the
top of the source file) must match where they are in that hierarchy. The
packaging is both for organization - my X-type functionality is grouped
*here* kind of thing - but also for applying code access modifiers.

If you are in one class in package A and you want to reference a class in
package B, that's where "import" statements come in. In this particular
case, where you have 3 classes (PhoneDir.java, SimpleIO.java,
DirectoryRecord.java), you may as well place them all in the same package
directory, but then the package declarations must also match.

One reason for not using the default package (no package declaration) is
that it makes *any* packaging difficult. You can't be in one class in a
named package and import another in the default. So for this and other
reasons (as in, out in the real world your code will intermingle with other
peoples' code) you want typically not to use the default package.

AHS
 
D

DeAndrea Monroe

If you are in one class in package A and you want to reference a class in
package B, that's where "import" statements come in. In this particular
case, where you have 3 classes (PhoneDir.java, SimpleIO.java,
DirectoryRecord.java), you may as well place them all in the same package
directory, but then the package declarations must also match.

I put these classes in a package and they all became their own
seperate package. I don't know if that was the right thing to do but
the erros went away. There are now three seperate additional tabs at
the top of the program containing SimpleIO.java, DirectoryRecord.java
but the other is main.java. I don't know how to change that.
 
D

DeAndrea Monroe

I put these classes in a package and they all became their own
seperate package. I don't know if that was the right thing to do but
the erros went away. There are now three seperate additional tabs at
the top of the program containing SimpleIO.java, DirectoryRecord.java
but the other is main.java. I don't know how to change that.

Also, I'm supposed to be storing the names and numbers into a linked
list but I haven't used a node at all in the program. Is this a
problem?
 
A

Arved Sandstrom

DeAndrea said:
I put these classes in a package and they all became their own
seperate package. I don't know if that was the right thing to do but
the erros went away. There are now three seperate additional tabs at
the top of the program containing SimpleIO.java, DirectoryRecord.java
but the other is main.java. I don't know how to change that.

No idea what you mean by tabs - must be a Google Code thing? And no idea why
you'd have a "main.java"; as written, PhoneDir *is* your main class.

All three classes in their own named package, with nothing in the default,
is exactly on the right track.

AHS
 
D

DeAndrea Monroe

No idea what you mean by tabs - must be a Google Code thing? And no idea why
you'd have a "main.java"; as written, PhoneDir *is* your main class.

When I started the program, it asked if I wanted a main class and I
checked yes but when I did that, it made the name of the program
main.java and I don't know how to change that.
 
A

Arved Sandstrom

DeAndrea said:
Also, I'm supposed to be storing the names and numbers into a linked
list but I haven't used a node at all in the program. Is this a
problem?

Well, OK, *this* is a pedagogical question actually. :) You're using a
linked-list implementation made available by the standard Java libraries,
namely the JDK. So *you* don't worry about nodes. Now, the question is, is
this what the assignment asked for or allowed, or does your instructor
actually want you to write a linked list (I certainly hope not). You might
want to clarify this.

AHS
 
D

DeAndrea Monroe

Well, OK, *this* is a pedagogical question actually. :) You're using a
linked-list implementation made available by the standard Java libraries,
namely the JDK. So *you* don't worry about nodes. Now, the question is, is
this what the assignment asked for or allowed, or does your instructor
actually want you to write a linked list (I certainly hope not). You might
want to clarify this.
He told us to use the library so I'm assuming that I don't have to use
nodes. There is an updated version of the program on the same link.
I'm still recieveing a few errors like:
cannot find symbol for equalsIgnoreCase
illegal start of expression
cannot find symbol for method set and curr
 
A

Arved Sandstrom

DeAndrea said:
When I started the program, it asked if I wanted a main class and I
checked yes but when I did that, it made the name of the program
main.java and I don't know how to change that.

Ahh, the tabs of open classes in the NetBeans editor, gotcha.

Operating on the assumption that you created a new Java project, namely a
Java application, and ended up with a Main class in some package that
NetBeans gave you, then added your classes to that package, simply delete
the Main class in the Project view. For running from the IDE you can right
click on your project in Project view, choose Properties > Run, and set
PhoneDir as your main class.

AHS
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top