-
How to use Linked lists and classes......
I was hoping someone could help me with this...I am trying to access methods
of a instance of a class that is stored in the LinkedList object. I can
print out the reference to the object stored in the linked list, but I can't
get to the methods. I have looked at List Iterator in the instance of my
linked list, but am not real sure on how to use it, nor can I find any real
good documentation on it. It also won't let me directly access the element
index using list.size or any of its methods. The code follows...Someone please
help and thanks for any help.
import java.io.*;
import java.util.*;
public class infile
{
public static void main(String [] argv) throws FileNotFoundException
{
String line;
LinkedList list = new LinkedList();
FileReader fr = null;
BufferedReader infile = null;
StringTokenizer t;
fr = new FileReader("fds.txt");
if(fr != null)
{
infile = new BufferedReader(fr);
if(infile == null)
{
System.out.println("Changing to BufferedReader failed");
System.exit(0);
}
}
else
{
System.out.println("File not found");
System.exit(0);
}
try
{
while((line = infile.readLine()) != null)
{
t = new StringTokenizer(line,">");
DbTemplate myFD = new DbTemplate();
String str = t.nextToken();
myFD.setLeftSide(str);
System.out.println(myFD.getLeftSide() );
str = t.nextToken();
myFD.setRightSide(str);
System.out.println(myFD.getRightSide() );
myFD.setfD();
System.out.println(myFD.getFD() );
list.add(myFD);
}//while end
}
catch(IOException e)
{
System.err.println("End of File");
}
System.out.println( "\nlist: " );
for ( int k = 0; k < list.size(); k++ )
{
System.out.print( list.get( k ) + " " );
k.getFD(); // ERROR about int can't be dereferenced
*******I want to print out the leftSide, rightSide, and myFD of class DbTemplate
right here. Thats all.*********
}
}//main end
}//Class end
class DbTemplate
{
private String fD, leftSide, rightSide, oldFD;
public void setfD()
{
fD = getLeftSide() + ">" + getRightSide();
}
public String getFD()
{
return fD;
}
public void setLeftSide(String x)
{
leftSide = x;
}
public String getLeftSide()
{
return leftSide;
}
public void setRightSide(String x)
{
rightSide = x;
}
public String getRightSide()
{
return rightSide;
}
}
-
Re: How to use Linked lists and classes......
> System.out.print( list.get( k ) + " " );
> k.getFD(); // ERROR about int can't be dereferenced
> *******I want to print out the leftSide, rightSide, and myFD of class
DbTemplate
> right here. Thats all.*********
list.get(k) returns an Object. You have to cast its result to DBTemplate if
you want to use it as such, e.g. "DBTemplate temp =
(DBTemplate)list.get(k);".
"k.getFD()" is in error because you declared "k" to be an int, which is a
primitive value and doesn't have any methods, let alone a getFD() method.
PC2
Brent <bqlauer@swbell.net> wrote in message news:39f339ed$1@news.devx.com...
>
> I was hoping someone could help me with this...I am trying to access
methods
> of a instance of a class that is stored in the LinkedList object. I can
> print out the reference to the object stored in the linked list, but I
can't
> get to the methods. I have looked at List Iterator in the instance of my
> linked list, but am not real sure on how to use it, nor can I find any
real
> good documentation on it. It also won't let me directly access the
element
> index using list.size or any of its methods. The code follows...Someone
please
> help and thanks for any help.
>
> import java.io.*;
> import java.util.*;
>
> public class infile
> {
> public static void main(String [] argv) throws FileNotFoundException
> {
> String line;
>
> LinkedList list = new LinkedList();
> FileReader fr = null;
> BufferedReader infile = null;
> StringTokenizer t;
>
> fr = new FileReader("fds.txt");
>
> if(fr != null)
> {
> infile = new BufferedReader(fr);
>
> if(infile == null)
> {
> System.out.println("Changing to BufferedReader failed");
> System.exit(0);
> }
> }
> else
> {
> System.out.println("File not found");
> System.exit(0);
> }
>
> try
> {
> while((line = infile.readLine()) != null)
> {
> t = new StringTokenizer(line,">");
>
> DbTemplate myFD = new DbTemplate();
>
> String str = t.nextToken();
> myFD.setLeftSide(str);
> System.out.println(myFD.getLeftSide() );
>
> str = t.nextToken();
> myFD.setRightSide(str);
> System.out.println(myFD.getRightSide() );
>
> myFD.setfD();
> System.out.println(myFD.getFD() );
>
> list.add(myFD);
>
>
> }//while end
>
> }
>
> catch(IOException e)
> {
> System.err.println("End of File");
> }
>
>
>
> System.out.println( "\nlist: " );
> for ( int k = 0; k < list.size(); k++ )
> {
> System.out.print( list.get( k ) + " " );
> k.getFD(); // ERROR about int can't be dereferenced
> *******I want to print out the leftSide, rightSide, and myFD of class
DbTemplate
> right here. Thats all.*********
>
> }
>
>
>
>
> }//main end
> }//Class end
>
> class DbTemplate
> {
> private String fD, leftSide, rightSide, oldFD;
>
> public void setfD()
> {
> fD = getLeftSide() + ">" + getRightSide();
> }
>
> public String getFD()
> {
> return fD;
> }
>
> public void setLeftSide(String x)
> {
> leftSide = x;
> }
>
> public String getLeftSide()
> {
> return leftSide;
> }
>
> public void setRightSide(String x)
> {
> rightSide = x;
> }
>
> public String getRightSide()
> {
> return rightSide;
> }
> }
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|