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: Simple java program help please.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Simple java program help please.

    package firstproject;
    import java.util.Scanner;
    import java.text.NumberFormat;

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

    //create scanner object
    Scanner sc = new Scanner(System.in);

    String repeat = "yes";
    while(repeat.equalsIgnoreCase("yes")){

    //Display Welcome massage
    System.out.println("Welcome to MPG Calculator ");
    System.out.println("");

    System.out.print("What is your Name: ");
    String name = sc.nextLine();

    System.out.print("What is your Car Model: ");
    String model = sc.nextLine();

    System.out.print("What is your Car Make: ");
    String make = sc.nextLine();

    System.out.print("What is your starting Odometer reader: ");
    double startingMileage = sc.nextDouble();

    System.out.print("What is your ending odometer reader: ");
    double endingMileage = sc.nextDouble();

    System.out.print("What is your Starting Gallons of Gas in the Tank:");
    double startingGallons = sc.nextDouble();

    System.out.print("What is your Ending Gallons of Gas in the Tank:");
    double endingGallons = sc.nextDouble();


    //Calculate mileage traveled
    double mileageTraveled = endingMileage - startingMileage;

    //calculate gallons of gas
    double gasUsed = startingGallons - endingGallons;

    //calculate Mileage per gallon
    double mpg = mileageTraveled / gasUsed;

    //price of a gas of gallon
    final double GAS_PRICE = 3.85;

    //calculate travel Cost
    double gasCostPerGallon = GAS_PRICE * gasUsed;

    String rate = "";

    //if else statement
    if (mpg <= 20){
    rate = "Gas Guzzler";
    }
    else if (mpg <= 30 && mpg >= 20){
    rate = "Good";
    }
    else if (mpg > 30){
    rate = "Amazing";
    }

    //Create currency formatting object
    NumberFormat cf = NumberFormat.getCurrencyInstance();

    NumberFormat df = NumberFormat.getNumberInstance();

    //display summary
    System.out.println("Welcome to the MPG Calculator!");
    System.out.println("");
    System.out.println("Customer Name: " + name );
    System.out.println("Car Model: " + model );
    System.out.println("Car Make: " + make);
    System.out.println("Starting Odometer Reading: " + startingMileage);
    System.out.println("Ending Odometer Reading: " + endingMileage);
    System.out.println("Miles Traveled: " + mileageTraveled);
    System.out.println("Gallons of Gas Used: " + gasUsed);
    System.out.println("Cost of Gas per Gallon: " + cf.format(gasCostPerGallon));
    System.out.println("Mile Per Gallon Usage (MPG): " + df.format(mpg));
    System.out.println("Car MPG rating is: " + rate);
    System.out.println("");

    //ask if they want to continue
    System.out.print("Do you want to continue? (Yes/No)");
    System.out.println("");

    repeat = sc.next();


    }

    System.out.println("Thank you for using Mileage App!");
    sc.close();
    }

    }



    Ok my problem is when say yes to continue, it prints out:
    Welcome to MPG Calculator

    What is your Name: What is your Car Model:


    instead of just what is your name

    please tell me what is wrong with my code thank you


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Simple java program help please.

    After using the last sc.next() there is still some data left in the buffer. You need to clear the buffer after calling next()

  3. The Following User Says Thank You to Parranoia For This Useful Post:

    Arg (April 28th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple java program help please.

    Thank you but can you tell me how?

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple java program help please.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly. You can edit your post and fix it.

Similar Threads

  1. simple java program
    By sravan18 in forum JavaServer Pages: JSP & JSTL
    Replies: 6
    Last Post: August 17th, 2013, 03:56 AM
  2. how to do this simple java program?
    By avistein in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 9th, 2013, 12:22 PM
  3. Simple Java Program
    By Robbiep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 24th, 2011, 07:27 AM
  4. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  5. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM