[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java.lang.Integer cannot be cast to java.lang.String

Posted on 2009-04-26
13
Medium Priority
?
3,501 Views
Last Modified: 2012-05-06
Ive written a simple class to hold a stack of cards, each card is assigned a number, so I can then pull out the value and name. however Im having issues passing an object which is an integer to the method, and pulling back a string.

Any ideas?
Stack cards = new Stack();
 
	public void listCards()
	{
		System.out.println("Currently Holding " + cards.size() + ":-");
		for (int x = 1;x <= cards.size();x++)
		{
			System.out.println("Holding " + cards.elementAt(x-1));
			System.out.println(this.getCardName(cards.elementAt(x-1)));
		}
		
	}
	
	private String getCardName(int x)
	{
		switch(x)
		{	
		case 1: return "Ace of Clubs";
		case 2: return "2 of Clubs";
		case 3: return "3 of Clubs";
		case 4: return "4 of Clubs";

Open in new window

0
Comment
Question by:tonelm54
  • 6
  • 5
  • 2
13 Comments
 
LVL 10

Accepted Solution

by:
oleber earned 2000 total points
ID: 24236398
you are having a Stack, without a type element.

Probably you need

Stack<Integer> cards = new Stack<Integer>();

instead of

Stack cards = new Stack();


Can you be more clear in your problem in case this doesn't work? Guessing it is difficult
0
 
LVL 92

Expert Comment

by:objects
ID: 24237898
>                         System.out.println(this.getCardName(cards.elementAt(x-1)));

you need to cast, looks like that should be:

                        System.out.println(this.getCardName((Integer) (cards.elementAt(x-1))));

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24239729
You need to cast to Card:
Card c = (Card)cards.elementAt(x-1);
System.out.println(c.getCardName(c.getRankOrWhateverMethodReturnsRankAsInt()));

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 24239739
Incidentally, i'm wondering why getCardName is only interested in clubs. It should have a default case too for bad input
0
 
LVL 10

Expert Comment

by:oleber
ID: 24239762
Let me say again:

Can you be more clear in your problem?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24240878
tonelm54, i'm wondering why you accepted that answer - it's not connected to your problem or a solution to it
0
 
LVL 10

Expert Comment

by:oleber
ID: 24240952
Are you joking CEHJ?

This is a autoboxing problem.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24241071
>>Are you joking CEHJ?

No ;-) Which line do you think is causing the problem and why?
0
 
LVL 10

Expert Comment

by:oleber
ID: 24241099
maybe:

System.out.println(this.getCardName(cards.elementAt(x-1)));



and this.getCardName needs a int, not a Object.

Please read the code in the snippet before doing this questions.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24241127
>>and this.getCardName needs a int, not a Object.

So why does  this error message say

>>java.lang.Integer cannot be cast to java.lang.String

instead of mentioning Object as one of the operands?
0
 
LVL 10

Expert Comment

by:oleber
ID: 24241185
I'm in here to help, so I'm going to do like Friar Nuno of St. Mary (canonized yesterday) another Portuguese.

Ask to the administrators to give you the points of this question, I have nothing against that.
0
 
LVL 92

Expert Comment

by:objects
ID: 24246423
> tonelm54, i'm wondering why you accepted that answer - it's not connected to your problem or a solution to it

nor is your comment so not sure what you are complaining about.
oleber's comment very obviously helped the asker so not sure why you have a problem.

> Ask to the administrators to give you the points of this question, I have nothing against that.

oleber,

don't let yourself be bullied, your answer was fine.

0
 
LVL 10

Expert Comment

by:oleber
ID: 24247360
I'm in here for the fun, and to make myself active in other domains then my own. Professionally I have 6 months as Java Programmer in almost 10 years of software development. I never read the rules in here, is there moneys for the experts in here or just a t-shirt? Otherwise I don't understand CEHJ position.

Persons like CEHJ seem to be in here for the points. Shooting one answer without even queering to read the question. If this is case let him be happy.

Just one note CEHJ: When a persons have a low level in some technology, this person may have difficulties to express there questions. try to think in this point.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question