-2

I am trying to create an array that creates a new value if it does not match a previous entry. If the entry is the same as a previous one, the index in the second array will add 1.

for (j = 0; !input.equals("x"); j++ ) { // team loop
    System.out.println("Which team just won? (x to exit)"); 
    input = scnr.nextLine();

    for (i = 0; i < team.length - 1; i++) {
        if (team[i] == input) {
            score[j]++;
        }
        else if (team[i] != input) {
            continue;
        }
    }
} 
Rgwitcher
  • 1
  • 2
  • 1
    Don't use == to compare strings. What is the declaration of team? – Hongyu Wang Jul 12 '18 at 19:59
  • String [] team = new String [5]; – Rgwitcher Jul 12 '18 at 20:02
  • Sorry but what is your question? Can you provide example of how your code is supposed to work? Some input with expected actions/output/result could be helpful. – Pshemo Jul 12 '18 at 20:08
  • Which team just won? (x to exit) user input Sooners Which team just won? (x to exit) user input Sooners Which team just won? (x to exit) Cowboys Which team just won? (x to exit) Bears Which team just won? (x to exit) x – Rgwitcher Jul 12 '18 at 20:13
  • Based on `!input.equals("x");` you seem to be aware of how strings should be compared. Why are you using `==` then? If you are not aware of difference take a look at: [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Also why do you have `else` block (not to mention that since you are in `else` then you already know that `team[i] == input` wasn't true so checking `team[i] != input` makes no sense there)? – Pshemo Jul 12 '18 at 20:21

2 Answers2

1

Looks like a good start, but there are some issues that you should keep in mind later on.

  • First, You must use team[i].equals(input) to compare two strings.

  • Second, there is no need for your second conditional "else if" statement. If the first "if" statement doesn't catch the requirement, it will continue to the next iteration anyway.

  • Third, please pay attention to your code formatting. Your semicolons {} are not aligned in a few places, making it confusing to read.

To answer your question a bit, think about what you're trying to do. You want your "team" and "score" arrays to align with each other such that team[0] has their score stored at score[0]. The way you have it set up, you search for the index of the team that was inputted. Then, you increment score at index j (which is incremented by one each cycle of the loop).

Let's say team "apple" was at index 0 and team "banana" was at index 1.

If the input for round one was "banana", your loop would find that team "banana" is at index 1 in your inner for loop. Then, it would increment the score at j, which is 0. This means the final score would be "apple":1 and "banana":0 even though banana was the winner.

Hope that helps you with thinking through the problem a bit!

Zach
  • 156
  • 8
1
while(True){
  System.out.println("Which team just won? (x to exit)"); 
  input = scnr.nextLine();
  if(input.equals('x')) break;
  for(int i=0;i< team.length-1;i++){
     if (team[i].equals(input)) {
        score[i]++;
     }
  }
}

Above code assume you have two arrays Team and Score with each index matching to a Team has the score on the same index in the Score array.

In your code the Else if(team[i] != input) code is actually redundant as this is what actually a for loop does on itself if the IF matching fails it continues itself.

And secondly, the string matching should be done using .equal(), as == compares the object, not the actual string. For actually the for your team string comparison you should try lowercasing the input as a team xyz won't match to Xyz.

*And instead of having two arrays for a team and score, you can use Maps(hashmaps) in which you can store the team as key and score as the value and on each input check if key (name of that team exists) and update the value by 1 if it does. like

Foo score = map.get(teamName);
if(score){
  score++;  
}
abhishek kasana
  • 1,462
  • 12
  • 14