0

I'm posting a segment of a larger block of code that I'm having trouble with. It should run by itself. For the purpose of testing, just input one at the first prompt. Once it runs the print statement, the program terminates instead of asking for the variable. I don't understand why. Can someone help me?

import java.util.Scanner;

public class Physics {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int switchNumber;
    String variableCaseOne;
    double distance;
    double initialVelocity;
    double time;
    double gravity;

    System.out.println("This section is for projectile motion.");
    System.out.println("Which equation would you like to use?");
    System.out.println("1. Horizontal Equation: D = Vi * t");
    System.out.println("2. Vertical Equation: D = Vi * t - (1/2)g * (t^2)");
    switchNumber = input.nextInt();
    if (switchNumber == 1) {
      System.out.println("Tell me which variable you'd like to solve for.");
      variableCaseOne = input.nextLine();
      if (variableCaseOne.equals("d")) {
        System.out.println("Enter the Initial velocity.");
        initialVelocity = input.nextDouble();
        System.out.println("Enter the time.");
        time = input.nextDouble();
        System.out.println("Distance equals: " + initialVelocity * time);
      }

    }
  }
}

Thank you all for helping!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • 4
    possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – PakkuDon Nov 09 '14 at 00:49

2 Answers2

3

if i have understood correctly try to change

variableCaseOne = input.nextLine();

to

variableCaseOne = input.next();

it works for me

snpt

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);

    int switchNumber;
    String variableCaseOne;
    double initialVelocity;
    double time;

    System.out.println("This section is for projectile motion.");
    System.out.println("Which equation would you like to use?");
    System.out.println("1. Horizontal Equation: D = Vi * t");
    System.out.println("2. Vertical Equation: D = Vi * t - (1/2)g * (t^2)");
    switchNumber = input.nextInt();
    if (switchNumber == 1)
    {
        System.out.println("Tell me which variable you'd like to solve for.");
        variableCaseOne = input.next();
        if (variableCaseOne.equals("d"))
        {
            System.out.println("Enter the Initial velocity.");
            initialVelocity = input.nextDouble();
            System.out.println("Enter the time.");
            time = input.nextDouble();
            System.out.println("Distance equals: " + initialVelocity * time);
        }
    }
    input.close();
}
Andrea Bori
  • 190
  • 2
  • 15
0

Did it also terminate the program when you put in the input a 1?

When:

if (switchNumber == 1)   //the input is not a 1 the program will terminate

Try it with a switch:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int switchNumber;
    String variableCaseOne;
    double distance;
    double initialVelocity;
    double time;
    double gravity;

    System.out.println("This section is for projectile motion.");
    System.out.println("Which equation would you like to use?");
    System.out.println("1. Horizontal Equation: D = Vi * t");
    System.out.println("2. Vertical Equation: D = Vi * t - (1/2)g * (t^2)");        

    switchNumber = input.nextInt();

    switch (switchNumber){

    case 1:   System.out.println("Tell me which variable you'd like to solve for.");
              variableCaseOne = input.next();

              if (variableCaseOne.equals("d")) {
                System.out.println("Enter the Initial velocity.");
                initialVelocity = input.nextDouble();
                System.out.println("Enter the time.");
                time = input.nextDouble();
                System.out.println("Distance equals: " + initialVelocity * time);
              }

    case 2 :  // your next case
    }         
}

Andrea Bori answer is the main Solution for your Problem

Weafs.py
  • 22,731
  • 9
  • 56
  • 78
Cronical
  • 34
  • 8