0
import java.util.Scanner;

public class JavaMovies {
      Scanner det = new Scanner(System.in);
      int age = det.nextInt();

public static void main(String[] args){
    calculator();


}


public static void calculator(){

Scanner det = new Scanner(System.in);

    String rMovie = "Attack of the nerds";
    String mMovie = "Nerd lyfe";
    String pgMovie = "Nerd adventures";
    String gMovie = "Nerd playtime";
    System.out.println("These are the movies that are playing this weekend"
            + rMovie
            + mMovie
            + pgMovie
            + gMovie);
    System.out.println("Please input your age");
   int age = det.nextInt();


System.out.print("Input the movie you wish to see:");
    String x = det.nextLine();                             //Program ends here...

    if(x.equals("Attack of the nerds") & age > 17){
        System.out.println("You can see Attack of the nerds");

    }else if(x.equals("Attack of the nerds") & age < 18){
        System.out.println("You can see attack of the nerds!");

    }if(x.equals("Nerd lyfe")& age > 15){
        System.out.println("You can see Nerd lyfe");

    }else if(x.equals("Nerd lyfe")& age < 16){
        System.out.println("You can not see Nerd lyfe");

    }if (x.equals("Nerd adventures")& age > 10){
        System.out.println("You can see Nerd adventures");

    }else if(x.equals("Nerd adventures")& age < 11){
        System.out.println("You can not see Nerd adventures");

    }if(x.equals("Nerd playtime")& age > 5){
        System.out.println("You can see Nerd playtime");

    }else if(x.equals("Nerd playtime")& age < 6){
        System.out.println("You can not see Nerd playtime");


    }
}
}

Everything is alright up to the start of the if statements, what can I do to fix this?

Console output(using netbeans) run: These are the movies that are playing this weekendAttack of the nerdsNerd lyfeNerd adventuresNerd playtime Please input your age 14 Input the movie you wish to see:BUILD SUCCESSFUL (total time: 4 seconds)

1 Answers1

1

I think the problem is that nextInt doesn't use up the whole line. If you type 32 when prompted for your age, nextInt will consume the 3 and 2 characters, but there is still a newline character remaining in the input string. Then, when you call nextLine, the effect is to return everything that's remaining in the current line, up to the next newline. Since the current line still has a newline character, this results in String x = det.nextLine() setting x to an empty string.

Try det.nextLine(); before prompting for the movie. (You don't need to assign it to a variable; just throw the result away.) That will cause the scanner to consume the newline, so that the next nextLine() will get another line of input.

ajb
  • 29,914
  • 3
  • 49
  • 73