Answer:
import java.util.Scanner;
public class ParseStrings {
public static void main(String[] args) {
/* Type your code here. */
Scanner sc= new Scanner(System.in);//to read inout from user
String sentence;//to hold user input
System.out.println("Enter input string");//asking user to input string reading into sentence
sentence=sc.nextLine();
while(!sentence.equals("q"))//continues untill user enters q
{
while(!sentence.contains(","))//loop ti eport error and continue till user enters valid string
{
System.out.println("No comma in string");
System.out.println("Enter input string");
sentence=sc.nextLine();
}
//extracting words from senetnece
String word1=sentence.substring(0, sentence.indexOf(',')).trim();
String word2=sentence.substring(sentence.indexOf(',')+1).trim();
System.out.println("First word:"+word1);
System.out.println("Second word:"+word2);
System.out.println("Enter input string");//asking user to input string reading into sentence
sentence=sc.nextLine();
}
return;
}
}
Explanation: