0

I have developed a character sorter and I want to prompt the user to enter a new string every time after the string is sorted. The issue I am having is that the scanner keeps scanning the first input of the user. If I use scanner.next(), it does not counts the white space at the end of the input, which is not the solution.

This is the start of the while loop. Once the code finishes, it starts again from "inputText".

    inputText:
    while(keepSorting){

        System.out.print("Please input the string to be sorted: " );
        c.settingInput(s.nextLine());
        System.out.println(" ");
        int option;

        more code
        .... }
3
  • nextLine will give you the next line. Not sure what you mean. Commented Oct 1, 2021 at 4:24
  • Do you want to stay inside the while loop? If so, there must be some line of code that redirects to inputText: Commented Oct 1, 2021 at 6:44
  • If you want help debugging your code then you need to post a minimal reproducible example. Commented Oct 2, 2021 at 6:26

1 Answer 1

1
Scanner sc= new Scanner(System.in); 
while(true){
String a = sc.nextLine();
System.out.println(a);

}

If you want to know more about Scanner then read this documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
@LizeshShakya Edited

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.