outputing suits like hearts, diamonds, spades

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 2843 Views - Last Post: 20 July 2009 - 05:12 PM Rate Topic: -----

#1 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

outputing suits like hearts, diamonds, spades

Post icon  Posted 18 July 2009 - 01:14 PM

right now there is a string that has 53 characters in it its all numbers the 1st number 0 and the rest are ones
a random number is picked from the String and is stored in the variable named card
i am trying to take the string and replaces the 1 in position card with a 0;
if u need classrification post right away i am online NOW
Is This A Good Question/Topic? 0
  • +

Replies To: outputing suits like hearts, diamonds, spades

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 01:22 PM

you can break the string into an array of characters using the toCharArray() method..
char [] characters = "Hello".toCharArray();



this will split the string in to an array of {'H', 'e', 'l', 'l', 'o' }.. so now you can choose a random character and then set it to whatever value u want by simply using its index to access it in the array

characters[index] = '0';



hope this helps
Was This Post Helpful? 1
  • +
  • -

#3 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Re: outputing suits like hearts, diamonds, spades

Post icon  Posted 18 July 2009 - 01:25 PM

A string has 53 numbers and a random number is drawn out. put in the variable named card
I store the integer value of card divided by 13 in an int named suit
so it will be like this 4/13 = 0 14/13 = 1 34/13 =2
then i write a statement that increases the value stored in suit by 3 and diplays the character value of this int
out.println((char)(suit+3));
this is SUPPOSED by the instructer to output the suit of cards
ty if u can help
ONLINE NOW
Was This Post Helpful? 0
  • +
  • -

#4 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 01:29 PM

View Postmostyfriedman, on 18 Jul, 2009 - 12:22 PM, said:

you can break the string into an array of characters using the toCharArray() method..
char [] characters = "Hello".toCharArray();



this will split the string in to an array of {'H', 'e', 'l', 'l', 'o' }.. so now you can choose a random character and then set it to whatever value u want by simply using its index to access it in the array

characters[index] = '0';



hope this helps

i cant use arrays
Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 01:34 PM

then the other way i could think of is the more complicated way, which is save the index with character you chose, have an empty string to accumulate your new string,and then loop through the string..on each iteration check if the current index is the one that you chose, if it is then you will concatenate a 0 else you will concatenate the current character..hope you understood this
Was This Post Helpful? 1
  • +
  • -

#6 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 01:40 PM

View Postmostyfriedman, on 18 Jul, 2009 - 12:34 PM, said:

then the other way i could think of is the more complicated way, which is save the index with character you chose, have an empty string to accumulate your new string,and then loop through the string..on each iteration check if the current index is the one that you chose, if it is then you will concatenate a 0 else you will concatenate the current character..hope you understood this

int pos = deck.indexOf(deck.charAt(card));
deck=deck.charAt(pos)="0";


do you mean this
it dont work
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 01:44 PM

No, he means saving the index that it takes...and using it like this.

deck[indexSaved] = "0";

Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 01:52 PM

i meant something like this
String result = "";

for(int i = 0; i < str.length(); i++)
{
	if(randomIndex == i)
		result += 0;
	else
		result += str.charAt(i);
}


str is the original string, and randomIndex is the index of the number from the string that used for a card..it should be clear now

This post has been edited by mostyfriedman: 18 July 2009 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 01:57 PM

View Postmostyfriedman, on 18 Jul, 2009 - 02:52 PM, said:

result += 0;


Careful with that statement. It will give a compilation error relating to something like this -- cannot convert from type int to String.

Use result += "0";

But before, I was under the impression we were using an array...nevermind. ;)

This post has been edited by Locke: 18 July 2009 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 02:04 PM

View PostLocke, on 18 Jul, 2009 - 12:57 PM, said:

View Postmostyfriedman, on 18 Jul, 2009 - 02:52 PM, said:

result += 0;


Careful with that statement. It will give a compilation error relating to something like this -- cannot convert from type int to String.

Use result += "0";

But before, I was under the impression we were using an array...nevermind. ;)


we were using an array until he said he cant use one..but btw it wont give an error since i'm concatenating an int with a string, the result will be also a string..it would give an error i was setting the 0 to the string and not to concatenating it..
like

result = 0;

that would give an error, but the other one wouldnt
Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 02:06 PM

View Postmostyfriedman, on 18 Jul, 2009 - 03:04 PM, said:

that would give an error, but the other one wouldnt


...
...
...

Ok, so you're right. Hm. :hmmm: :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 02:09 PM

View PostLocke, on 18 Jul, 2009 - 01:06 PM, said:

View Postmostyfriedman, on 18 Jul, 2009 - 03:04 PM, said:

that would give an error, but the other one wouldnt


...
...
...

Ok, so you're right. Hm. :hmmm: :rolleyes:


of course, i'm always right :P..
Was This Post Helpful? 1
  • +
  • -

#13 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 08:12 PM

thread closed

This post has been edited by hlln: 19 July 2009 - 12:17 AM

Was This Post Helpful? 0
  • +
  • -

#14 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 09:08 PM

bump
Was This Post Helpful? 0
  • +
  • -

#15 hlln  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 12-July 09

Re: outputing suits like hearts, diamonds, spades

Posted 18 July 2009 - 09:44 PM

bumpguest
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3