• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Java: How to put a String into a Char array

The program will receive the following input of characters (Cards) for a Poker program:

2c 3s 6d 7s 8h Kh Jd 4d Jc Kc Qc Tc 9c Jh 9s 9h 9d Qs Ks Js

I need to store that in an array, and then need to parse the array one at a time to store each Card as an object. I was thinking of creating a while loop to run throgh the array one character at a time.

The thing is, I first need to store that input string into the array. My question is how.

The attached code is what I have so far:

import java.util.*;

public class DriverPoker 
{
   public static void main(String args[]) 
      {
	Scanner card_input = new Scanner(System.in);
		
	char [] stringCardsArray = new char[60];
	stringCardsArray = card_input.nextLine();

Open in new window

0
InfoTechEE
Asked:
InfoTechEE
1 Solution
 
pivarCommented:
Hi,

To parse and fill an array you could do:

        Scanner card_input = new Scanner("2c 3s 6d 7s 8h Kh Jd 4d Jc Kc Qc Tc 9c Jh 9s 9h 9d Qs Ks Js");
               
        char[][] cards = new char ["2c 3s 6d 7s 8h Kh Jd 4d Jc Kc Qc Tc 9c Jh 9s 9h 9d Qs Ks Js".length() / 3 + 1][2];
        int i = 0;
        while (card_input.hasNext()) {
            cards[i++] = card_input.next().toCharArray();
        }


/peter
0
 
InfoTechEEAuthor Commented:
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now