[Java] String as user input

Mave

TMS Founder
Administrator
Messages
234,196
Location
Belgium
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

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?
 
it worked for me when I put the "random words" part before the number part... but that does also mean that I have to input the words before the number. hth!
the code:
public class userinput {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String randomword;
System.out.println("enter some random words");
randomword=input.nextLine();
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();


do{
System.out.println("we now display the counter: " +startcounter+ " and we add the value.");
startcounter+=plus;
} while(startcounter<=userstopcounter);
System.out.println("we are at the end, the loop stops. random words: "+randomword);

}
}

and the result:
enter some random words
appel
Where to start?
1
where to stop
10
count with
1
we now display the counter: 1 and we add the value.
we now display the counter: 2 and we add the value.
we now display the counter: 3 and we add the value.
we now display the counter: 4 and we add the value.
we now display the counter: 5 and we add the value.
we now display the counter: 6 and we add the value.
we now display the counter: 7 and we add the value.
we now display the counter: 8 and we add the value.
we now display the counter: 9 and we add the value.
we now display the counter: 10 and we add the value.
we are at the end, the loop stops. random words: appel

Process finished with exit code 0
 
Well thanks:biggrin:

Why didn't I try that myself >.>

Though I find it a bit odd that it DOES work like this..
 
Me too... My best guess is that it handels the do while as soon as it has all the input it needs, but I'm not sure.
 
yes, but I recreated the code, mine contained no typo, and it didn't work with me either...
 
Back
Top Bottom