0

The code below works perfectly when ran, but if you enter two words in the "bands" question you'll only get one printed back.

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("What is your name?");
    String name;
    name = scan.next();
    System.out.println("Hello " + name);
    System.out.println("What is your age?");
    int years;
    years = scan.nextInt();
    int ageInMonths;
    ageInMonths = years * 12;
    System.out.print("Your age is ");
    System.out.print(ageInMonths);
    System.out.println(" in months");
    System.out.println("What are your favorite two bands?");
    String bands = scan.next();
    System.out.println("I like >>" + bands + "<<too!");
  }
}

2 Answers2

0

The method next() from the class Scanner will return only the next token.

As written in the oracle docu:

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

The default delimiter ist a space, so if your band name will contain more then one word, it will not work.

For reading a whole line of input user nextLine()

If you want to have both band names separatly, call nextLine() twice to get each input separatly:

System.out.println("What are your favorite two bands?");
String band1 = scan.nextLine();
String band2 = scan.nextLine();
System.out.println("I like >>" + band1 + " and " + band2 + "<<too!");

In this case the user has to press 'enter' after each input.

EDIT:

As mentioned in the comment of Elliott Frisch, other readXXX methods will not remove the lineendings from the inputstream. see: stackoverflow.com/q/13102045/2970947

You can use nextLine() for every input or remove the lineending after each reading. Sample with your code:

    Scanner scan = new Scanner(System.in);
    System.out.println("What is your name?");
    String name;
    name = scan.next();
    scan.nextLine();
    System.out.println("Hello " + name);
    System.out.println("What is your age?");
    int years;
    years = scan.nextInt();
    scan.nextLine();
    int ageInMonths;
    ageInMonths = years * 12;
    System.out.print("Your age is ");
    System.out.print(ageInMonths);
    System.out.println(" in months");
    System.out.println("What are your favorite two bands?");
    String bands = scan.nextLine();
    System.out.println("I like >>" + bands + "<<too!");

API References:

next()

nextLine()

Jarlik Stepsto
  • 1,366
  • 7
  • 16
0

To get a full string with multiple words (until enter press) you should use scan.nextLine() instead of scan.next() (scan.next() is use to read next word only).

Do not forget to add an extra scan.nextLine() after scan.nextInt()and beforescan.nextLine()` (see it in Java Scanner class reading strings):

public static void main(String... args) {
    try (Scanner scan = new Scanner(System.in)) {
        System.out.print("What is your name? ");
        String name = scan.nextLine();
        System.out.println("Hello " + name);

        System.out.print("What is your age? ");
        int years = scan.nextInt();
        int ageInMonths = years * 12;
        System.out.println("Your age is " + ageInMonths + " in months");

        System.out.print("What are your favorite two bands? ");
        scan.nextLine();    // extra one after scan.nextInt() - it retrieves a empty string
        String bands = scan.nextLine();
        System.out.println("I like >>" + bands + "<< too!");
    }
}

Demo, console output:

What is your name? John Doe
Hello John Doe
What is your age? 666
Your age is 7992 in months
What are your favorite two bands? one two three
I like >>one two three<< too!
oleg.cherednik
  • 12,764
  • 2
  • 17
  • 25