Problem getting charAt to work here

I want to get the last character in any string

Page 1 of 1

9 Replies - 2276 Views - Last Post: 14 February 2010 - 11:14 PM Rate Topic: -----

#1 spark69  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 30-January 10

Problem getting charAt to work here

Posted 14 February 2010 - 01:06 PM

So I am trying to build a Card class, without arrays and such, and I have 1 card that is 3 characters long to input, 10 of whatever suit. At first I tried substring but that won't work due to the 10. How can I get the charAt to work?



public class Card
{
	
	private String rank;
	private String suit;

		public Card(String input)
		{
			rank = input.substring(0, 1); 
			suit = input.charAt(suit.length() - 1); 
			
			
			
		}
//Return the rank regardless of case input, or output "unknown" if other. 
		public String getDescription()
		{
			{
			if (rank.equalsIgnoreCase("A"))
					rank = "Ace of ";
			else if (rank.equalsIgnoreCase("J"))
					rank = "Jack of ";
			else if (rank.equalsIgnoreCase("Q"))
					rank = "Queen of ";
			else if (rank.equalsIgnoreCase("K"))
					rank = "King of ";
			else if (rank.equalsIgnoreCase("2"))
					rank = "Two of ";
			else if (rank.equalsIgnoreCase("3"))
					rank = "Three of ";
			else if (rank.equalsIgnoreCase("4"))
					rank = "Four of ";
			else if (rank.equalsIgnoreCase("5"))
					rank = "Five of ";
			else if (rank.equalsIgnoreCase("6"))
					rank = "Six of ";
			else if (rank.equalsIgnoreCase("7"))
					rank = "Seven of ";
			else if (rank.equalsIgnoreCase("8"))
					rank = "Eight of ";
			else if (rank.equalsIgnoreCase("9"))
					rank = "Nine of ";
			else if (rank.equalsIgnoreCase("10"))
					rank = "Ten of ";
			else
				return "Unknown";
			}
			
			{
			if (rank.equals("Unknown"))
				return "Unknown";
			else if (suit.equalsIgnoreCase("H"))
				rank = rank + "Hearts";
			else if (suit.equalsIgnoreCase("D"))
				rank = rank + "Diamonds";
			else if (suit.equalsIgnoreCase("C"))
				rank = rank + "Clubs";
			else if (suit.equalsIgnoreCase("S"))
				rank = rank + "Spades";
			else
				return rank;
			}
			return rank;
			
			
		}
}



errors
Enter the card notation:
10d
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from char to String

	at Card.<init>(Card.java:13)
	at CardPrinter.main(CardPrinter.java:15)


This post has been edited by spark69: 14 February 2010 - 01:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem getting charAt to work here

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Problem getting charAt to work here

Posted 14 February 2010 - 01:20 PM

Your problem lies at this line because suit is a String and the charAt() method returns a primitive char. In order to make this work, simply use the + "" expression to make your char a String. Like so:
suit = input.charAt(suit.length() - 1) + "";   


Was This Post Helpful? 1
  • +
  • -

#3 spark69  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 30-January 10

Re: Problem getting charAt to work here

Posted 14 February 2010 - 01:33 PM

View Postmacosxnerd101, on 14 February 2010 - 12:20 PM, said:

Your problem lies at this line because suit is a String and the charAt() method returns a primitive char. In order to make this work, simply use the + "" expression to make your char a String. Like so:
suit = input.charAt(suit.length() - 1) + "";   



So that fixed the error of converting character to string, but now I get
Exception in thread "main" java.lang.NullPointerException
	at Card.<init>(Card.java:13)
	at CardPrinter.main(CardPrinter.java:15)


and I was getting correct answers except for 10 of suit, and now I just get the error.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Problem getting charAt to work here

Posted 14 February 2010 - 01:38 PM

The reason you are getting a NullPointerException is b/c you are referencing the suit.length() method before the suit pointer references a String. I think you want to reference input.length() instead.
Was This Post Helpful? 1
  • +
  • -

#5 spark69  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 30-January 10

Re: Problem getting charAt to work here

Posted 14 February 2010 - 02:54 PM

View Postmacosxnerd101, on 14 February 2010 - 12:38 PM, said:

The reason you are getting a NullPointerException is b/c you are referencing the suit.length() method before the suit pointer references a String. I think you want to reference input.length() instead.


You lost me. Do I need to change the constructor again somehow?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Problem getting charAt to work here

Posted 14 February 2010 - 02:59 PM

I'll try to explain again. When you have a class with a reference type instance-variable (and this applies to local and global variables as well, not just instance variables), that variable is a pointer or a reference to an object in memory that can only reference objects of the declared type. So name in the example below can only point to a String object in memory.

When you declare it below without initializing, it doesn't reference any objects yet, so you can't do work with it.
public class SomeClass{
   String name; 
}



At this line, you are trying to do work with suit (by invoking the String length() method) before it points to an object in memory.
suit = input.charAt(suit.length() - 1) + ""; 



What I think you want to do is reference the last character in input, like so:
suit = input.charAt(input.length() - 1) + ""; 


Was This Post Helpful? 1
  • +
  • -

#7 spark69  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 30-January 10

Re: Problem getting charAt to work here

Posted 14 February 2010 - 05:53 PM

So now I see you what you were saying, and with those changes I got the code to make all the right returns, except the 10 of suit. Can I write an if/else in the constructor to make the class look at the input, and if the input is two characters long, use the substring getting only the first character, and if the input is three characters long, use a substring to get the first two? If so how?

Or is there a better way?

public class Card
{
	
	private String rank;
	private String suit;

		public Card(String input)
		{
			{
			if (input == charLength(2))
				rank = input.substring(0, 1);
			else
				rank = input.substring(0, 2);
			}
			rank = input.substring(0, 1); 
			suit = input.charAt(input.length() - 1) + "";	
		}



I tried the above and got the following errors
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method charLength(int) is undefined for the type Card

	at Card.<init>(Card.java:11)
	at CardPrinter.main(CardPrinter.java:15)


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem getting charAt to work here

Posted 14 February 2010 - 06:04 PM

Your initial post was for the last character of a String. That will give it to you

char digit = str.charAt(str.length() - 1);

No need to pursue this topic that goes from one direction to another
Topic closed
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem getting charAt to work here

Posted 14 February 2010 - 11:06 PM

Topic re-open at user request
but wasting our time into it as far as I am concerned... but if you still wanted open: your rights
Sorry
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Problem getting charAt to work here

Posted 14 February 2010 - 11:14 PM

Your error message is telling you that you haven't defined the charLength(int) method in the Card class. Really, I don't understand why you want to use your own method if the String class comes with a length() method.

If you look at the API, you'll see that there are two overloaded substring methods- substring(int) and substring(int, int). The first, substring(int), returns a String from the character at the specified index inclusive to the end of the String. The second, substring(int, int), returns a substring from the specified beginning inclusive to the specified ending index exclusive. You may find this bit helpful when parsing the input String.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1