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 4 of 4

Thread: Very new to Java basic question

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Very new to Java basic question

    import java.util.Scanner;

    public class IfTest0
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner( System.in );

    //Prompt Hits

    System.out.print( "Enter number of hits > " );
    int hitAmount = scan.nextInt();
    System.out.println( "Number of hits is " + hitAmount );

    //Promt at bats

    System.out.print( "Enter number of at bats > " );
    int atBats = scan.nextInt();
    System.out.println( "Number of at bats is " + atBats );

    int Average;
    Average = (hitAmount/atBats);

    if ( Average>0.300 )
    {
    System.out.println( "Eligible for All-Stars " );
    }
    else
    {
    System.out.println ( "Not eligible for All-Stars " );
    }



    }
    }



    So I'm trying to use an if/else statement to state that when the batting average is greater than .300 the player is eligible for all stars. What happens is that when I input the hits and at bats it always says the player is not eligible. But when the average equals one the player is eligible which I find very strange but I can't figure out what is wrong with my code.

    Edit: I think I figured it out, I was using the / to divide which would give me a value of 0 I just need to figure out how to properly divide now I'm assuming.
    Last edited by loofy; July 23rd, 2011 at 05:41 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Very new to Java basic question

    Do you know about integer division? 4/5 = 0 and 10/8 = 1
    If you want fractional values you should use double variables.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Very new to Java basic question

    Quote Originally Posted by Norm View Post
    Do you know about integer division? 4/5 = 0 and 10/8 = 1
    If you want fractional values you should use double variables.
    That worked perfectly I changed my int's to doubles and now I'm getting the correct response.

    One more question on the other program I have:

    public class IfTest1
    {
    public static void main(String[] args)
    {
    double square;
    double rectangle;
    double SquareSide;
    double RectangleSide1;
    double RectangleSide2;

    SquareSide = 0.666666667;
    RectangleSide1 = 1/9;
    RectangleSide2 = 4;

    square = SquareSide*SquareSide;

    System.out.println ( "The area of the square is " + square);

    rectangle = RectangleSide1*RectangleSide2;

    System.out.println ( "The area of the rectangle is " + rectangle);

    This is where I assumed my problem was the division, I know that my 1/9 value is giving me 0. What do I need to do in order to get a decimal value?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Very new to Java basic question

    Change either the 1 or 9 to a double. For example: 1.0/9

Similar Threads

  1. please answer some basic question
    By togaurav in forum Java Theory & Questions
    Replies: 5
    Last Post: April 16th, 2011, 07:58 AM
  2. [???]basic question on frame window panel
    By Codeguess in forum AWT / Java Swing
    Replies: 1
    Last Post: January 12th, 2011, 08:03 AM
  3. Basic Operational Question on Tomcat/Servlets
    By dottore11 in forum Java Servlet
    Replies: 1
    Last Post: December 24th, 2010, 04:11 AM
  4. [SOLVED] Asking what I suspect to be a very basic question
    By Noobert in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 24th, 2010, 07:42 AM
  5. How to link two classes in java to use it method
    By Sterzerkmode in forum Object Oriented Programming
    Replies: 3
    Last Post: May 13th, 2009, 06:52 AM