|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

12-11-2007, 04:42 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
|
Weird data output
Hi Guys,
I have a main class, an employee class and a fullTime class which is a subclass of employee. I am trying to output some hard written data in the main class but I get the following characters when I run it:
Here is my code:
Employee Class:
public abstract class Employee {
protected int workid ;
protected String name ;
protected int tel ;
protected static int lastid = 100000 ;
protected double pay ;
public Employee() {
name = "" ;
tel = 0;
workid = lastid++ ;
pay = 0.0 ;
}
public Employee (String nm, int ext) {
name = nm ;
tel = ext ;
workid = lastid++ ;
pay = 0.0 ;
}
public abstract void calculatePayment(); // abstract method
}
FullTime Class:
public class FT extends Employee {
protected static double salary = 1600.0;
public FT () {
super();
}
public FT (String nm, int ext){
super(nm, ext);
}
}
Main Class:
public class main {
public static void main( String[] args )
{
FT e1 = new FT ( "John", 1237 ) ;
FT e2 = new FT ( "Michelle", 1291 ) ;
System.out.println( "test: " + e1 ) ; // Output data
}
}
Last edited by Shaolin : 12-11-2007 at 04:45 AM.
|
|

12-11-2007, 04:46 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
System.out.println( "test: " + e1 ) ;
You are printing out the class, not any of the data in it.
System.out.println( "test: " + e1.name)
That will only work if the main class is in the same package as everything else (since name is protected).
|
|

12-11-2007, 05:27 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
|
Thanks. its not in the same package.
What if I want to use a vector/iterator to output the data? I tried it and it gave me the same output outlined in the first post.
What adjustments will I need to make in order to get it to work ?
Last edited by Shaolin : 12-11-2007 at 11:21 PM.
|
|

12-11-2007, 06:50 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
|
A solution would be to store all of the data in the Employee class as a single Vector: the name work id, pay, etc.
Then return that data and iterate it.
|
|

12-11-2007, 06:56 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
|
Can you give me an example as this is all new to me. Thanks
|
|

12-11-2007, 07:13 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
I tried the following:
ListIterator vIter = vec.listIterator() ;
while ( vIter.hasNext() )
{
System.out.println( vec ) ;
vIter.next() ;
}
And I got this output:
[FT@19821f, FT@addbf1]
[FT@19821f, FT@addbf1]
Here is the whole code for Directory(storage):
Last edited by Shaolin : 12-11-2007 at 11:22 PM.
|
|

12-11-2007, 07:24 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
Well all these variables are different types at the moment:
protected int workid ;
protected String name ;
protected int tel ;
protected static int lastid = 100000 ;
protected double pay ;
You those all have to become Strings and be placed inside a vector. I dont know if you want that but here is an example:
String s = "s";
int i = 5;
Vector<String> data = new Vector<String>();
data.add(s);
data.add(String.valueOf(i));
Iterator<String> ir = data.iterator();
while(ir.hasNext())
System.out.println(ir.next());
|
|

12-11-2007, 07:27 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
Good try, but you are printing out the vector, not the item inside it. You need to print out the content of the vector if you want to see any meaningful data. So you need:
System.out.println(vec.next());
|
|

12-11-2007, 07:43 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
|
I tried that and got the following error:
Directory.java:56: cannot find symbol
symbol : method next()
location: class java.util.Vector<Employee>
System.out.println(vec.next());
|
|

12-11-2007, 07:57 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
The next() method is not part of Vector, its part of Iterator:
Vector<Employee> data = new Vector<Employee>();
Iterator<String> ir = data.iterator();
while(ir.hasNext())
System.out.println(ir.next());
|
|

12-11-2007, 08:13 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
That didn't work. Here are the error messages I get:
Directory.java:59: incompatible types
found : java.util.Iterator<Employee>
required: java.util.Iterator<java.lang.String>
Iterator<String> vIter = vec.iterator() ;
^
Last edited by Shaolin : 12-11-2007 at 11:22 PM.
|
|

12-11-2007, 08:43 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
OK so I've been messing around with the code and I this is what I have at the moment:
ListIterator vIter = vec.listIterator() ;
while ( vIter.hasNext() )
{
System.out.println( vIter.next() ) ;
}
It works, but the output is as follows:
I know that Im printing the vector, not the actual content. How do I print the content ?
|
|

12-11-2007, 06:27 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
|
Fixed problem.
Last edited by Shaolin : 12-11-2007 at 11:21 PM.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|