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
Items item = (Items) scanner.nextLine();
I tried doing it this way
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?
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