简体   繁体   中英

program won't loop after first user input

I have been trying to write this code where a user enters two words with or without a comma and if it does not have a comma print the an error saying so and then loop back to ask for another set of words. Yes this is homework and I have searched the internet for help and it just has not clicked with me so far. I am needing help with the loop in my code which is java. These are the set of requirements for my warm up program followed by my code. Thank you for any help anyone can give.

1) Prompt the user for a string that contains two strings separated by a comma. 2) Report an error if the input string does not contain a comma. 3) Extract the two words from the input string and remove any spaces. Store the strings in two separate variables and output the strings. 4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. Here is my code:

import java.util.Scanner;
public class ParseStrings {

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);
        Scanner inSS = null;
        String lineString = "";
        String firstWord = "";
        String secondWord = "";
        boolean inputDone = false;

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

        while (!inputDone) {
            lineString = scnr.nextLine();
            inSS = new Scanner(lineString);

            if (firstWord.equals("q")) {
                System.out.println("Exiting.");
                inputDone = true;

            }

            if (lineString.contains(",")) {
                String[] parts = lineString.trim().split("\\s*,\\s*");
                firstWord = parts[0];
                secondWord = parts[1];
                System.out.println("First word: " + firstWord);
                System.out.println("Second word: " + secondWord);
            } else {
                System.out.println("Error: No comma in string");

            }
            break;
        }
        return;
    }
}

1) You do not need the return statement for a main method of type void

2) The break at the end of your loop is what is terminating it after the first run.

Writing break; within your loop is the same thing as telling your loop to stop looping. If you want to define another condition to terminate your loop, but dont want to put it in your while , then put your break inside of some sort of condition, that way it doesn't happen every single time.

I am trying to figure out when the user enters q the program quits here is my code so far.

import java.util.Scanner;

public class ParseStrings {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    while (true) {
        String s = sc.nextLine();
        System.out.println("Enter input string: ");
        if (s.indexOf(",") == -1) //checks if there is a comma in the string
        {
            System.out.println("Error: No comma in string");
        } else {
            //there is a comma in the string
            String s1 = s.substring(0, s.indexOf(","));
            String s2 = s.substring(s.indexOf(",") + 1);
            s1 = s1.replace(" ", "");
            s2 = s2.replace(" ", "");
            //store both the strings in s1 and s2
            System.out.println("First word: " + s1);

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

            s1 = s1.trim();
            s2 = s2.trim();  //remove extra spaces

            System.out.println(s1 + " " + s2);
            break;
        }
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM