Results 1 to 7 of 7
  1. #1
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Talking toString() method error?

    Eclipse is telling me:
    Multiple markers at this line
    - Method breakpoint:Card [entry] -
    toString()
    - This method must return a result of
    type String
    - overrides java.lang.Object.toString

    I don't understand why this is producing an error because my method returns a String. What is wrong?
    Java Code:
    	//toString method
    		public String toString(){
    			switch (card){
    				case 0:
    					return "2 of Clubs";
    	
    				case 1:
    					return "3 of Clubs";
    	
    				case 2:
    					return "4 of Clubs";
    		
    				case 3:
    					return "5 of Clubs";
    	
    				case 4:
    					return "6 of Clubs";
    			
    				case 5:
    					return "7 of Clubs";
    	
    				case 6:
    					return "8 of Clubs";
    
    				case 7:
    					return "9 of Clubs";
    
    				case 8:
    					return "10 of Clubs";
    
    				case 9:
    					return "Jack of Clubs";
    
    				case 10:
    					return "Queen of Clubs";
    
    				case 11:
    					return "King of Clubs";
    
    				case 12:
    					return "Ace of Clubs";
    
    				case 13:
    					return "2 of Spades";
    
    				case 14:
    					return "3 of Spades";
    
    				case 15:
    					return "4 of Spades";
    
    				case 16:
    					return "5 of Spades";
    
    				case 17:
    					return "6 of Spades";
    
    				case 18:
    					return "7 of Spades";
    
    				case 19:
    					return "8 of Spades";
    
    				case 20:
    					return "9 of Spades";
    
    				case 21:
    					return "10 of Spades";
    
    				case 22:
    					return "Jack of Spades";
    
    				case 23:
    					return "Queen of Spades";
    
    				case 24:
    					return "King of Spades";
    
    				case 25:
    					return "Ace of Spades";
    
    				case 26:
    					return "2 of Hearts";
    
    				case 27:
    					return "3 of Hearts";
    
    				case 28:
    					return "4 of Hearts";
    
    				case 29:
    					return "5 of Hearts";
    
    				case 30:
    					return "6 of Hearts";
    
    				case 31:
    					return "7 of Hearts";
    
    				case 32:
    					return "8 of Hearts";
    
    				case 33:
    					return "9 of Hearts";
    
    				case 34:
    					return "10 of Hearts";
    
    				case 35:
    					return "Jack of Hearts";
    
    				case 36:
    					return "Queen of Hearts";
    
    				case 37:
    					return "King of Hearts";
    
    				case 38:
    					return "Ace of Hearts";
    
    				case 39:
    					return "2 of Diamonds";
    
    				case 40:
    					return "3 of Diamonds";
    
    				case 41:
    					return "4 of Diamonds";
    
    				case 42:
    					return "5 of Diamonds";
    
    				case 43:
    					return "6 of Diamonds";
    
    				case 44:
    					return "7 of Diamonds";
    
    				case 45:
    					return "8 of Diamonds";
    					
    				case 46:
    					return "9 of Diamonds";
    					
    				case 47:
    					return "10 of Diamonds";
    					
    				case 48:
    					return "Jack  of Diamonds";
    					
    				case 49:
    					return "Queen of Diamonds";
    					
    				case 50:
    					return "King of Diamonds";
    					
    				case 51:
    					return "Ace of Diamonds";
    			}
    		}

  2. #2
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    nevermind, I'm assuming the error is due to the fact that if it isn't within the range of 0-51 it doesn't return anything

  3. #3
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by blueduiker View Post
    nevermind, I'm assuming the error is due to the fact that if it isn't within the range of 0-51 it doesn't return anything
    Yes thats the error. Just type retun null for example after the end brace of your switch statement. It shldnt complain after that.

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Just type retun null for example after the end brace of your switch statement.
    I knew it!!! lol
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

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

    Default

    Quote Originally Posted by [RaIdEn] View Post
    Yes thats the error. Just type retun null for example after the end brace of your switch statement. It shldnt complain after that.

    The compiler won't complain, but perhaps the players might! I mean if you are playing Bridge and someone lays down the Joker (card==-1) that would generally be taken as something of a faux pas.

    If the proper operation of the program depends on Card having a card value in a particular range, then throw an exception.

  6. #6
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by pbrockway2 View Post
    The compiler won't complain, but perhaps the players might! I mean if you are playing Bridge and someone lays down the Joker (card==-1) that would generally be taken as something of a faux pas.

    If the proper operation of the program depends on Card having a card value in a particular range, then throw an exception.
    i agree but it was just an instantial solution but i guess your right throwing the exception would work.

  7. #7
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by pbrockway2 View Post
    The compiler won't complain, but perhaps the players might! I mean if you are playing Bridge and someone lays down the Joker (card==-1) that would generally be taken as something of a faux pas.

    If the proper operation of the program depends on Card having a card value in a particular range, then throw an exception.
    i agree but it was just an instantial solution but i guess your right throwing the exception would work.

    Here, you can return a string that indicates the failure of the correct such as
    return "Card not present"; would be more accurate than returning null.

Similar Threads

  1. Trying to write toString method
    By curious in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 03:43 AM
  2. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 12:43 AM
  3. question about writing a toString method
    By heather.diggs in forum New To Java
    Replies: 2
    Last Post: 11-13-2008, 12:13 AM
  4. toString() method of java.security.MessageDigest
    By Nicholas Jordan in forum Advanced Java
    Replies: 4
    Last Post: 09-18-2008, 03:32 PM
  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
  •