Answered You can hire a professional tutor to get the answer.

QUESTION

I can't seem to figure out how to get my code to move to a New line.*; import java.

I can't seem to figure out how to get my code to move to a New line... can you help

// import statements

import java.lang.*;

import java.util.*;

// Create a class named ParseStrings

public class ParseStrings

{

   // Create a main method

   public static void main(String[] args)

   {

       // Create a string named inputString to accept input from the user

       String inputString, firstWord, secondWord;

       int comma = 0, nextComma = 0;

       // Create an object for the scanner class

       Scanner read = new Scanner(System.in);

       // Create a boolean variable named flag

       boolean flag = true;

       // Create a do while loop to accept data

       // continuously if the user enters wrong input

       while (true)

       {

           do

           {

               System.out.println("Enter input string: ");

               // Accept data from the user

               inputString = read.nextLine();

               if (inputString.equals("q"))

               {

                   System.exit(1);

               }

               // Get the index value of the comma

               comma = inputString.indexOf(",");

               nextComma = inputString.lastIndexOf(",");

               // Create a if statement to check the input string

               // contains more commas or not

               if (nextComma > comma)

               {

                   System.out.println("Error: Too many comma in string");

                   flag = true;

               }

               // Create an else if statement to check the input string

               // contains atleast one comma or not

               else if (comma <= 0)

               {

                   System.out.print("Error: No comma in string");

                   flag = true;

               }

               else

               {

                   // Split the string from the index of comma and

                   // assign the substrings in the data string array

                   String[]data = inputString.split(",");

                   // Remove all white spaces in the data string

                   firstWord = data[0].replaceAll(" ", "");

                   secondWord = data[1].replaceAll(" ", "");

                   // Display data

                   System.out.println("First word: " + firstWord );

                   System.out.println("Second word: " + secondWord );

                   flag = false;

               }

           } while (flag);

       }

   }

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question