Results 1 to 13 of 13
  1. #1
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default 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:

    FT@19821f
    FT@addbf1
    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 02:45 AM.

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    System.out.println( "test: " + e1 ) ;
    You are printing out the class, not any of the data in it.
    Java Code:
    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).

  3. #3
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    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 09:21 PM.

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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.

  5. #5
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Can you give me an example as this is all new to me. Thanks

  6. #6
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    I tried the following:
    Java Code:
       	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 09:22 PM.

  7. #7
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Well all these variables are different types at the moment:
    Java Code:
    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:
    Java Code:
    		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());

  8. #8
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    I tried the following
    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:

    Java Code:
    System.out.println(vec.next());

  9. #9
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    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());

  10. #10
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    The next() method is not part of Vector, its part of Iterator:
    Java Code:
    		Vector<Employee> data = new Vector<Employee>();
    		Iterator<String> ir = data.iterator();
    		while(ir.hasNext())
    			System.out.println(ir.next());

  11. #11
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    That didn't work. Here are the error messages I get:

    Java Code:
    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 09:22 PM.

  12. #12
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    OK so I've been messing around with the code and I this is what I have at the moment:

    Java Code:
          ListIterator vIter = vec.listIterator() ;
    
          while ( vIter.hasNext() )
          {
             System.out.println( vIter.next() ) ;
          }
    It works, but the output is as follows:

    Java Code:
    FT@19821f
    FT@addbf1
    I know that Im printing the vector, not the actual content. How do I print the content ?

  13. #13
    Shaolin is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    Fixed problem.
    Last edited by Shaolin; 12-11-2007 at 09:21 PM.

Similar Threads

  1. need help, weird question kinda.
    By carlos123 in forum New To Java
    Replies: 6
    Last Post: 01-22-2008, 03:19 AM
  2. output
    By Camden in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 10:34 PM
  3. weird looking shape JButton is it possible?
    By unhurt in forum AWT / Swing
    Replies: 8
    Last Post: 11-03-2007, 09:10 AM
  4. How to redirect the output
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:30 PM
  5. get the output from whoami
    By gary in forum Advanced Java
    Replies: 2
    Last Post: 06-12-2007, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •