I want take String as input and break them in words and this every words assigned to the variables in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

28th Mar 2020, 9:05 AM
Santosh Singh
Santosh Singh - avatar
5 Answers
+ 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
28th Mar 2020, 9:39 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 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.
28th Mar 2020, 9:15 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 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++; }
28th Mar 2020, 9:16 AM
Prathvi
Prathvi - avatar
+ 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
28th Mar 2020, 9:30 AM
Santosh Singh
Santosh Singh - avatar
+ 1
Thank u
28th Mar 2020, 9:40 AM
Santosh Singh
Santosh Singh - avatar