0
I want take String as input and break them in words and this every words assigned to the variables in java
Plz help me
5 Réponses
+ 6
Mango Man
You need to know number_of_words or max_number_of_words in String before making those variables, if not then you can use index of words array or you can go through this link to generate variables dynamically in java:
https://stackoverflow.com/questions/19336202/how-to-create-variables-dynamically-in-java/19336268#19336268
+ 4
//for input:
String str=new Scanner(System.in).nextLine();
//for splitting string str into words separated by spaces:
String words[]=str.trim().split("\\s+");
you can find number of words as number of elements in words array & can access word at any position in it using its index.
You didn't specified how are words separated in String so I am assuming them to be separated by spaces only.
+ 2
String s="First try Yourself";
char[] a=s.toCharArray();
char[] m=new char[a.length];
int i=0;
for(char t:a){
m[i]=t;
i++;
}
+ 1
String="hello my dear friends";
A=hello
B=my
T=dear
Z=friends
String="where are you";
R=where
Q=are
Z=you
I want this type every string words are assign to new variables
+ 1
Thank u