Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Family
{
    public static void main(String [] args) throws IOException
    {
        Scanner inFile = new Scanner(new File("MaleFemaleInFamily.txt"));
        String token = "";
        int counter = 0;
        int girlAndBoy = 0;
        int twoBoys = 0;
        int twoGirls = 0;

        while(inFile.hasNext())
        {
            token = inFile.next();
            System.out.println(counter + "\t" + token + " ");
            if(token.equals("GB"))
            {
                girlAndBoy++;
            }
            else if(token.equals("BB"))
            {
                twoBoys++;
            }
            else if(token.equals("GG"))
            {
                twoGirls++;
            }
            counter++;
        }

        int gbPercent = (girlAndBoy * 100) / counter;
        int bbPercent = (twoBoys * 100) / counter;
        int ggPercent = (twoGirls * 100) / counter;

        System.out.println("\n");
        System.out.println("GB: " + girlAndBoy + " \\ " + gbPercent + "%");
        System.out.println("BB: " + twoBoys + " \\ " + bbPercent + "%");
        System.out.println("GG: " + twoGirls + " \\ " + ggPercent + "%");
        System.out.print("GB + " + "BB + " + "GG " + "= ");
        System.out.println(girlAndBoy + twoBoys + twoGirls);
        inFile.close();
    }
}


I wrote this code to find out how many times GB, BB, and GG appeared in a file.
I made the program count the total and the three individuals but then I get the individual count for each but they don't add to the total. What am I doing wrong?

Note: The source code is java.
Posted
Comments
PIEBALDconsult 26-Dec-14 21:22pm    
How about some sample data? Expected and actual results? Use Improve question to add context and detail.
CristianCruz503 26-Dec-14 22:29pm    
149999 GB <--*This the last number of the counter, its the total number.

The rest is the amount of times those variables appear in the file. However, they don't add to the total and the percentage does not add up to 100%, a quarter is missing.

GB: 37561 \ 25%
BB: 37621 \ 25%
GG: 37575 \ 25%
GB + BB + GG = 112757

I don't know what's wrong, I don't see any mistake in the code but if you can review it for me I would appreciate it! I'm only starting to code in java.
PIEBALDconsult 26-Dec-14 22:47pm    
Well, how many tokens are not one of those you test for?
CristianCruz503 26-Dec-14 23:18pm    
Well no... I used that variable to read the ".txt" file. I could send you the file if you don't mind. I mean I do since Im counting the tokens(GB, BB, GG).

Im not able to send you the exact file but here's a link. I'm hoping you can access it

C:\APCS Course Files\Module 5\Assignment\5.03 File Input\MaleFemaleInFamily.txt

1 solution

there may be other tokens which not matching with your if conditions check below code
Java
public static void main(String [] args) throws IOException
{
    Scanner inFile = new Scanner(new File("MaleFemaleInFamily.txt"));
    String token = "";
    int counter = 0;
    int girlAndBoy = 0;
    int twoBoys = 0;
    int twoGirls = 0;
    int other = 0;

    while(inFile.hasNext())
    {
        token = inFile.next();
        System.out.println(counter + "\t" + token + " ");
        if(token.equals("GB"))
        {
            girlAndBoy++;
        }
        else if(token.equals("BB"))
        {
            twoBoys++;
        }
        else if(token.equals("GG"))
        {
            twoGirls++;
        }else
        {
            other++;
        }
        counter++;
    }

    int gbPercent = (girlAndBoy * 100) / counter;
    int bbPercent = (twoBoys * 100) / counter;
    int ggPercent = (twoGirls * 100) / counter;
    int otherPercent = (other * 100) / counter;

    System.out.println("\n");
    System.out.println("GB: " + girlAndBoy + " \\ " + gbPercent + "%");
    System.out.println("BB: " + twoBoys + " \\ " + bbPercent + "%");
    System.out.println("GG: " + twoGirls + " \\ " + ggPercent + "%");
    System.out.println("Other: " + other + " \\ " + otherPercent + "%");
    System.out.print("GB + " + "BB + " + "GG " + "OTHER" + "= ");
    System.out.println(girlAndBoy + twoBoys + twoGirls +other);
    inFile.close();
}
 
Share this answer
 
Comments
CristianCruz503 26-Dec-14 23:05pm    
Im not able to send you the exact file but here's a link. I'm hoping you canaccess it. Its the "(new File("MaleFemaleInFamily.txt"));"

C:\APCS Course Files\Module 5\Assignment\5.03 File Input\MaleFemaleInFamily.txt
DamithSL 26-Dec-14 23:12pm    
have you run and test my code above?
what are the results?
CristianCruz503 26-Dec-14 23:30pm    
There are only "GBBBGG" in the file. Its a class assisgnment and we are on christmas break so my teacher cant help :/. There arent any other tokens than those 3.
DamithSL 27-Dec-14 0:14am    
what is the percentage you get for other?
CristianCruz503 27-Dec-14 9:46am    
149999 GB


GB: 37561 \ 25%
BB: 37621 \ 25%
GG: 37575 \ 25%
Other: 37243 \ 24%
GB + BB + GG OTHER= 150000

That's what I got. However, the file only consists of GBBBGGs, there isn't any other stuff. That's whats confusing me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900