Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi I am figuring out how to split the strings so heres my code: because i using bufferedreader and i have two textboxes so it reads both the text boxes (the 1st textbox i type john), the second textbox i type peter) the output is johnpeter so i trying to split the textboxes instead of reading just 1 line straight.

BufferedReader reader = new BufferedReader(new InputStreamReader(
            req.getInputStream()));
         String name;

    while ((name = reader.readLine().toString()) != null) 
    {

        Statement stmt;


  String[] players = name.split("");
        String playerO = players[1]; 
        String playerX = players[2];

Current output is:

Player 1 :j
Player 2 :o

I would like my output to be:

Player 1 :john
Player 2 :peter
share|improve this question

closed as unclear what you're asking by 323go, vorrtex, Raedwald, DwB, Roman C Jul 17 '13 at 19:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

5  
Well, split on what?? –  Rohit Jain Jul 17 '13 at 18:03
    
code not compilable –  Jigar Joshi Jul 17 '13 at 18:03
    
@JigarJoshi Probably the lack of quotes on johnpeter –  StephenTG Jul 17 '13 at 18:04
3  
You're better off doing a substring, because the character you split on gets lost in the process. But that means you would need to know the length of what the 2 parts will be. Why not add a , between the parts then do split(",") –  eldris Jul 17 '13 at 18:06
    
because i using bufferreader it read the line straight ,so i want to split them –  Gim Yong Jul 17 '13 at 18:32

3 Answers 3

As is, you won't be able to split the string where you want to, as there's no clear delimiting character. If you stored it as "john peter" or "john,peter" or something like that, it would be easier to split.

Then you would just need to change

String[] players = name.split("");

to String[] players = name.split(" ");

or String[] players = name.split(",");

Also, as others have mentioned, remember that the first item in players is players[0], not players[1]

share|improve this answer

As others have alluded to, your original string "johnpeter" needs to instead be something like

"john,peter,joey,tom,dick,harry";

then you can

String name = "john,peter,joey,tom,dick,harry"; 
String[] players = name.split(",");
String playerO = players[0]; 
String playerX = players[1];


System.out.println("Player 1 :" + players[O]);//or, playerO
System.out.println("Player 2 :" + players[1]);//or, playerX

Note the zero-base of the array, as well. Hope this helps!

share|improve this answer
    
+1 Good point about the zero-base on the array, I had missed that –  StephenTG Jul 17 '13 at 18:12

I am not sure what you trying to do since split("on what").

Try some thing like this if space between names,

    String name = "john peter";
    String[] players = name.split(" ");
    String playerO = players[0];
    String playerX = players[1];


    System.out.println("Player 1 :" +playerO);
    System.out.println("Player 2 :" +playerX);

If you want to split("??") there should be (split on what) identifier

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.