Solved

Java: How to put a String into a Char array

Posted on 2011-09-03
2
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 36478794
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
ID: 36478868
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 110
collection output issue 9 66
Java Eclipse Loop 3 31
Hibernate Inheritance Strategy for Abstract Base class and two concrete classes 3 20
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

726 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