Hi Im trying to write simple miles per gallon program, I have gotten every part so far, but I cannot figure out how to "calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point."

``````package random;
import java.util.Scanner;
public class random17 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
while (true) {
System.out.print("Enter Miles Driven: ");
float milesDriven = input.nextFloat();
if (milesDriven < 0) {
break;
}
System.out.print("Enter Gallons Used: ");
float gallonsUsed = input.nextFloat();
if (gallonsUsed < 0) {
System.out.println("*Invalid Input*");
break;
}
double mpg = (double)milesDriven / (double)gallonsUsed;
System.out.print("Miles Per Gallon: " + mpg + "\n");

float count = gallonsUsed/milesDriven;
float total = 0;
total += (float)mpg;
// test code //System.out.println(total);
float avgmpg = (float)total / count;
System.out.printf("Average MPG: " + avgmpg + "\n");
System.out.println("--------------------------------");
}
}
}``````

you need to somehow "remember" values from previous iterations of the while loop. so if you have a MAX number of iterations you could use an array, but perhaps it would be easiest to use a vector instead. you should also consider an exit clause to the loop.

``````public class Random17 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
List<Double> mpgs = new ArrayList<Double>();
final int MAXENTRY = 2;
int count = 0;

while (count < MAXENTRY) {

System.out.print("Enter Miles Driven: ");

float milesDriven = input.nextFloat();

if (milesDriven …``````

## All 7 Replies

you need to somehow "remember" values from previous iterations of the while loop. so if you have a MAX number of iterations you could use an array, but perhaps it would be easiest to use a vector instead. you should also consider an exit clause to the loop.

i was hoping that

``total += (float)mpg``

would do just this, but apparently different, I'm not familiar with vectors, arrays, or exit clauses, if you wouldn't mind shedding some light on a solution to my way or a vector or an exit clause, it would be very helpful, as am I very new to java.

``````public class Random17 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
List<Double> mpgs = new ArrayList<Double>();
final int MAXENTRY = 2;
int count = 0;

while (count < MAXENTRY) {

System.out.print("Enter Miles Driven: ");

float milesDriven = input.nextFloat();

if (milesDriven < 0) {

break;

}

System.out.print("Enter Gallons Used: ");

float gallonsUsed = input.nextFloat();

if (gallonsUsed < 0) {

System.out.println("*Invalid Input*");

break;

}

double mpg = (double) milesDriven / (double) gallonsUsed;

// add mpg to mpg list

System.out.print("Miles Per Gallon: " + mpg + "\n");
count++;
}

// calculate average mpgs for all

double avgmpg = calculateTotal(mpgs)/mpgs.size();

System.out.printf("Average MPG: " + avgmpg + "\n");

System.out.println("--------------------------------");

}

public static double calculateTotal(List<Double> mpgs){
double total = 0;
for(Double mpg : mpgs){
total += mpg;
}
}

}``````

MAXENTRY specifies the number of time to enter your input

To prevent the user for being ask for already entered inputs replace your input lines with this code block

``````float milesDriven = 0;

do{
System.out.print("Enter Miles Driven: ");
milesDriven = input.nextDouble();
}while(milesDriven <= 0);``````

do the same for the second input request; this prevents you from exiting the outer loop and having to ask for both input data again

Is there a way just to use Strings, doubles, ints and floats, and not lists? I'm now getting an error on

``mpgs.add(mpg);``
``````package JavaApplication1;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
double mpgs = 0.0;
final int MAXENTRY = 2;
int count = 0;

while (count < MAXENTRY) {

System.out.print("Enter Miles Driven: ");

float milesDriven = input.nextFloat();

if (milesDriven < 0) {

break;

}

System.out.print("Enter Gallons Used: ");

float gallonsUsed = input.nextFloat();

if (gallonsUsed < 0) {

System.out.println("*Invalid Input*");

break;

}

double mpg = (double) milesDriven / (double) gallonsUsed;

// add mpg to mpg list

System.out.print("Miles Per Gallon: " + mpg + "\n");
count++;
}

// calculate average mpgs for all

double avgmpg = calculateTotal(mpgs)/mpgs.size();

System.out.printf("Average MPG: " + avgmpg + "\n");

System.out.println("--------------------------------");

}

public static double calculateTotal(double mpgs){
double total = 0;
for(Double mpg : mpgs){
total += mpg;
}
}

}``````

look at my code properly its not `double mpgs` its `List<Double> mpgs` . The only way to get arround this with out using a list or an array is to do this

``````public class Random17 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
double total = 0;
final int MAXENTRY = 2;
int count = 0;

while (count < MAXENTRY) {

System.out.print("Enter Miles Driven: ");

float milesDriven = input.nextFloat();

if (milesDriven < 0) {

break;

}

System.out.print("Enter Gallons Used: ");

float gallonsUsed = input.nextFloat();

if (gallonsUsed < 0) {

System.out.println("*Invalid Input*");

break;

}

double mpg = (double) milesDriven / (double) gallonsUsed;

// add mpg to mpg list
total += mpg;

System.out.print("Miles Per Gallon: " + mpg + "\n");
count++;
}

// calculate average mpgs for all

double avgmpg = total/count;

System.out.printf("Average MPG: " + avgmpg + "\n");

System.out.println("--------------------------------");

}

}``````

ahh I forgot about the MAXENTRY thing, thanks! it works now

Be a part of the DaniWeb community

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