简体   繁体   中英

Why does my program return zeros only?

I am working on this code for a summer course so I don't have to take the introductory technology course at school.

public class Family {
   public static void main(String[] args) throws IOException {
      String token = "";
      int counter = 0, boyBoy = 0, boyGirl = 0, girlGirl = 0;
      double ratioBB = 0.0, ratioBG = 0.0, ratioGG = 0.0;

      Scanner inFile = new Scanner(new File("MaleFemale.txt"));
      DecimalFormat df = new DecimalFormat("#.##");

      while (inFile.hasNextLine()) {
         counter++;
         token = inFile.nextLine( );
              if(token.equals("BB")) boyBoy++;
         else if(token.equals("BG") || token.equals("GB")) boyGirl++;
         else girlGirl++;
      }

      ratioBB = (boyBoy / counter) * 100;
      ratioBG = (boyGirl / counter) * 100;
      ratioGG = (girlGirl / counter) * 100;
      df.format(ratioBB);
      df.format(ratioBG);
      df.format(ratioGG);

      System.out.println("Sample Size: " + counter);
      System.out.println("Two Boys: " + ratioBB);
      System.out.println("Boy and Girl: " + ratioBG);
      System.out.println("Two Girls: " + ratioGG);
    }
}

However, whenever I run the program with the accompanying text file (see spoiler below), the last three print statements always return zero. What is going on here?

Here is a sample of the file being read in:

MaleFemale.txt

GB
BG
BG
GG
GB
GB
GB
GB
BG
BG
GB
GG
BG
BG
GG
....

The real text file is 150000 of these types of entries, would the amount of data have anything to do with this? Thank you so much.

You are performing integer division in Java, and that must result in another integer. So, unless they are all boys or all girls, "anything less than counter" divided by "counter" yields 0 .

Cast one of them to double to force floating-point arithmetic.

ratioBB = ((double) boyBoy / counter) * 100;

You can do the same for the other divisions in the lines following that line.

You are also ignoring the String output of df.format each time you use it. Use the return value; it doesn't change the number inputted into it.

System.out.println("Two Boys: " + df.format(ratioBB));  // likewise for more lines

Your problem lies here:

int boyBoy = 0, boyGirl = 0, girlGirl = 0, counter = 0;

ratioBB = (boyBoy / counter) * 100;
ratioBG = (boyGirl / counter) * 100;
ratioGG = (girlGirl / counter) * 100;

Since all the variables involved are integers, the comptuation of (boyBoy / counter) will return an integer, rounded down . Because boyBoy / counter is guaranteed to be less than 1, it rounds down to 0.

Instead, try casting the integers to doubles before dividing, as these will return the actual ratio. Then, instead of multiplying by 100 , multiply by 100.0 , as multiplication by 100 will also cast to an integer.

ratioBB = ((double) boyBoy / counter) * 100.0;

The first expression is cast to a double , and the multiplication also casts to a double . The other way around, you'll run into this problem.

These are called implicit casts, and over time, you'll learn to watch out for them.

When you divide an int by an int (boyBoy / counter) the result is also an int. If the result < 1, it will be truncated to 0.

You can fix it by casting to double before the division : ((double)boyBoy / counter) .

Apart from the integer division already mentioned in the other answers, your scanning loop is wrong. Your single tokens seem to be separated by a space and there are no line breaks, but you're reading full lines with inFile.hasNextLine() and inFile.nextLine() . So inFile.nextLine() will return the entire file content!

Instead, use inFile.hasNext() and inFile.next() to read single tokens only:

while (inFile.hasNext()) {
    counter++;
    String token = inFile.next( );
    if (token.equals("BB")) boyBoy++;
    else if (token.equals("BG") || token.equals("GB")) boyGirl++;
    else girlGirl++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM