0

I am trying to get my code to repeat, and I try to enter "while" but it says:

illegal start of expression.

Any help would be appreciated as to where to enter this.

import java.util.Scanner;

public class GasMileage
{
    public static void main(String[ ] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("How many miles were driven?");
        int miles;
        miles = keyboard.nextInt();

        System.out.print("How many gallons were used?");
        int gallons;
        gallons = keyboard.nextInt();

        int mpg;
        mpg = miles / gallons;

        System.out.println(" The Miles-Per-Gallon used in this trip are " + mpg );
    }
}
trincot
  • 317,000
  • 35
  • 244
  • 286
john
  • 31
  • 5
  • 3
    Welcome on StackOverflow! I edited your question to add the most important tag: Java. I also indented the code you provided to improve readability. Please add the line of code with the `while` so we can see what you were trying to do. – trincot Feb 21 '16 at 19:17

2 Answers2

2

Example of what you might be trying to do (details below)

import java.util.Scanner;

public class GasMileage
{
    public static void main(String[ ] args)
    {
        Scanner keyboard = new Scanner(System.in);
        boolean stillInLoop = true;

        while (stillInLoop)
        {
            System.out.print("How many miles were driven? ");
            int miles;
            miles = keyboard.nextInt();

            System.out.print("How many gallons were used? ");
            int gallons;
            gallons = keyboard.nextInt();

            int mpg;
            mpg = miles / gallons;

            System.out.println(" The Miles-Per-Gallon used in this trip are " + mpg);

            stillInLoop = false;
        }
    }
}

A few tips:

1) If you use a while condition, you are running that loop until something no longer remains true, in which case java will automatically exit the loop. These conditions are either true or false (boolean). I notice that you didn't specify what you'd like in your while loop. It would really help if you could provide us with more information as to what else you're trying to do.

2) Be sure to include the space in between your question and quotation marks in your print statements or your output will get bunched up like this:

Bad practice:

System.out.print("How many miles were driven?");

Output: How many gallons were used?5

Good practice:

System.out.print("How many miles were driven? ");

Output: How many gallons were used? 5

Notice the spacing.

3) The code I gave you might seem vague but it's because I didn't have a condition to work with specifically. Figure out which section(s) of your code need to continue running until a certain condition has been met. I called the boolean variable "stillInLoop" in my example but generally this is extremely unhelpful to anyone reading your code and you'd probably be better off naming it something more helpful.

Hopefully this helped out a bit. Good luck!

  • Thank you, I did notice the improper spacing and corrected that prior. Your answer was very helpful, thanks again. – john Feb 22 '16 at 21:52
0

You probably didn't use the right syntax with your while loop, it should look something like this:

//in your main method
while(true){
   //here you ask the user questions
}
//end of your program

So when you fill this in your program you get:

public class GasMileage{
   public static void main(String[ ] args){

      Scanner keyboard = new Scanner(System.in);

      while(true){
      System.out.print("How many miles were driven?");
      int miles;
      miles = keyboard.nextInt();

      System.out.print("How many gallons were used?");
      int gallons;
      gallons = keyboard.nextInt();

      int mpg;
      mpg = miles / gallons;

      System.out.println(" The Miles-Per-Gallon used in this trip are " + mpg );
   }  
}

This will loop your program until you force it to stop, to let the program stop otherwise you will need to change the 'true' in the while loop to something which is triggered by something in your code

Andritchi Alexei
  • 1,380
  • 1
  • 16
  • 23