Author Topic: [Java] String as user input  (Read 932 times)

0 Members and 1 Guest are viewing this topic.

Offline Mave

  • TMS Founder
  • Administrator
  • Godfather
  • *********
  • Posts: 116387
[Java] String as user input
« on: October 30, 2011, 01:46 »
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

Quote
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;

Quote
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?
« Last Edit: October 30, 2011, 01:48 by Mave »

Online Stybar

  • Master Member
  • ********
  • Posts: 8117
  • NO DATA AVAILABLE
Re: [Java] String as user input
« Reply #1 on: October 30, 2011, 02:09 »
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:
Quote
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:
Quote
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

Offline Mave

  • TMS Founder
  • Administrator
  • Godfather
  • *********
  • Posts: 116387
Re: [Java] String as user input
« Reply #2 on: October 30, 2011, 02:15 »
Well thanks:D

Why didn't I try that myself >.>

Though I find it a bit odd that it DOES work like this..

Online Stybar

  • Master Member
  • ********
  • Posts: 8117
  • NO DATA AVAILABLE
Re: [Java] String as user input
« Reply #3 on: October 30, 2011, 02:17 »
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.

Offline GPow69

  • consololol ಠ_ಠ
  • Global Moderator
  • Ultimate Member
  • ********
  • Posts: 18872
  • EDM for life ♥
Re: [Java] String as user input
« Reply #4 on: October 30, 2011, 03:43 »
I don't know what's wrong, but I did notice a typo :P

Quote
   System.out.println("Enter some random words");
        randomwords = input.nextLine();

Quote
System.out.println("We are at the end, the loop stops. Random words: " + randomword);


Online Stybar

  • Master Member
  • ********
  • Posts: 8117
  • NO DATA AVAILABLE
Re: [Java] String as user input
« Reply #5 on: October 30, 2011, 04:04 »
yes, but I recreated the code, mine contained no typo, and it didn't work with me either...