-1

In my current project I'm given input from the user in a single String(example: ADD p1 premium), ADD- create a new account, p1- account name, premium- type of the account).

Until now this information was always given to me by different Strings( in this case 3 strings).

The only thing I know about the content is that it's divided by spaces, and that I'll be receiving 3 words.

Is there any method that searches for certain type of char in a string? Thanks

jdldms
  • 1
  • 1
  • 3
    possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – l'L'l Apr 04 '15 at 14:34
  • use `String [] str` = [String.split ( " " )](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-), that will return an array of `String`s, where each index represents a single word, like `ADD`, `p1` and `premium` at `str [ 0 ]`, str [ 1 ]` and `str [ 2 ]` respectively – nIcE cOw Apr 04 '15 at 14:36

2 Answers2

0

This will return a array of splitted words [ADD, p1, premium]

splitted_array[0] = 'ADD'; splitted_array[0] = 'p1'; splitted_array[0] = 'premium';

CODE

String input = "ADD p1 premium";
String[] splitted_array = input.split(" ");
Magesh Vs
  • 82
  • 4
0

You can use String.split(), like this:

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {

    String str = "ADD p1 premium";
    String delimiter = " ";

    String[] tokensVal = str.split(delimiter);

    System.out.println("Operation = " + tokensVal[0]);
    System.out.println("Account name = " + tokensVal[1]);
    System.out.println("Account type = " + tokensVal[2]);

  }
}
Vitaly Olegovitch
  • 3,509
  • 6
  • 33
  • 49