-1

When I run the code I am not able to input to the scanner and continue through the code the way I want to. Can someone help me with some advice? I have imported the java.util.Scanner succesfully. BTW, I do call the method in the original program, I just removed it before I posted the question. I am using BlueJ.

public class Instructions extends ConsoleProgram
{
public boolean question(String prompt) {
    Scanner s = new Scanner(System.in);  
    println(prompt);
    String str = s.next();
    boolean result = true;
    while(!(str.equals("yes") || str.equals("no"))) {
        str = s.next();
        println("enter yes or no");
        }
    if (str.equals("yes")) {
    result = true;
    } else if (str.equals("no")) {
    result = false;
    }
    return result;
}
ajb
  • 31,309
  • 3
  • 58
  • 84
DSATH
  • 23
  • 5
  • This code wouldn't produce any problem with the user console. Please provide a [mcve], tell us if there is an exception and how you run your code (which IDE/tool). – Tom Jun 25 '16 at 02:19
  • You know that you have to display (and use) the terminal window (ctrl+t)? – Tom Jun 25 '16 at 02:41
  • Updating your question based on the answer posted by @Stephen C and not saying anything else (whether the answer resolved the problem or not) is very bad manners. – Alex Jun 25 '16 at 03:52
  • @Alex In fact I did reply to Stephan and said, "Okay there was a bug but I wasn't able to test for that bug because the scanner wasn't allowing me to input." And I edited my post so as not to distract from the real problem at hand which is that the program is not letting me add an input with the scanner class. – DSATH Jun 25 '16 at 04:39
  • I am not a very experienced coder. I am not sure if importing acm.program.*; or any of the other acm's would mess with the Scanner. When I call the method in the main code, all that appears on the screen is the prompt that I inserted as an argument for my question method. Nothing else executes and I am not able to input anything – DSATH Jun 25 '16 at 05:06

1 Answers1

1

Am I using the Scanner properly?

That isn't the problem. The real problem is a straight-forward bug in your application logic. This condition:

  !(str.equals("yes") && str.equals("no"))

can never be false. A String cannot be both equal to "yes" AND equal to "no" at the same time. Therefore your while loop cannot terminate.


UPDATE

Following the edit, your code should more or less work. But this is not quite right.

while(!(str.equals("yes") || str.equals("no"))) {
    str = s.next();
    println("enter yes or no");
}

1) You are reading the next input token BEFORE you prompt for it.

2) You are not consuming the remaining characters after the first token of the line that the user just entered.

This is better

while(!(str.equals("yes") || str.equals("no"))) {
    s.nextLine();
    println("enter yes or no");
    str = s.next();
}

I suggest you go back and read the javadocs for the Scanner class carefully.

It is also possible that new Scanner(System.in) is wrong. That is normally the right thing to do, but your requirements might require you to read use input from some other input stream.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Okay there was a bug but I wasn't able to test for that bug because the scanner wasn't allowing me to input :( – DSATH Jun 25 '16 at 02:16
  • 2
    @DSATH You changed the `&&` in your original question. Is there still a problem? If so, please elaborate on what it is. As of now, since you've edited the post, but haven't said anything more about it, we can't tell what's going on. If the fix Stephen suggested made everything work, then do not edit your question like this. – ajb Jun 25 '16 at 02:46
  • @ajb I did change it. It was an error in the code that I was not able to check because of my not being able to initiate the code. The scanner class is not allowing me to provide an input to start the sequence in the code is the problem. – DSATH Jun 25 '16 at 04:44
  • @Stephen C All you have said are correct fixes to my code. Thank you. But I am not able to input anything to the variable "str" because the Scanner isn't working for some ridiculous reason. – DSATH Jun 25 '16 at 05:23
  • @DSATH - When I tweaked your code to work as a normal Java program (without the silly ACM nonsense) and applied the fixes I suggested ... it works for me. – Stephen C Jun 25 '16 at 05:37
  • @Stephen C That's what I was afraid of. I have to use them for the free online Standford course I am working through, though. :/ Oh well, atleast I know the paradigm is correct. Thanks Steph, and sorry if my editing my code was bad manners, didn't mean for it to come off like that. – DSATH Jun 25 '16 at 06:20
  • I think you need to check if you should be using `System.in`. – Stephen C Jun 25 '16 at 07:13
  • FIGURED IT OUT. I looked at the documentation for acm.program.*; and there is a method in it called "readLine()" which virtually has the same utility as Scanner. Everything works fine now! – DSATH Jun 25 '16 at 08:44
  • @DSATH I think editing your code is OK as long as you say something about it, like "I changed `||` to `&&` to fix the problem Stephen pointed out, but it's still not working because ... and ...". – ajb Jun 25 '16 at 15:41