-7

I have a error when i give a input for string t= "is my favorite language"; it shows output java is . please tell what i made a mistake.

public class DataTypes {

        public static void main(String[] args) {

            String s = "Java ";

            Scanner scan = new Scanner(System.in);

            String t = scan.next();

           String u = s.concat(t);
            System.out.println(u);
    }
    }
Tom
  • 14,120
  • 16
  • 41
  • 47
user306128
  • 101
  • 3
  • 3
    use `scan.nextLine()` – dryairship Jul 05 '16 at 13:12
  • 4
    See [What's the difference between next() and nextLine() methods from Scanner class?](http://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class). – Magnus W Jul 05 '16 at 13:14
  • You're here long enough to know that you're not allowed to change your question to something else, if there is already at least one answer about your original question. So I rolled your last edit back. If you have a new issue, then create a new question. – Tom Jul 05 '16 at 13:39
  • @ Tom,but someone will be tell it's already asked question? – user306128 Jul 05 '16 at 13:42

2 Answers2

0

scan.next() will get the next token from the input. The default delimiter for this is whitespace, so it will get the first word from your input.

To get all of the input up until the newline (when the user presses enter) use scan.nextLine() instead.

String s = "Java ";
Scanner scan = new Scanner(System.in);

String t = scan.nextLine();

String u = s + t;
System.out.println(u);
explv
  • 2,598
  • 7
  • 16
0

Use:

scanner.nextLine()

This will read a whole line till the system defined line seperator (usually \n)

scanner.next() only reads the next token till space.

Kushan
  • 5,275
  • 2
  • 27
  • 41