Solved

Java: How to put a String into a Char array

Posted on 2011-09-03
2
321 Views
Last Modified: 2012-05-12
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
Comment
Question by:InfoTechEE
2 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
Comment Utility
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
 

Author Comment

by:InfoTechEE
Comment Utility
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now