0

I having some trouble with an APCS assignment. The program is supposed to read strings with a length of two from a text file - test1.txt - and print out percentages of: a) girl-girl, boy-boy, boy-girl or girl-boy combinations, and b) the total number of individual groups.

I've been trying for an hour to figure this out! Although I'm suspicious of the String declaration in line 25, I don't have a way to confirm that. Furthermore, I'm worried that I messed up my if-else-if-else loop without prompting a compiler error.

The source code is attached for your reference. If you need any additional information, please don't hesitate to ask.

Since I'm a new user with a reputation < 10, please see the attached image:

http://imgur.com/7HpJF

For elaboration on what isn't working. I took a screenshot and wrote relevant comments on it!

/**
 * Family takes user input of sets of boys, girls, and boys + girls. Results are then
 * tabulated and displayed in a percentage form to the user. The total number of 
 * individuals are also displayed.
 * 
 * @E. Chu
 * @Alpha
 */

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class Family {

   public static void main (String[] args) throws IOException {

   int boyCount = 0;
   int girlCount = 0;
   double boyGroupCount = 0.0;
   double girlGroupCount = 0.0;
   int mixedGroupCount = 0;
   int totalPersonCount = 0;
   double totalGroupCount;
   String currentToken = "  ";
   Scanner inFile = new Scanner (new File ("test1.txt"));

   while (inFile.hasNextLine()) {
       currentToken = inFile.nextLine( );
       if (currentToken == "BG") {
          boyCount++;
          girlCount++; 
          mixedGroupCount++; }
       else if (currentToken == "GB") {
           boyCount++;
           girlCount++;
           mixedGroupCount++; } 
       else if (currentToken == "BB") {
          boyCount += 2; 
          boyGroupCount++; }
       else {
          girlCount += 2; 
          girlGroupCount++; } 
   }

   inFile.close();
   totalPersonCount = boyCount + girlCount;
   totalGroupCount = boyGroupCount + girlGroupCount + mixedGroupCount;
   System.out.println("Sample Size: " + totalPersonCount);
   System.out.println("Two Boys (%): " + boyGroupCount / totalGroupCount + "%");
   System.out.println("One Boy, One Girl (%): " + mixedGroupCount + "%");
   System.out.println("Two Girls (%): " + girlGroupCount / totalGroupCount + "%");

} // End of main method.

} // End of class Family.
Emre Erkan
  • 8,433
  • 3
  • 48
  • 53
Ernie Chu
  • 35
  • 6

2 Answers2

4

currentToken == "BB" should be currentToken.equals("BB")

Don't use == use the method equals instead

mabounassif
  • 2,311
  • 6
  • 29
  • 46
  • 1
    Thank you for explaining the correct convention in which to complete the issue instead of just giving the answer straight out. I can't believe I forgot about equals()! – Ernie Chu Nov 30 '11 at 05:16
2

Hint: you don't want to compare strings using ==, look into the equals method.

Mac
  • 14,615
  • 9
  • 62
  • 80