-1
import java.util.Scanner;

public class MadLibs {
    public static void main(String[] args) {
        String name, place, college, profession, animal, petName;
        int number;

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter a name: ");
        name = keyboard.nextLine();

        System.out.print("Enter a number: ");
        number = keyboard.nextInt();

        System.out.print("Enter a place: ");
        place = keyboard.nextLine();

        System.out.print("Enter a college: ");
        college = keyboard.nextLine();

        System.out.print("Enter a profession: ");
        profession = keyboard.nextLine();

        System.out.print("Enter a animal: ");
        animal = keyboard.nextLine();

        System.out.print("Enter a pet name: ");
        petName = keyboard.nextLine();

        keyboard.close();

    }
}

console results

Can't figure out why "enter a college" and "enter a place" are printing on the same line and not acting as separate inputs.

  • Did you input a place? – Tarik Jan 07 '23 at 05:25
  • 1
    `System.out.print` vs `System.out.println` and/or adding and extra blank `System.out.println("");` here or there should help out a bit... – hooknc Jan 07 '23 at 05:31
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Abra Jan 07 '23 at 06:29

2 Answers2

0

Try below snippet it will definitely work :

public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter a name: ");
        String name = keyboard.nextLine();

        System.out.print("Enter a number: ");
        int number = keyboard.nextInt();

        System.out.print("Enter a place: ");
        keyboard.next();
        String place = keyboard.nextLine();

        System.out.print("Enter a college: ");
        keyboard.next();
        String college = keyboard.nextLine();

        System.out.print("Enter a profession: ");
        String profession = keyboard.nextLine();

        System.out.print("Enter a animal: ");
        String animal = keyboard.nextLine();

        System.out.print("Enter a pet name: ");
        String petName = keyboard.nextLine();
    }
Satyajit Bhatt
  • 211
  • 1
  • 7
0

The nextLine() method of java.util.Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end.

enter code here

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String name, place, college, profession, animal, petName;
        int number;

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter a name: ");
        name = keyboard.nextLine();

        System.out.print("Enter a number: ");
        number = keyboard.nextInt();

        System.out.print("Enter a place: ");
        place = keyboard.nextLine();
        keyboard.nextLine(); //brings to next line

        System.out.print("Enter a college: ");
        college = keyboard.nextLine();

        System.out.print("Enter a profession: ");
        profession = keyboard.nextLine();

        System.out.print("Enter a animal: ");
        animal = keyboard.nextLine();

        System.out.print("Enter a pet name: ");
        petName = keyboard.nextLine();

        keyboard.close();

    }
}