Java - Using only .substring procedure to find multiple words within a string -
i'm trying use utilize space " " separate words within string user inputs. example, user input string might "random access memory" or "java development kit." regardless of entered string, space separate each word. in addition this, cannot use .split or array, since common solutions have found far. utilizing .substring allowed. in failed attempts, have been capable of obtaining first inputted word (ex. "random"), cannot separate second , third words (ex. "access memory"). i'm not going publish failed attempts, asking if please provide code separating both second , third words entered? thanks.
p.s. know used create acronyms, can part, need identify each substring.
import javax.swing.*; public class threeletteracronym { public static void main(string[] args) { string wordsinput, initialinput; string first = ""; string second = ""; string third = ""; int firstword; int secondword; int wordlength; int secondwordlength; char c; wordsinput = joptionpane.showinputdialog(null, "please enter 3 words"); initialinput = wordsinput; wordlength = wordsinput.length(); firstword = 0; secondword = 0; while(firstword < wordsinput.length()) { if(wordsinput.charat(firstword) == ' ') { first = wordsinput.substring(0,firstword); second = wordsinput.substring(firstword + 1, wordsinput.length()); //i know spot last 2 words displayed in output, //the closest have been displaying relevant. firstword = wordsinput.length(); } ++firstword; } joptionpane.showmessagedialog(null, "original phrase was: " + initialinput + "\nthree letter acronym is: " + first + second); } }
just separate in words without (strangely) using spring.split:
string mystring = "random access memory"; int begin = 0; int end; while((end = mystring.indexof(" ", begin)) != -1) { system.out.println(mystring.substring(begin, end)); begin = end + 1; } system.out.println(mystring.substring(begin));
Comments
Post a Comment