Write a program that takes in a string, find all the spaces, and output the numb
ID: 3907500 • Letter: W
Question
Write a program that takes in a string, find all the spaces, and output the number of spaces in String. Must use the template given below.public class StringSpaces { public static void main(String[] args) { //Text is inputted here String text = new String(); text = "____."; //Variables taken for computations. Initialize them Appropriately. int index = ; //the index used to find the index of space index int spaceIndex = ; int count = ; /*A while loop is being used. Complete the loop Appropriately to find the solution to this program. / /* The spaceIndex stores the integer index found using the indexOf() method of the String Class. The indexOf() method takes in 2 arguments, one is the String to be found out and the other is the index from where the search should start for the String to be found. I have filled it up for you. */ while(){ spaceIndex =text.indexOf(" ", index); } //Print out the total spaces in the given text. } Write a program that takes in a string, find all the spaces, and output the number of spaces in String. Must use the template given below.
public class StringSpaces { public static void main(String[] args) { //Text is inputted here String text = new String(); text = "____."; //Variables taken for computations. Initialize them Appropriately. int index = ; //the index used to find the index of space index int spaceIndex = ; int count = ; /*A while loop is being used. Complete the loop Appropriately to find the solution to this program. / /* The spaceIndex stores the integer index found using the indexOf() method of the String Class. The indexOf() method takes in 2 arguments, one is the String to be found out and the other is the index from where the search should start for the String to be found. I have filled it up for you. */ while(){ spaceIndex =text.indexOf(" ", index); } //Print out the total spaces in the given text. } Write a program that takes in a string, find all the spaces, and output the number of spaces in String. Must use the template given below.
public class StringSpaces { public static void main(String[] args) { //Text is inputted here String text = new String(); text = "____."; //Variables taken for computations. Initialize them Appropriately. int index = ; //the index used to find the index of space index int spaceIndex = ; int count = ; /*A while loop is being used. Complete the loop Appropriately to find the solution to this program. / /* The spaceIndex stores the integer index found using the indexOf() method of the String Class. The indexOf() method takes in 2 arguments, one is the String to be found out and the other is the index from where the search should start for the String to be found. I have filled it up for you. */ while(){ spaceIndex =text.indexOf(" ", index); } //Print out the total spaces in the given text. }
Explanation / Answer
public class StringSpaces {
public static void main(String[] args) {
//Text is inputted here
String text = new String();
text = "____.";
//Variables taken for computations. Initialize them Appropriately.
int index = 0; //the index used to find the index of space index
int spaceIndex = 0;
int count = 0;
/*A while loop is being used. Complete the loop Appropriately to find the solution to this program. /
/* The spaceIndex stores the integer index found using the indexOf() method of the String Class. The indexOf() method takes in 2 arguments, one is the String to be found out and the other is the index from where the search should start for the String to be found. I have filled it up for you. */
while(spaceIndex != -1){
spaceIndex =text.indexOf(" ", index);
// The start value should begin from one position after the current position of the space
index = spaceIndex+1;
// Increament of count calue after each occurance of the space
count++;
}
//Print out the total spaces in the given text.
System.out.println(count);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.