How to parse a String in Java without using .parse()?

I’m trying to write a program that will take a string and parse it into two outputs, with the delimiter being a comma. It loops until the user enters the character “q”.

i.e.: Console prompts to enter an input, and user inputs “first, second” and then “q” for the second prompt, and the output will be:

Enter input string:
First word: first
Second word: second

Enter input string:

If there is no comma in the input, it throws an error and prompts again

i.e. User inputs “first second” and the output will be:

Enter input string:
Error: No comma in string
Enter input string:

Below is what I have so far:

import java.util.Scanner;

public class ParseStrings {
   public static void main(String[] args) {
         Scanner scnr = new Scanner(; // Scanner for standard input
         Scanner inSS = null;                   // Scanner to write input to buffer
         String firstWord = "";                 // First word string
         String secondWord = "";                // Second word string
         String userInput = "";                 // Input from prompt
         int i = 0;                             // Loop iterator
         boolean inputDone = false;             // Boolean to repeat while loop
         boolean hasComma = false;              // Boolean to check for comma

         // Do while inputDone is false
         while (!inputDone) {

            //Prompt user to input a string
            System.out.println("Enter input string: ");

            // Write string to userInput
            userInput = scnr.nextLine();

            // Exit program if user inputs q
            if((userInput.equals("q"))) {
               inputDone = true;

               // Write userInput to buffer
               inSS = new Scanner(userInput);

               // Write first word from buffer
               firstWord =;

               // Loop through first word string
               for (i = 0; i < firstWord.length(); ++i) {

                  // If char is a comma, write everything after to secondWord and set inputDone to true
                  if(firstWord.charAt(i) == ',') {
                     secondWord =;
                     hasComma = true;

               // If hasComma is false, return error
               if (!hasComma){
                  System.out.println("Error: No comma in string");
               // Else print first word and second word
               else {
                  System.out.println("First word: " + firstWord);
                  System.out.println("Second word: " + secondWord);


  • I don’t know how to remove the comma from the output
  • If there is no space it errors out
  • If there is a space between the first word and the comma, it writes firstWord but does not overwrite the previous value for secondWord (or errors if it’s the first input given)
  • I’m taking a beginner’s Java course and we have not gone over the .parse() method yet, so it’s not really an option at this time.

Thank you in advance!