|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Java String.charAt() Help
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
Overclocker in Training
|
Hey guys, I really need your help. I'm working on a project which I'm supposed to make a card game. I'm supposed to use the charAt method. I have the code written and the rest of the methods have no problems but I cannot figure out what's going on with the charAt. Thank you very much in advanced.
Here is my code: Code:
public void cardInput()
{
Scanner input = new Scanner (System.in);
diamonds = new String[13];
clubs = new String[13];
hearts = new String[13];
spades = new String[13];
String hand;
System.out.println("Enter hand seperated by spaces:");
hand = input.nextLine();
for(int i = 0; i < 37; i++)
{
if (hand.charAt[i] == "D")
{
diamonds[diamondsIncrement] = hand.charAt[i-1];
diamondsIncrement+= 1;
}
if (hand.charAt[i] == "C")
{
clubs[clubsIncrement] = hand.charAt[i-1];
clubsIncrement++;
}
if (hand.charAt[i] == "H")
{
clubs[clubsIncrement] = hand.charAt[i-1];
clubsIncrement++;
}
if (hand.charAt[i] == "S")
{
hearts[heartsIncrement] = hand.charAt[i-1];
heartsIncrement++;
}
}
}
Last edited by FaLLeNAn9eL : 03-08-07 at 01:13 AM. |
||||||||||||
|
|
|
|
#2 (permalink) | ||||||||||||
|
110100001101001111000
|
Hehe, easy fix. You're using hand.charAt[i] as if you're indexing an array. charAt() is a method in the String class. What you need is:
hand.charAt(i) and you should be just fine! I'm also not sure about java, but in C, you use ' ' for characters and " " for strings. Just letting you know because the conditions may have to be hand.charAt(i) == 'D' Edit: For my own curiosity, what card game are you making?
__________________
Last edited by C-bro : 03-08-07 at 01:26 AM. |
||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||
|
Overclocker in Training
|
OMG thank you so much! One last question
, is there a way to compare chars and strings?
|
||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
Well you can compare a character in a string to a character.
Let's say you want to compare String string = "the" to char letter = 'a' to see if they are equal if(string.charAt(0) - letter == 0) If the character is before the string's char (alphabetically) or the char is capital and the string's char is lowercase the expression will be negative. You could make a compareTo method out of that Code:
int compareTo(String string, char letter, int index)
{
return(string.charAt(index) - letter);
}
Last edited by rabidgnome229 : 03-08-07 at 01:33 AM. |
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||
|
110100001101001111000
|
Quote:
hand.charAt(i) == 'D' This will ensure that both the left and right sides are char data types.
__________________
|
|||||||||||||
|
|
|
|
#6 (permalink) | ||||||||||||
|
Overclocker in Training
|
Zomg you = genius!
The last error im getting is from assigning the array[index] to the char. Is it possible to assign the value the way i have it setup?
|
||||||||||||
|
|
|
|
#7 (permalink) | ||||||||||||
|
110100001101001111000
|
hearts[heartsIncrement] = (hand.charAt[i-1]).toString();
Try that. It's not the best way to do it, but it may work.
__________________
|
||||||||||||
|
|
|
|
#8 (permalink) | ||||||||||||
|
Overclocker in Training
|
It doesn't work
![]()
|
||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
hearts[heartsIncrement] = ("" + hand.charAt[i-1]);
|
|||||||||||||
|
|
|
|
#10 (permalink) | ||||||||||||
|
110100001101001111000
|
Alright, then just use arrays up top where you declare each of the suits. Then if you need to output the suits as strings, just convert them at time of printing.
Edit: Good call rabid. I can't believe I overlooked the concatenation with an empty string.
__________________
|
||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|