Java: User Input

I attached the first few lines of my main class. As you can see the very first thing it does is ask for user input. Which is great when I work in Eclipse.
I provide the input: 2c 3s 6d 7s 8h Kh Jd 4d Jc Kc Qc Tc 9c Jh 9s 9h 9d Qs Ks Js
and everything works great.

However, last minute, the professor said he will be running our programs through the following command line command:
java CrazyPoker 2c 3s 6d 7s 8h Kh Jd 4d Jc Kc Qc Tc 9c Jh 9s 9h 9d Qs Ks Js

How do I change my code to accept the above command as user input.
 
public class Game 
{
   public static void main(String args[])
   {
     Scanner scanInput = new Scanner(System.in);
     String card_input = scanInput.nextLine();
		
     char [] cardsArray = new char[card_input.length()];
     cardsArray = card_input.toCharArray();

Open in new window


Here's the actual full code of the class:
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

// 2c 3s 6d 7s 8h Kh Jd 4d Jc Kc Qc Tc 9c Jh 9s 9h 9d Qs Ks Js
/**
 * Game class takes in user input, stores in an array, and sends the cards for evaluation.
 * @author
 * 
 */
public class Game 
{
	public static void main(String args[])
	{
		double amtPlayers;
		int finalPlayers;
		
		Scanner scanInput = new Scanner(System.in);
		String card_input = scanInput.nextLine();
		
		char [] cardsArray = new char[card_input.length()];
		cardsArray = card_input.toCharArray();
		
		int counter = 0;
		int community = 15;
		double playerChars = 9.0;
		int player = 9;
		
		amtPlayers = Math.ceil((card_input.length()-community)/playerChars);
		finalPlayers = (int) amtPlayers;
		
		// create array to store the results of the hand evaluation
		int handStrength[] = new int[finalPlayers+1];
		
		// Community Cards
		ArrayList<Card> communityArrayList = new ArrayList<Card>();		
		while(counter<community)
		{
			Card cardObj = new Card(cardsArray[counter], cardsArray[counter+1]);
			communityArrayList.add(cardObj);
			counter += 3;
		}
		ArrayList<PlayerStrength> obj = new ArrayList<PlayerStrength>();
		
		// For Loop[i] iterates through the players -- depending on how many cards supplied
		for(int i=1;i<=finalPlayers;i++)
		{
			ArrayList<Card> playerArrayList = new ArrayList<Card>();
			
			// For Loop [j] iterates through each (player's) or (i's) cards, adds them to a hand, evaluates.
			for(int j=counter;j<(community+(player*i));j+=3)
			{
				Card cardObj = new Card(cardsArray[j], cardsArray[j+1]);
				playerArrayList.add(cardObj);
				counter+=3;
			}
			
			/**
			 * A series of for loops that send the Hand Evaluator all possible hand combinations
			 */
			int [] initialStrength = new int[15];
			int z=0;
			for(int p1=0;p1<3;p1++)
				for(int p2=p1+1;p2<3;p2++)
				{
					for(int c1=0;c1<5;c1++)
						for(int c2=c1+1;c2<5;c2++)
							for(int c3=c2+1;c3<5;c3++)
								for(int c4=c3+1;c4<5;c4++)
								{
									ArrayList<Card> playerHand = new ArrayList<Card>();
									playerHand.add(communityArrayList.get(c1));
									playerHand.add(communityArrayList.get(c2));
									playerHand.add(communityArrayList.get(c3));
									playerHand.add(communityArrayList.get(c4));
									playerHand.add(playerArrayList.get(p1));
									playerHand.add(playerArrayList.get(p2));
									
									Hand theHand = new Hand(); // evaluation hand
									initialStrength[z] = theHand.evaluateHand(playerHand); //store hand strength here
									z++;
								}
				}
			Arrays.sort(initialStrength);
			handStrength[i]=initialStrength[initialStrength.length-1];
			
			PlayerStrength plyr = new PlayerStrength(handStrength[i],i);
			obj.add(plyr);		
		}
        Collections.sort(obj, new Comparator<PlayerStrength>()
        		{
        			public int compare(PlayerStrength o1, PlayerStrength o2)
        			{
        				return o1.compareTo(o2);
        			}            
        		});
		
        for(int i=0;i<finalPlayers;i++)
		{
		/*	Prints winning players in order + their score/strength
		 * 
		 * System.out.println("Player "+obj.get(i).accessPlayer() + 
		 * 				" -- " + obj.get(i).accessStrength());
		 */
        	System.out.println("Player "+obj.get(i).accessPlayer());
		}
	}
}

Open in new window

InfoTechEEAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
I think instaend of reading from keyboard you ca do it this way

public static void main(String args[]){

String card_input = "";

for(int j=0; j<args.length; j++){

card_input += args[j] + " ";


}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gurvinder Pal SinghCommented:
basically now the array of inputs is the 'args[]' array that appears as the parameter of the main method. So now you don't have to prepare the array (from scanner) to process the same.
0
for_yanCommented:

I cannot test as I don't have other calsses - but if you just take one string with all cards
and then parse it and if you always have one space between the cards, I think this should work
Just be careful - do you want the space in fornt of the frist card - then you should start with
  String card_input = " "; and then add  card_input +=" " + args[j] ;
and remove this:    card_input = card_input.trim();

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

// 2c 3s 6d 7s 8h Kh Jd 4d Jc Kc Qc Tc 9c Jh 9s 9h 9d Qs Ks Js
/**
 * Game class takes in user input, stores in an array, and sends the cards for evaluation.
 * @author
 *
 */
public class Game
{
	public static void main(String args[])
	{
		double amtPlayers;
		int finalPlayers;

		//Scanner scanInput = new Scanner(System.in);
	//	String card_input = scanInput.nextLine();
        
        String card_input = "";

for(int j=0; j<args.length; j++){

card_input += args[j] + " ";


}
        card_input = card_input.trim();

		char [] cardsArray = new char[card_input.length()];
		cardsArray = card_input.toCharArray();

		int counter = 0;
		int community = 15;
		double playerChars = 9.0;
		int player = 9;

		amtPlayers = Math.ceil((card_input.length()-community)/playerChars);
		finalPlayers = (int) amtPlayers;

		// create array to store the results of the hand evaluation
		int handStrength[] = new int[finalPlayers+1];

		// Community Cards
		ArrayList<Card> communityArrayList = new ArrayList<Card>();
		while(counter<community)
		{
			Card cardObj = new Card(cardsArray[counter], cardsArray[counter+1]);
			communityArrayList.add(cardObj);
			counter += 3;
		}
		ArrayList<PlayerStrength> obj = new ArrayList<PlayerStrength>();

		// For Loop[i] iterates through the players -- depending on how many cards supplied
		for(int i=1;i<=finalPlayers;i++)
		{
			ArrayList<Card> playerArrayList = new ArrayList<Card>();

			// For Loop [j] iterates through each (player's) or (i's) cards, adds them to a hand, evaluates.
			for(int j=counter;j<(community+(player*i));j+=3)
			{
				Card cardObj = new Card(cardsArray[j], cardsArray[j+1]);
				playerArrayList.add(cardObj);
				counter+=3;
			}

			/**
			 * A series of for loops that send the Hand Evaluator all possible hand combinations
			 */
			int [] initialStrength = new int[15];
			int z=0;
			for(int p1=0;p1<3;p1++)
				for(int p2=p1+1;p2<3;p2++)
				{
					for(int c1=0;c1<5;c1++)
						for(int c2=c1+1;c2<5;c2++)
							for(int c3=c2+1;c3<5;c3++)
								for(int c4=c3+1;c4<5;c4++)
								{
									ArrayList<Card> playerHand = new ArrayList<Card>();
									playerHand.add(communityArrayList.get(c1));
									playerHand.add(communityArrayList.get(c2));
									playerHand.add(communityArrayList.get(c3));
									playerHand.add(communityArrayList.get(c4));
									playerHand.add(playerArrayList.get(p1));
									playerHand.add(playerArrayList.get(p2));

									Hand theHand = new Hand(); // evaluation hand
									initialStrength[z] = theHand.evaluateHand(playerHand); //store hand strength here
									z++;
								}
				}
			Arrays.sort(initialStrength);
			handStrength[i]=initialStrength[initialStrength.length-1];

			PlayerStrength plyr = new PlayerStrength(handStrength[i],i);
			obj.add(plyr);
		}
        Collections.sort(obj, new Comparator<PlayerStrength>()
        		{
        			public int compare(PlayerStrength o1, PlayerStrength o2)
        			{
        				return o1.compareTo(o2);
        			}
        		});

        for(int i=0;i<finalPlayers;i++)
		{
		/*	Prints winning players in order + their score/strength
		 *
		 * System.out.println("Player "+obj.get(i).accessPlayer() +
		 * 				" -- " + obj.get(i).accessStrength());
		 */
        	System.out.println("Player "+obj.get(i).accessPlayer());
		}
	}
}

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

ksivananthCommented:
make it flexible so that it runs in both mode! try this,

public static char [] getInput( String args[] ){
  if( args != null && args.length > 0 )
     return args[0].toCharArray() ;
  }

  System.out.println( "Input: " ) ;
            Scanner scanInput = new Scanner(System.in);
            String card_input = scanInput.nextLine();
            
            char [] cardsArray = new char[card_input.length()];
            cardsArray = card_input.toCharArray();
  return cardsArray ;

}
      public static void main(String args[])
      {
            double amtPlayers;
            int finalPlayers;
            
            char [] cardsArray = getInput( args )
0
Hugh McCurdyCommented:
Since this is for a class, for educational purposes, I'd like you to compile and run this class.  It mostly uses for_yan's code.  I think it will help educationally if you see what happens.
public class Test
{

  public static void main(String args[])
  {

    String card_input = "";

    for(int j=0; j<args.length; j++)
    {
      card_input += args[j] + " ";
    } // for

    System.out.println ( card_input );

  } // main()

} // Test class

Open in new window

0
CEHJCommented:
You need to do two things:

a. Change the name of the main class to CrazyPoker
b. Instead of line 20 (in your full code) use the below
StringBuilder sb = new StringBuilder();
	String sep = "";
	for(int i = 0;i < args.length;i++) {	
	    sb.append(sep).append(args[i].toString());
	    sep = " ";
	}
	String card_input = sb.toString();

Open in new window

0
CEHJCommented:
The accepted answer won't meet your teacher's requirements btw
0
InfoTechEEAuthor Commented:
How so? Seems to be working.

Using this now....

public static void main(String args[]){

String card_input = "";

for(int j=0; j<args.length; j++){

card_input += args[j] + " ";
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.