Solved

Java: How to put a String into a Char array

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 83
allswap challenge 6 76
get weblogic logged in user in java 2 57
varialbe initialization 11 30
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

914 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

19 Experts available now in Live!

Get 1:1 Help Now