do-while loop not working as it should

11,320

Solution 1

You are storing user info in stdInput but your while checks stdin. Try this way

String stdInput = null;

do {
    stdInput = stdin.next();

    //your ifs....

} while (!stdInput.equalsIgnoreCase("xxxxx"));

Solution 2

This Works:)

I just submitted this code to codelab and it works just fine.

Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters ) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line.

int land = 0;
int air = 0;
int water = 0;
String word = "";
while(!(word.equals("xxxxx"))) {
 word = stdin.next();
if(word.equals("land")) {
    land++;
}else if(word.equals("air")) {
    air++;
}else if(word.equals("water")) {
    water++;
} 
}
System.out.println("land:" + land);
System.out.println("air:" + air);
System.out.println("water:" + water);
Share:
11,320
Dragon Wolf
Author by

Dragon Wolf

Double Major for Computer Science and Cognitive Science (w/ specialization in computation or human comp. interaction haven't specialized just yet). I love programming, but have not programmed in over 15 years! Last time I did this was VB and AOL2.5-3.0 days!

Updated on June 13, 2022

Comments

  • Dragon Wolf
    Dragon Wolf almost 2 years

    Ok so basically I am having trouble finding out why this is not working as I think it should, and need help getting to the right output. I have tried messing with this format a few ways, but nothing works, and I really don't understand why. Here are the instructions, followed by my source for it:

    INSTRUCTIONS

    Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line.

    ASSUME the availability of a variable, stdin , that references a Scanner object associated with standard input.

    SOURCE:

    int land = 0;
    int air = 0;
    int water = 0;
    
    do
    {
         String stdInput = stdin.next();
            if (stdInput.equalsIgnoreCase("land"))
            {
                land++;
            }else if (stdInput.equalsIgnoreCase("air"))
            {
                air++;
            }else if (stdInput.equalsIgnoreCase("water"))
            {
                water++;
            }
    }while (stdin.equalsIgnoreCase("xxxxx") == false); // I think the issue is here, I just dont't know why it doesn't work this way
    System.out.println("land: " + land);
    System.out.println("air: " + air);
    System.out.println("water: " + water);
    
  • Dragon Wolf
    Dragon Wolf about 11 years
    AHHH and I see that the ! goes BEFORE the boolean expression for the string as well, don't recall our teacher mentioning THAT to us! I see how that would work though! Ill try these out brb...
  • Bohemian
    Bohemian about 11 years
    It's good coding practice to never compare a boolean with a boolean constant (it just adds more code for no value). Instead always use the boolean by itself, or put a ! before it for the negative.
  • Dragon Wolf
    Dragon Wolf about 11 years
    SO TRUE LOL! OMG I hate myself but that kind of double checking is all part of the game, thanks for pointing that out! FYI got it working as intended now!
  • Dragon Wolf
    Dragon Wolf about 11 years
    Ya that really helped, didn't even notice my original boolean was wrong to begin with! Got everything squared away now thanks for the help
  • Pshemo
    Pshemo about 11 years
    @DragonWolf it this answer solves your problem you can [accept it] to mark this problem is solved :)