Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: New to the complmunity, need some assistance please.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to the complmunity, need some assistance please.

    Okay, first off, hello to all. I am a mechanical engr. student, and am in a intro Java class. This is my first experience with programming of any sort, so I really have no idea what I am doing. Alot of monkey see, monkey do right now. I have a program, coin toss, which I am sure alot of you have seen. I have the bulk of the program functional, just the final portion, where it tallies the heads and tails, and calculates a percentage for both. I have copied and pasted, (the attach file wasn't working for me) what I have got so far, can someone please take a look and tell me (in a very slow, childlike manner) what I am doing wrong? i have highlighted in red what I think the problem areas are, but I can't figure out how to fix it. Thank you.

    Nick the New Guy.



    import java.util.Scanner;
    public class CoinToss
    {
    public static void main (String [] args)
    {
    int headCount = 0;
    int tailCount = 0;
    float percentHeads = (headCount++) / 8 * 100;
    float percentTails = (tailCount++) / 8 * 100;
    char tossResult;
    System.out.println ("Flip a coin a total of eight times.");
    System.out.println ("After each flip, enter the value of the flip,");
    System.out.println ("use 'h' for heads,");
    System.out.println ("and use 't' for tails.");
    System.out.println ("The program will keep track of the number of 'Heads',");
    System.out.println ("the number of 'Tails', and the percentages of each.");
    System.out.println ("What is the first value?");
    Scanner keyboard = new Scanner (System.in);
    tossResult = keyboard.next().charAt(0);
    if(tossResult == 'h');
    headCount++;
    if(tossResult == 't');
    tailCount++;
    System.out.println ("What is the second value?");
    tossResult = keyboard.next().charAt(0);
    if(tossResult == 'h');
    headCount++;
    if(tossResult == 't');
    tailCount++;
    System.out.println ("What is the third value?");
    tossResult = keyboard.next().charAt(0);
    if(tossResult == 'h');
    headCount++;
    if(tossResult == 't');
    tailCount++;
    System.out.println ("What is the fourth value?");
    tossResult = keyboard.next().charAt(0);
    if(tossResult == 'h');
    headCount++;
    if(tossResult == 't');
    tailCount++;
    System.out.println ("What is the fifth value?");
    tossResult = keyboard.next().charAt(0);
    if(tossResult == 'h');
    headCount++;
    if(tossResult == 't');
    tailCount++;
    System.out.println ("What is the sixth value?");
    tossResult = keyboard.next().charAt(0);
    if(tossResult == 'h');
    headCount++;
    if(tossResult == 't');
    tailCount++;
    System.out.println ("What is the seventh value?");
    tossResult = keyboard.next().charAt(0);
    if(tossResult == 'h');
    headCount++;
    if(tossResult == 't');
    tailCount++;
    System.out.println ("What is the eigth value?");
    tossResult = keyboard.next().charAt(0);
    if(tossResult == 'h');
    headCount++;
    if(tossResult == 't');
    tailCount++;
    System.out.println ("The total amount of 'Heads' entered is:");
    System.out.println ("headCount + " headCount++");
    System.out.println ("The total amount of 'Tails' entered is:");
    System.out.println ("tailCount + " tailCount++");
    System.out.println ("The percentage of 'Heads' is:");
    System.out.println ("percentHeads + percentHeads");
    System.out.println ("The percentage of 'Tails' is:");
    System.out.println ("percentTails");


    }
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: New to the complmunity, need some assistance please.

    System.out.println ("headCount + " headCount++");
    System.out.println ("tailCount + " tailCount++");
    That will confuse it as you have an unclosed pair of " ".

    I'm not sure if you want the "+" in the quotes or not.

    Also, percent heads isn't being updated every time you increment percent heads. Same for percent tails.

    Also, maybe you initialize percent heads and percent tails after you've got your final head and tail count.

    Right now it's going to be the same value every time no matter what is chosen.

    float percentHeads = (headCount++) / 8 * 100;
    float percentTails = (tailCount++) / 8 * 100;

    Since that appears to be the postincrement operator, I think it applies it to the original value and then increments. Also, not quite sure on this one, but if Java evaluates expressions in the normal mathematical precedence order, which I think it does, it'll be doing this:

    (headCount) / (8*100)

    0/800 = 0;
    Last edited by javapenguin; October 6th, 2011 at 04:35 PM.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to the complmunity, need some assistance please.

    When your printing out values such as headCount and percentHeads you dont need quotation marks.

    You just need the value in the brackets such as:

    System.out.println ("The total amount of 'Heads' entered is:");
    System.out.println (headCount); <--------- just the value headCount, no need for the quotes.

    alternatively if you wanted it on one line you could print it out by doing this:

    System.out.println ("The total amount of 'Heads' entered is: " +headCount);


    But for now try this for the code youve highlighted in red at the bottom:

    System.out.println ("The total amount of 'Heads' entered is:");
    System.out.println (headCount);
    System.out.println ("The total amount of 'Tails' entered is:");
    System.out.println (tailCount);
    System.out.println ("The percentage of 'Heads' is:");
    System.out.println (percentHeads);
    System.out.println ("The percentage of 'Tails' is:");
    System.out.println (percentTails);

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to the complmunity, need some assistance please.

    Also where you calculate the percentage here:

    float percentHeads = (headCount++) / 8 * 100;
    float percentTails = (tailCount++) / 8 * 100;

    you dont want to divide headcount++ and tailCount++ , these are not the real values you want to work with, headCount++ adds 1 to headCount and gives headCount the new value, therefore you want to use headCount and tailCount for your percentage calculations. ie. this:

    float percentHeads = (headCount) / 8 * 100;
    float percentTails = (tailCount) / 8 * 100;

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to the complmunity, need some assistance please.

    Hmmmmmmmm something is still not right it gives the following results

    The total amount of 'Heads' entered is:
    8
    The total amount of 'Tails' entered is:
    8
    The percentage of 'Heads' is:
    0
    The percentage of 'Tails' is:
    0

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: New to the complmunity, need some assistance please.

    Also, you don't always have to use a String per se for the println.

    System.out.println("The amount of 'Heads' entered is: " + headcount);

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: New to the complmunity, need some assistance please.

    First, please use the code tags to wrap your code in...it makes it readable and easy to look over.

    Second, while other members seem to have the ability to guess quite efficiently, myself...I have no idea what the problem is because you haven't told me. Does this compile? Does it misbehave? Are there exceptions? The more information the better so we don't guess as to what the problem is (and in the process, waste time not solving the underlying problem)...the above posts exemplify this, as one post suggests the problem is compile time, while another a runtime problem.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: New to the complmunity, need some assistance please.

    Also, don't have ; after your if statements. That is basically causing your if to execute an empty statement and then always will therefore increment headcount and tailcount. That's why it's always 8 for both.

    ";" is actually a valid statement, albeit an empty one.

  9. #9
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to the complmunity, need some assistance please.

    Yes, also like the poster above said, the calculations are doing 8*100 first.

    Maybe you should try changing the maths around and do: (headcount) * 100 / 8

    That should give you the correct result as your basically multiplying the count by 12.5 which is an 1/8 of 100

  10. #10
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to the complmunity, need some assistance please.

    great stuff guys, I appreciate it. Here's where I am now...

    ;'s deleted from if statments
    upon compiling, headcount and tailcount are functional.
    tried changing the math around for the percentage, still coming up with 0.0 for both heads and tails.

    import java.util.Scanner;
    public class CoinToss
    {
    	public static void main (String [] args)
    	{
    		int headCount = 0;
    		int tailCount = 0;
    		float percentHeads = (headCount) * 100 / 8;
    		float percentTails = (tailCount) * 100 / 8;
    		char tossResult;
    		System.out.println ("Flip a coin a total of eight times.");
    		System.out.println ("After each flip, enter the value of the flip,");
    		System.out.println ("use 'h' for heads,");
    		System.out.println ("and use 't' for tails.");
    		System.out.println ("The program will keep track of the number of 'Heads',");
    		System.out.println ("the number of 'Tails', and the percentages of each.");
    		System.out.println ("What is the first value?");
    		Scanner keyboard = new Scanner (System.in);
    		tossResult = keyboard.next().charAt(0);
    		if(tossResult == 'h')
    			headCount++;
    		if(tossResult == 't')
    			tailCount++;
    		System.out.println ("What is the second value?");
    		tossResult = keyboard.next().charAt(0);
    		if(tossResult == 'h')
    			headCount++;
    		if(tossResult == 't')
    			tailCount++;
    		System.out.println ("What is the third value?");
    		tossResult = keyboard.next().charAt(0);
    		if(tossResult == 'h')
    			headCount++;
    		if(tossResult == 't')
    			tailCount++;
    		System.out.println ("What is the fourth value?");
    		tossResult = keyboard.next().charAt(0);
    		if(tossResult == 'h')
    			headCount++;
    		if(tossResult == 't')
    			tailCount++;
    		System.out.println ("What is the fifth value?");
    		tossResult = keyboard.next().charAt(0);
    		if(tossResult == 'h')
    			headCount++;
    		if(tossResult == 't')
    			tailCount++;
    		System.out.println ("What is the sixth value?");
    		tossResult = keyboard.next().charAt(0);
    		if(tossResult == 'h')
    			headCount++;
    		if(tossResult == 't')
    			tailCount++;
    		System.out.println ("What is the seventh value?");
    		tossResult = keyboard.next().charAt(0);
    		if(tossResult == 'h')
    			headCount++;
    		if(tossResult == 't')
    			tailCount++;
    		System.out.println ("What is the eigth value?");
    		tossResult = keyboard.next().charAt(0);
    		if(tossResult == 'h')
    			headCount++;
    		if(tossResult == 't')
    			tailCount++;
    		System.out.println ("The total amount of 'Heads' entered is:");
    		System.out.println (headCount);
    		System.out.println ("The total amount of 'Tails' entered is:");
    		System.out.println (tailCount);
    		System.out.println ("The percentage of 'Heads' is:");
    		System.out.println (percentHeads);
    		System.out.println ("The percentage of 'Tails' is:");
    		System.out.println (percentTails);
     
    	}
    }

  11. #11
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to the complmunity, need some assistance please.

    Got it, the float percentHeads / Tails is a postincrement operator. I didn't realize where you initialize the operator made a difference. Thanks so much for the help. Assignment is past due already so no credit, but atleast I learned something.

  12. #12
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New to the complmunity, need some assistance please.

    How did you sort this?

    Im trying to learn aswell.

  13. #13
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to the complmunity, need some assistance please.

    Quote Originally Posted by djl1990 View Post
    How did you sort this?

    Im trying to learn aswell.
    I don't want to put up the finished code, as this is a common introduction class code (I think)because I don't know if it violates any home assistance policies help by the website. Admins, please let me know if this is the case or not. The reason the averaging wasn't working was because I initialized the that operator (float percentHeads and float percentTails) before the increments for the averages were established. Simply moving them down to after the user inputs fixed that. I also had thrown in some random quote signs in the system.out.println for the headCount and tailCount that were confusing it.

    I know I am being abstract but like I said, this site is a great resource, I learned more in 8 responses here than I did in two weeks in class, and I don't want to jeopardize my membership. Are you having a specific problem with a similar program or just trying to learn in general? If its specific, throw it up in this thread, don't worry about thread jacking, you'll get you're answer. I worked for two weeks on my own, came here, got enough info to figure out my answer in 2 hours.

Similar Threads

  1. AI Search Simulator Assistance
    By GeekWarth in forum What's Wrong With My Code?
    Replies: 12
    Last Post: September 17th, 2011, 03:40 PM
  2. NEED ASSISTANCE W/ JAVA PROGRAM
    By Neddrick in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 29th, 2010, 08:20 PM
  3. array program assistance needed
    By JavaNoob82 in forum Collections and Generics
    Replies: 4
    Last Post: December 14th, 2009, 05:49 AM
  4. Replies: 5
    Last Post: December 13th, 2009, 07:10 PM
  5. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 01:12 AM