View Single Post
  #1 (permalink)  
Old 07-04-2007, 05:57 AM
Felissa Felissa is offline
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
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
Code:
Items item = (Items) scanner.nextLine();
I tried doing it this way

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?
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
Reply With Quote
Sponsored Links