Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 03-08-07   #1 (permalink)
Overclocker in Training
 
FaLLeNAn9eL's Avatar
 
intel nvidia

Join Date: Oct 2006
Posts: 1,131

Rep: 90 FaLLeNAn9eL is acknowledged by some
Unique Rep: 67
Trader Rating: 2
Default Java String.charAt() Help

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++;
			}
		}

	}
__________________

Overclock.net Headphone Club: Because perfect hair is overrated.
Quote:
Originally Posted by DJ.BigBear (about his ATH-A900) View Post
you betcha
still lovin it.
it makes pr0n more realistic

System: reborn
CPU
Q6600 SLACR
Motherboard
Asus P5N32-E
Memory
2 Gb Crucial Ballistix
Graphics Card
2x eVGA 8800 GTS SLI
Hard Drive
32 Gb Raptor + 74 Gb WD Raptor
Sound Card
X-Fi Elite Pro
Power Supply
Ultra X2 Connect 550W
Case
Thermaltake Armor (Black)
CPU cooling
Zalman CNPS9700
OS
Windows XP SP2
Monitor
ViewSonic 20" LCD

Last edited by FaLLeNAn9eL : 03-08-07 at 01:13 AM.
FaLLeNAn9eL is offline FaLLeNAn9eL's Gallery   Reply With Quote
Old 03-08-07   #2 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

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?

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N

Last edited by C-bro : 03-08-07 at 01:26 AM.
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 03-08-07   #3 (permalink)
Overclocker in Training
 
FaLLeNAn9eL's Avatar
 
intel nvidia

Join Date: Oct 2006
Posts: 1,131

Rep: 90 FaLLeNAn9eL is acknowledged by some
Unique Rep: 67
Trader Rating: 2
Default

OMG thank you so much! One last question , is there a way to compare chars and strings?
__________________

Overclock.net Headphone Club: Because perfect hair is overrated.
Quote:
Originally Posted by DJ.BigBear (about his ATH-A900) View Post
you betcha
still lovin it.
it makes pr0n more realistic

System: reborn
CPU
Q6600 SLACR
Motherboard
Asus P5N32-E
Memory
2 Gb Crucial Ballistix
Graphics Card
2x eVGA 8800 GTS SLI
Hard Drive
32 Gb Raptor + 74 Gb WD Raptor
Sound Card
X-Fi Elite Pro
Power Supply
Ultra X2 Connect 550W
Case
Thermaltake Armor (Black)
CPU cooling
Zalman CNPS9700
OS
Windows XP SP2
Monitor
ViewSonic 20" LCD
FaLLeNAn9eL is offline FaLLeNAn9eL's Gallery   Reply With Quote
Old 03-08-07   #4 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

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);
}
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw

Last edited by rabidgnome229 : 03-08-07 at 01:33 AM.
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 03-08-07   #5 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

Quote:
Originally Posted by FaLLeNAn9eL View Post
OMG thank you so much! One last question , is there a way to compare chars and strings?
The easiest way is the one you did. Unless you're getting an error between mismatched data types. In which case do what I had in my post above with:

hand.charAt(i) == 'D'

This will ensure that both the left and right sides are char data types.

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 03-08-07   #6 (permalink)
Overclocker in Training
 
FaLLeNAn9eL's Avatar
 
intel nvidia

Join Date: Oct 2006
Posts: 1,131

Rep: 90 FaLLeNAn9eL is acknowledged by some
Unique Rep: 67
Trader Rating: 2
Default

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?
__________________

Overclock.net Headphone Club: Because perfect hair is overrated.
Quote:
Originally Posted by DJ.BigBear (about his ATH-A900) View Post
you betcha
still lovin it.
it makes pr0n more realistic

System: reborn
CPU
Q6600 SLACR
Motherboard
Asus P5N32-E
Memory
2 Gb Crucial Ballistix
Graphics Card
2x eVGA 8800 GTS SLI
Hard Drive
32 Gb Raptor + 74 Gb WD Raptor
Sound Card
X-Fi Elite Pro
Power Supply
Ultra X2 Connect 550W
Case
Thermaltake Armor (Black)
CPU cooling
Zalman CNPS9700
OS
Windows XP SP2
Monitor
ViewSonic 20" LCD
FaLLeNAn9eL is offline FaLLeNAn9eL's Gallery   Reply With Quote
Old 03-08-07   #7 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

hearts[heartsIncrement] = (hand.charAt[i-1]).toString();

Try that. It's not the best way to do it, but it may work.

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 03-08-07   #8 (permalink)
Overclocker in Training
 
FaLLeNAn9eL's Avatar
 
intel nvidia

Join Date: Oct 2006
Posts: 1,131

Rep: 90 FaLLeNAn9eL is acknowledged by some
Unique Rep: 67
Trader Rating: 2
Default

It doesn't work
__________________

Overclock.net Headphone Club: Because perfect hair is overrated.
Quote:
Originally Posted by DJ.BigBear (about his ATH-A900) View Post
you betcha
still lovin it.
it makes pr0n more realistic

System: reborn
CPU
Q6600 SLACR
Motherboard
Asus P5N32-E
Memory
2 Gb Crucial Ballistix
Graphics Card
2x eVGA 8800 GTS SLI
Hard Drive
32 Gb Raptor + 74 Gb WD Raptor
Sound Card
X-Fi Elite Pro
Power Supply
Ultra X2 Connect 550W
Case
Thermaltake Armor (Black)
CPU cooling
Zalman CNPS9700
OS
Windows XP SP2
Monitor
ViewSonic 20" LCD
FaLLeNAn9eL is offline FaLLeNAn9eL's Gallery   Reply With Quote
Old 03-08-07   #9 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,975
Blog Entries: 1

Rep: 564 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

hearts[heartsIncrement] = ("" + hand.charAt[i-1]);
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 03-08-07   #10 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,832

Rep: 283 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 215
FAQs Submitted: 6
Folding Team Rank: 237
Hardware Reviews: 9
Trader Rating: 1
Default

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.

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 02:02 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.37232 seconds with 8 queries