Results 1 to 5 of 5
  1. #1
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default Printing my toString method

    Hi. I'm trying to get it so that I can print off my toString method. Or send the variable allStr from the toString method to the main method and print it there. I don't know enough about Java to figure it out myself, and I could really use some help.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Driver
    {
    	String license;
    	String name;
    	String street;
    	String city;
    	String province;
    	
    	public Driver()
    	{
    		license = "N/A";
    		name = "N/A";
    		street = "N/A";
    		city = "N/A";
    		province = "N/A";
    	}
    	
    	public Driver( String lE, String nE, String sT, String cY, String pE )
    	{
    		license = lE;
    		name = nE;
    		street = sT;
    		city = cY;
    		province = pE;
    	}
    	
    	public String toString( Driver drStr)
    	{
    		String licStr = license;
    		String namStr = name;
    		String strStr = street;
    		String citStr = city;
    		String proStr = province;
    		
    		String allStr = licStr + " belongs too " + namStr + " who lives at " + strStr + " in " + citStr + ", " + proStr;
    		System.out.println( allStr );
    		return allStr;
    	}
    
    	public static void main( String[] args )
    	{
    		Driver driver;
    		
    		System.out.println( "This is the main method" );
    		
    		String lE = (JOptionPane.showInputDialog( "License number:" ));
    		String nE = (JOptionPane.showInputDialog( "Name:" ));
    		String sT = (JOptionPane.showInputDialog( "Street:" ));
    		String cY = (JOptionPane.showInputDialog( "City:" ));
    		String pE = (JOptionPane.showInputDialog( "Province:" ));
    		
    		driver = new Driver( lE, nE, sT, cY, pE );
    		
    		System.out.println( toString );
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    The usual set up is for toString() to return a String that is descriptive of the Driver instance. This means (1) that it takes no arguments and (2) that it does not do System.out.println() stuff.

    Java Code:
    public String toString() {
        return license + " belongs to " + name + etc
    }
    Then when you want to use it you call the toString() of a particular instance of Driver.

    Java Code:
    driver = new Driver( lE, nE, sT, cY, pE );
    System.out.println( driver.toString() );
    -----------------

    Your variable names are a bit cyptic. Consider using license for the license string everywhere.

    Java Code:
    public class Driver
    {
        String license;
        // etc
    	
        public Driver()
        {
            license = "N/A";
            // etc
        }
    
        public Driver( String license, etc )
        {
            this.license = license;
            // etc
        }
    
        public static void main( String[] args )
        {
            Driver driver;
    		
            System.out.println( "This is the main method" );
    		
            String license = (JOptionPane.showInputDialog( "License number:" ));
            // etc
    		
            driver = new Driver( license, etc );
    		
            System.out.println( driver.toString() );
        }
    }

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    to call method
    Java Code:
        int aInt = 25;
        String aString = aInt.toString()
    In your case your toString() method has a parameter so you have to pass that object on your method.

    Good Luck.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Quote Originally Posted by mine0926 View Post
    to call method
    Java Code:
        int aInt = 25;
        String aString = aInt.toString()
    In your case your toString() method has a parameter so you have to pass that object on your method.

    Good Luck.
    That code won't compile. aInt is primitive so you can't call methods on it.

    The String argument in the OP's version of toString() is never used. The best thing is not to supply such an argument (what string would you supply?) but, rather, to remove it from the toString() definition.

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    did not test it, my fault. It should be
    Java Code:
    Integer aInt = new Integer(25);

Similar Threads

  1. Why isn't my toString method working
    By arri3oi in forum New To Java
    Replies: 7
    Last Post: 12-13-2010, 06:34 AM
  2. overriding toString method
    By matin1234 in forum New To Java
    Replies: 3
    Last Post: 06-01-2010, 05:35 AM
  3. toString() method error?
    By blueduiker in forum New To Java
    Replies: 6
    Last Post: 02-07-2010, 04:19 AM
  4. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 12:43 AM
  5. toString method
    By apfroggy0408 in forum New To Java
    Replies: 6
    Last Post: 01-31-2008, 05:08 AM

Tags for this Thread

Posting Permissions

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