Marko Scekic
Marko Scekic

Reputation: 101

Code fails when multiple words inputed

:D I'm completly new to Java and now I'm doing basic examples(with Java 1.7 and Eclipse IDE)

I have tried the code below.It should take user inputs an them display them in the end.

package loop;
import java.util.Scanner;

public class MainClass {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner user_input = new Scanner( System.in );

    String name;
    System.out.print("What is yout name?: ");
    name = user_input.next();

    String quest;
    System.out.print("What is your quest?: ");
    quest = user_input.next();

    String color;
    System.out.print("What is your favourite color?: ");
    color = user_input.next();

    String sentence;
    sentence = "Your name is" + " " + name + " " + ",and your quest is" + " " + quest + " " + ",and your favourite color is" + " " + color;

    System.out.println(sentence);





    }

}

and everything thing works fine when I use only one word for input like here

What is yout name?: Marko
What is your quest?: seeking
What is your favourite color?: green
Your name is Marko ,and your quest is seeking ,and your favourite color is green

but everything fails with multiple words inputed:

What is yout name?: Marko Scekic
What is your quest?: What is your favourite color?: seeking blue
Your name is Marko ,and your quest is Scekic ,and your favourite color is seeking

What is yout name?: Marko
What is your quest?: seeking for Holy grail
What is your favourite color?: Your name is Marko ,and your quest is seeking ,and your favourite color is for

I tried everything that is in my power(I replaced print with println and reversed,tried changing the order of the questions etc..).

Upvotes: 2

Views: 95

Answers (4)

John
John

Reputation: 2042

To expand on what Reimeus said: Scanner.next() returns the next token from the scanner. Basically, it regards whitespace as a delimiter between tokens meaning that "Marko Scekic" is two different tokens: "Marko" being the first, and "Scekic" being the second.

You may be thinking of it like this: first the code does a System.out.println("What is your name?"); then asks the user for information using next() and then prints something else with System.out.println() so it seems that next() should give everything that was input between the two println() calls.

Unfortunately, the Scanner class doesn't know anything at all about whether System.out.println() has been called or not. From its point of view the following are exactly the same:

String name;
System.out.print("What is yout name?: ");
name = user_input.next();

String quest;
System.out.print("What is your quest?: ");
quest = user_input.next();

and

String name;
name = user_input.next();

String quest;
quest = user_input.next();

In other words, printing things using System.out.println() has no effect on what next() does.

So you need to either use something different than Scanner, or as Reimus said, use nextLine(), since this will return everything from the beginning of the line until the point that the user hits the enter key (which is a '\n' character).

Upvotes: 1

tyty5949
tyty5949

Reputation: 1530

If you want to use scanner, you will need to use the nextLine() method from Scanner to get the whole line. It reads in the whole line including spaces. Now using the next() method only reads up until the first space then stops.

String name;
System.out.print("What is yout name?: ");
name = user_input.nextLine();
//etc...

Link to java doc

Upvotes: 1

najk
najk

Reputation: 123

As Reimeus stated earlier you should use user_input.nextLine();

This will read the complete line, and will read everything until \n is found, which means a new line, in your case the user hits enter.

Upvotes: 2

Florian Groetzner
Florian Groetzner

Reputation: 512

You should better use a BufferedReader

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = bufferedReader.readLine();

to get input:

For example:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

String name;
System.out.print("What is yout name?: ");
name = bufferedReader.readLine();

Upvotes: 1

Related Questions