Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I declare a string as a user input, then check to see if it's "yes" or "no"? (JAVA)
    primarykey
    data
    text
    <p>I cannot get my program to take a user input as a string and then see if it equals "yes" or "no" and if it equals neither, then to display "incorrect entry." It always displays "incorrect entry" regardless if I type "yes" or "no." I have tried a few different types of if and do/while's but I just can't seem to get it: </p> <p>Class file: </p> <pre><code>import java.util.Scanner; public class PhysicsProblem { private double vI; // initial velocity private double vF; // final velocity private double t; // time private double deltaX; // change in the x value //Make sure to add acceleration public PhysicsProblem (double vI, double vF, double t, double deltaX) { this.vI = vI; this.vF = vF; this.t = t; this.deltaX = deltaX; } public void setVi(String strVi) { while (!(strVi.equals("no") || strVi.equals("yes"))); { System.out.println("incorrect entry"); } if (strVi.equals("yes")) { System.out.println("Enter the initial velocity: "); vI = new Scanner(System.in).nextDouble(); } if (strVi.equals("no")) { System.out.println("The program is assuming you want to solve" + "for intial velocity"); } } </code></pre> <p>Program: </p> <p>import java.util.Scanner;</p> <pre><code>public class PhysicsProblemSolver { public static void main (String[] args) { double vI = 0; double vF = 0; double t = 0; double deltaX = 0; Scanner scan = new Scanner(System.in); PhysicsProblem problem1 = new PhysicsProblem (vI, vF, t, deltaX); // Checks to see if initial velocity is given System.out.println("Do you know the initial velocity? (Type 'yes' or 'no')"); String strVi = scan.next(); problem1.setVi(strVi); </code></pre> <p>I know it looks like an incomplete program, and it is, but I just needed help with this one section, so I tried not to include the unnecessary parts. Sorry if it's confusing!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload