I just started learning Java and I have been playing around with it. I am trying to make this program return a double value like 1000/3 = 333.33333..... but it returns 333.000 what am I doing wrong in Java. It works in C though :P. Thanks in Advance!

//Exercise : 4.17
//Program that inputs miles driven and gallons of gas
//Program calcualates and displays: miles per gallon and print out sum of miles per gallon/no of trips
//ie. Will use float for all average results
//miles and gallons should be in integers

import java.util.Scanner;//Program will use Scanner class

public class Mileage
{

    //Main method that executes the java program
    public static void main ( String[] args )
    {
        //Declaring variables to be used in program
        int miles = 0;
        int gallons = 0;
        double miles_per_gallon = 0;
        double miles_per_gallon_trips = 0;
        int sum_miles = 0;
        int sum_gallons = 0;


        //Creating a Scanner object to get input in command window
        Scanner input = new Scanner ( System.in );

        while (miles != -1 )
        {
           //Promping User to get input
           System.out.print("\nEnter the number of miles (-1 to exit): ");
           miles = input.nextInt();

           if ( miles != -1){
            sum_miles += miles;//Summing up miles to be used to calculate total average
           //Prompt User to enter the number of gallons 
           System.out.print("\nEnter the number of gallons of gas used: ");
           gallons = input.nextInt();
           sum_gallons += gallons;//Summing up gallons to use to calculate total average

           miles_per_gallon = miles / gallons;//Calculate the avarage of miles per gallon

         System.out.printf("\nMiles per gallon: %f\n",miles_per_gallon); //Print out the 
       }

           else
            miles = -1;

        }

         if ( miles_per_gallon > 0.0){
            miles_per_gallon_trips = (sum_miles / sum_gallons);
       System.out.printf("Total miles Per Gallon is %f\n",miles_per_gallon_trips);}

    }//end main method
}//end class Mileage

miles_per_gallon = miles / gallons;

Because miles and gallons are both ints this division is done in int arithmetic. After that, the result is converted to double to match the target variable miles_per_gallon, but by then it's too late.

Thanks!

Can you tell me if there are some bad programming practices in my code I would really apreciate it. So I can change them now before they become a problem when I am looking for work. Am I over commenting?(I want my code to be so simple that even a kid can get it)

In my opinion it's reasonably good code. So although there are quite a few points raised in this post, don't be discouraged.
The variables names are very clear, so it doesn't need a lot of comments. Many of the comments you do have explain what you are trying to achieve - excellent.
The unneccessary coments are lines like 12, 15, 24. Anyone reading this code should know enough Java to understand public static void main, or new Scanner(System.in)
The else clause on 45/46 is redundant.
Java naming conventions are to use "camel case" not underlines for variable names, eg milesPerGallon
You should be very careful to keep you indentation and brackets lined up - errors in structure are very hard to see otherwize. A programmer's editor is useful here.
YOu may want to deal with the user entering 0 for the gallons - right now that will trigger a horrible crash.
I don't know why you have the if test online 50.

Thanks +JamesCherrill for the great advice. In line 45/46, I put the else test for the program to break because without it the program tend to keep going as if I put
else continue;In line 50 I didn't want to print the value of miles_per_gallon_trips after the user had enterd miles as -1. Will try with 0. And again thanks for the advice will work on it.

Hey would you have preffered me to use end-of-file indicator to help the user indicate that he/she is done putting input

What you did was a standard way of doing this.
Personally I prefer this pattern, which has only one read of user input

while (true) {
   get user input
   if (user input is end-of-file value) break:

   process normal user input
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.