Results 1 to 3 of 3
  1. #1
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default ClassCastException

    I'm trying to write a small text based game. I decided I would like to add items to the game. When using the Scanner I'm trying to turn a String into an Items.

    since I cannot cast
    Java Code:
    Items item = (Items) scanner.nextLine();
    I tried doing it this way

    Java Code:
    Object itemToBeUsed = (Object) scanner.nextLine();
    Items item = (Items) itemToBeUsed;
    the complier is fine with that, but at runTime it gives me a ClassCastException when I try and use it. How should I go about fixing this?
    Java Code:
    else if(cmd.equalsIgnoreCase("take")){
            	Scanner itemToTake = new Scanner(System.in);
            	Object itemBeingTaken = itemToTake.nextLine();
            	
            	Items item = (Items) itemBeingTaken;
            	return ""
               	+this.location.take(this, item);
            }
            	
            else if(cmd.equalsIgnoreCase("use")){ 
            	Scanner itemToUse = new Scanner(System.in);
            	System.out.println("what item would you like to use?");
            	
            	Object itemString = itemToUse.nextLine();
            	Items item = (Items) itemString;
            	
            	return ""+
            	useItemFromInventory(item);}
    any help? Thanks.

    Felissa:p

  2. #2
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default

    What this means is, at runtime, java cannot convert from class Object to class Items. What I would do, is use the constructor in the Items class to accept your string value and use that to create your Items object.

    Java Code:
    String itemString = itemToUse.nextLine();
    Items item = new Items(itemString);
    This would avoid the nasty type casting and in my opinion provide cleaner code. I don't know how practical this is for you, as I don't know what your Items class looks like.

    Greetings.

    Marcus:cool:

  3. #3
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    here is my Items class code:

    Java Code:
    package finalproject;
    
    public interface Items {
    
    	public void useItem(Game game);
    .	
    .
    .
    }
    it's an interface, but here is a class that implements Items
    Java Code:
    package finalproject;
    public class HealthPotion implements Items{
    	public void useItem(Game game){
    		int health = game.getHealth() + 50;
    		if(health >= 100){
    			game.setHealth(100);
    			System.out.println("your health has increase to 100");
    		}	
    		else{ game.setHealth(health); 
    			System.out.println("your health has increased to "+ health);
    		}
    	}
    }
    Thanks!

    Felissa:p

Similar Threads

  1. ClassCastException in TreeSet
    By pHew in forum New To Java
    Replies: 2
    Last Post: 01-16-2008, 12:20 AM
  2. Replies: 1
    Last Post: 07-16-2007, 04:31 PM
  3. ClassCastException
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:26 AM

Posting Permissions

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