0
import java.util.*;
public class MovieInfo
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the name of the film");
    String name = in.nextLine();
    System.out.println("Enter the film budget");
    int bud = in.nextInt();
    System.out.println("Enter the running time");
    double time = in.nextDouble();
    System.out.println("Enter the film production house");
    String s1 = in.nextLine(); 
    System.out.println("Film being screened : "+name);
    System.out.println("Budget "+bud+" cores");
    System.out.println("Running time "+time+" hours");
    System.out.println("Production : "+s1);
}
}

In the output of the above program, it does not take input for String s1. How do I fix it? Any help would be appreciated.

shreyas.k
  • 111
  • 1
  • 11
  • 1
    what exactly do you mean? what is your input? what does your print statement print? – Stultuske Mar 28 '18 at 08:58
  • Run the above program in your system. We are able to give input to String name, int bud, double time but when it comes to next line it just prints "Enter the film production house" and terminates the program. It is not taking any input to String s1. @Stultuske – shreyas.k Mar 28 '18 at 09:05

2 Answers2

3

Try to put the following additional line:

in.nextLine();

after that line:

double time = in.nextDouble();

to consume new line character (it's created when you accept input double with Enter key) that is not being consumed with .nextDouble() call.


Please refer to link to that question, provided by LenosV, to get detailed information on what's the reason of that behavior of Scanner.

Przemysław Moskal
  • 3,321
  • 2
  • 8
  • 20
0

This will work

import java.util.*;
public class MovieInfo
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the name of the film");
    String name = in.nextLine();
    System.out.println("Enter the film budget");
    int bud = in.nextInt();
    System.out.println("Enter the running time");
    double time = in.nextDouble();
    System.out.println("Enter the film production house");
    String s1 = in.next(); 
    System.out.println("Film being screened : "+name);
    System.out.println("Budget "+bud+" cores");
    System.out.println("Running time "+time+" hours");
    System.out.println("Production : "+s1);
}
}
Anto Livish A
  • 282
  • 3
  • 15