1

I am having some difficulty with taking a String input from a user as shown in the code below, separating the tokens and shoving them into a string array. The input is recorded correctly however after I try to run the positions.split(" "); the dataString array does not take in the separated strings. What I am trying to do is take a set of numbers, ex: 1 3 4 then separate them into individual tokens and then instantiate a dataString string array that will have a length of 3 and the numbers 1, 3 and 4 inserted in each respective position.

Scanner input = new Scanner(System.in);
String[] dataString;
String positions;

...

System.out.print("Hand:" + currentHand);
input.nextLine();
System.out.print("\nEnter positions of cards to keep (e.g. 1 4 5 ):");
positions=input.nextLine();
dataString = positions.split(" ");

if (dataString.length > 5) {
    System.out.print("You can hold a maximum of 5 cards");
    positions = input.nextLine();
    dataString = positions.split(" ");
}

input.close();
Edward Falk
  • 9,991
  • 11
  • 77
  • 112
TImmuh
  • 57
  • 1
  • 2
  • 10
  • String array should initialize. – Roman C May 16 '13 at 18:29
  • 2
    What do you mean "does not take in"? The assignment doesn't occur? Or you aren't getting the expected result? Likely the former. If you're not getting the proper results what are you getting? Can you paste your program output? – Christian Bongiorno May 16 '13 at 18:29
  • This is what I get as an output when I print out dataString right after performing the positions.split() Balance: $100 Bet: 50 Hand:[A Spades, 9 Hearts, J Clubs, 4 Clubs, 5 Clubs] Enter positions of cards to keep (e.g. 1 4 5 ): 1 [Ljava.lang.String;@48bc9f58 Hand:[A Spades, 9 Hearts, J Clubs, 4 Clubs, 5 Clubs] – TImmuh May 16 '13 at 18:32

2 Answers2

1

It seems to be a possible duplicate of How to split a String by space

On the other hand you should consider using a loop for checking the length of the dataString. :)

while (dataString.length > 5){
   System.out.print("You can hold a maximum of 5 cards");
   positions = input.nextLine();
   dataString = positions.split("\\s+");
}
Community
  • 1
  • 1
András Iványi
  • 317
  • 1
  • 10
0

the dataString array does not take in the separated strings.

Yes, it does. .split(" ") does exactly that to a String like "1 2 3".

Debug your code. Where you have:

dataString = positions.split(" ");

Put:

System.out.println("Positions entered: \""+positions+"\"");
dataString = positions.split(" ");
System.out.println("dataString obtained: "+java.util.Arrays.toString(dataString));

And then if you enter 1 2 3, you'll get as output:

Positions entered: "1 2 3"
dataString obtained: [1, 2, 3]

And you'll see, there is no problem in splitting the String.

Update:

You may be having trouble with duplicate spaces. In that case, you can remove them by using:

dataString = positions.replaceAll("\\s+", " ").trim().split(" ");

Instead of:

dataString = positions.split(" ");

That shall turn strings like " 1 2 3 " into "1 2 3" before splitting.

Community
  • 1
  • 1
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • You're right, however oddly enough there is an empty position that always finds its way in the first array position. Can't get it to show in this comment but there's a space between the 2 and the bracket. Enter positions of cards to keep (e.g. 1 4 5 ): 2 3 5 Positions entered: " 2 3 5" dataString obtained: [ 2, 3, 5] – TImmuh May 16 '13 at 18:55
  • It worked for cutting the space in the first position of the array. However, now the other positions following it have a empty space before. It's [1, 2, 3] instead of [1,2,3]. – TImmuh May 16 '13 at 19:24
  • No, you dont have empty spaces before, that is printing right! When it prints `[1, 2, 3]`, it means there are 3 strings: `"1"`, `"2"` and `"3"`. If it were `" 2"` and `" 3"`, there would be more spaces between the `,` and the numbers, like `[1,__2,__3]`. So, it **is** working. – acdcjunior May 16 '13 at 19:29
  • Perfect, thank you so much! I completely loathe scanner inputs, this helped a great deal. – TImmuh May 16 '13 at 19:30