Fill the code to split the words in a String and print themimport java.util.Scanner;public class Main{ public static void main(String args[]) { Scanner sc= new Scanner(System.in); String name= ; //fill code to get the words in the String String[] words = ; for(String : ) System.out.println(str); }}
Question
Fill the code to split the words in a String and print themimport java.util.Scanner;public class Main{ public static void main(String args[]) { Scanner sc= new Scanner(System.in); String name= ; //fill code to get the words in the String String[] words = ; for(String : ) System.out.println(str); }}
Solution
Here is the completed code:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter a string:");
String name= sc.nextLine(); //fill code to get the string input from user
String[] words = name.split(" "); //fill code to split the string into words
for(String str : words) //iterate through each word
System.out.println(str); //print each word
}
}
In this code, we first create a Scanner object to read the input from the user. We then prompt the user to enter a string and read this input using sc.nextLine(). The split(" ") method is used to split the string into words at each space character. Finally, we use a for-each loop to iterate through each word in the words array and print it.
Similar Questions
In the below code snippet, fill the code to get the character array for a given string, str.Code : Scanner sc=new Scanner(System.in); String str=sc.next(); char c[] = ; for(char x : c ) System.out.print(x+" ");
import java.io.*;import java.util.*;public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int Myint = scanner.nextInt(); double d = scanner.nextDouble(); scanner.nextLine(); String mystring = scanner.nextLine(); System.out.println(mystring); System.out.println(d); System.out.println(Myint); /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ }}
Reverse a string using StringBuffer.Fill the code snippet provided, to perform the above.Code : Scanner sc=new Scanner(System.in); String str=sc.next(); //Store str using StringBuffer StringBuffer sb = ; ; //reverse using sb String reverse = ; //Convert StringBuffer to String System.out.println(reverse);
import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in the array"); int n = sc.nextInt(); //Declare the array int arr[ ]=new int[n]; //Get the elements from user System.out.println("Enter the elements of the array"); for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } //Code to find the sum of odd elements in the array int sum=findSumOddElements(); // invoke the method System.out.println("The sum is "+); } public int findSumOddElements() { int sum=0; for(int i=0;i<;i++) { if(()!=0) { sum=sum+arr[i]; } } ; //return statement }}
Tom is playing scrabble and forms new words. He wants to count the number of characters present in the word formed by him. Help him to do this using the below java program.Code :import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); //find length and print the same int len= ; System.out.println("The length of the word is "+) ; }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.