Jump to content

How can I do a while loop with user input? (Java)

Hi everyone, the problem I'm having is specifically with the while loop. I learned C++ before this, and that is why the loop is written that way

Example:


while (cin >> input) {...}

 

I know that the way I'm trying to use the scanner doesn't work, because it requires the user to input twice, but I'm not sure how to accomplish what I want. Any ideas?

 

 


import java.util.Scanner;
public class ScoreTester
{
 
  public static void main(String [] args)
  {
    int playerScore = 0;
    String word = "";
    Scanner in = new Scanner(System.in);
    WordScore player = new WordScore();
    System.out.println("Please enter each word; input '-1' when finished:");
    while (in.next() != "-1")
    {
      if (word != "" && word != "-1") {System.out.println(player.returnScore(word) + " points earned.");}
      playerScore += player.returnScore(word);
    }
    
    System.out.println("Total score for all words: " + playerScore);
    in.close();
  }
}

rubber dome apologist

Link to post
Share on other sites

Well, for the Java scanner you need to wrap it in an try/catch block, but I would make a boolean flag of "validData" or something like that, then I'd loop on if that true, then inside the loop at the end check again, i.e. word.compareToIgnoreCase("") == 0 || word.compareToIgnoreCase(-1) == 0. You can't use == for comparing Strings in Java, because EVERYTHING is an OBJECT (except the primitive types like, int, double, boolean, but there are Object wrappers for thos) and if you try to use == on an Object then it doesn't compare the value of the Object, it checks to see if they are two references pointing to the same spot on the heap. Therefore you have to have use either compareTo() or compareToIngoreCase() on the String object which returns a 0 if they are equal and some number if they are not, (I think it's -1 if the original word is "greater" and 1 if the word passed in the function is "greater", either way if it's not 0 it's not equal. The difference in the methods is pretty obvious, the first one would be false for word.compareTo(Word) and the second one would be true for the same comparison.

 

This should help explain what I mean a little better...

 

  public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        boolean validData = true;
        String word = "";

        while (validData) {
            System.out.print("Please enter a word, enter -1 when done: ");

            try {
                word = scanner.next();
            }catch (InputMismatchException ex){
                System.out.println(ex.getMessage());
            }

            if(word.compareToIgnoreCase( " ") == 0 || word.compareToIgnoreCase("-1") == 0){
                validData = false;
            }else{
                System.out.printf("Your word was %s%n", word);
            }

        }
}

Which gives this  on the console...

Please enter a word, enter -1 when done: bingo
Your word was bingo
Please enter a word, enter -1 when done: shirt
Your word was shirt
Please enter a word, enter -1 when done: -1

Process finished with exit code 0

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×