So I am experimenting a little bit with user input and then using that input to display it with a System.out.println now I wanted to let the user enter some words and then output them at the bottom at my script.
It works with a single word when I use input.next();
Whenever I use input.nextLine(); the script ignores the user input for that and just goes to the do while part.
Code
When I compile that I get;
It does ask for the input, but there's no time to give it since it goes straight to the do while.
Any solution for this?
It works with a single word when I use input.next();
Whenever I use input.nextLine(); the script ignores the user input for that and just goes to the do while part.
Code
import java.util.Scanner;
public class DoWhile {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int startcounter;
System.out.println("Where to start?");
startcounter=input.nextInt();
int userstopcounter;
System.out.println("Where to stop?");
userstopcounter = input.nextInt();
int plus;
System.out.println("Count with?");
plus = input.nextInt();
String randomword;
System.out.println("Enter some random words");
randomwords = input.nextLine();
do{
System.out.println("We now display the counter: " + startcounter + " and we add the user-inputted value.");
startcounter = startcounter + plus;
}while(startcounter <= userstopcounter);
System.out.println("We are at the end, the loop stops. Random words: " + randomword);
}
}
When I compile that I get;
Where to start?
1
Where to stop?
10
Count with?
1
Enter some random words
We now display the counter: 1 and we add the user-inputted value.
We now display the counter: 2 and we add the user-inputted value.
We now display the counter: 3 and we add the user-inputted value.
We now display the counter: 4 and we add the user-inputted value.
We now display the counter: 5 and we add the user-inputted value.
We now display the counter: 6 and we add the user-inputted value.
We now display the counter: 7 and we add the user-inputted value.
We now display the counter: 8 and we add the user-inputted value.
We now display the counter: 9 and we add the user-inputted value.
We now display the counter: 10 and we add the user-inputted value.
We are at the end, the loop stops. Random words:
Process finished with exit code 0
It does ask for the input, but there's no time to give it since it goes straight to the do while.
Any solution for this?