Solved

Java: How to put a String into a Char array

Posted on 2011-09-03
2
342 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 54
java stored proc example 9 30
servlet filter example 37 62
tomcat startup error 5 64
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 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