-1

I have a program that I am writing and I have it completed but I keep receiving an error in a counter that I am using. The purpose of the program is to build a plate stacker and I have finished that but need to write a counter based on the particular colors used (how many green plates...etc.). As I know several classmates use this site to cheat, I will not post the code in whole but only the fragments I feel are necessary. Below is an example of a statement that I am using to attempt to get a count of the beige plates in the text file. The statement is in a while loop with the other colors having the same coding. I also have initialized the counter (int beige = 0;) prior to this and outside of the while loop:

        if(line == "beige")
        {
            beige++;
        }

I have a System.out following these statements to display the count but it always comes up as 0 so I believe the problem is in this particular fragment. If I need to send additional coding I will but I believe it is something to do with this or the placement of it in the while loop.

DTRobertson94
  • 201
  • 1
  • 3
  • 13

1 Answers1

0

First, you are comparing the string in a wrong way. Use .equals method.

if (line.equals("beige"))
    beige++;

Second, you can use HashMap for this kind of problem where you keep updating the value(count) for key(color) and it will save you a lot of if conditions if say there are a hundred different colors!

Wajahat
  • 1,593
  • 3
  • 20
  • 47