1

So I am trying to take basic input values like a or b between two different user inputs, match them, and print the score for that one loop, then also print the accumulative total score. I feel like I am doing it right but every time my score prints out the value of 0. So lets say the key values are a a a and the and values are a b a. That should print out the value 2, and continually run, also resetting the score back to 0.

    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader input1 = new BufferedReader(reader);
    BufferedReader input2 = new BufferedReader(reader);

    String inputValue;
    String[] key;
    String[] ans;
    int totalScore = 0;

    inputValue = input1.readLine();
    key = inputValue.split("\\s+");

    while(true){

       int score = 0;

       inputValue = input2.readLine();
       ans = inputValue.split("\\s+");

       if(key[0] == ans[0])
          score += 1;
       if(key[1] == ans[1])
          score += 1;
       if(key[2] == ans[2])
          score += 1;

       totalScore = score;

       System.out.print(score + "\n");
       System.out.print(totalScore + "\n");
  }
Hello World
  • 25
  • 1
  • 7

1 Answers1

1

You're comparing strings; use .equals() instead of ==

example:

if (key[0].equals(ans[0])) {}

Remember: (almost) Always use .equals() for string comparison.

Aify
  • 3,543
  • 3
  • 24
  • 43