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: Java code problem?

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java code problem?

    Hi all! My assignment question is this:

    Write a program that asks the user to input
    • The number of gallons of gas in the tank
    • The fuel efficiency in miles per gallon
    • The price of gas per gallon
    Then print the cost per 100 miles and how far the car can go with the gas in the tank.

    Thus far, I've written the code:

    public class assignmenti
    {
    public static void main ( String args[] )
    {
    Scanner keyboard = new Scanner(System.in);
    double gallons, milesPerGallon, pricePerGallon;

    //Get input
    System.out.println("Gallons in the tank?");
    gallons = keyboard.nextdouble();
    System.out.println("Fuel efficiency?");
    milesPerGallon = keyboard.nextdouble();
    System.out.println("Price of gas per gallon?");
    pricePerGallon = keyboard.nextdouble();

    // Print output
    System.out.println("Cost per 100 miles:" + ((100/milesPerGallon) * pricePerGallon) + "Dollars");
    System.out.println("How far the car can go:" + (gallons * milesPerGallon) + "miles");
    }
    }


    But it won't compile inside my terminal. Anyone see any detrimental errors here as to why my program isn't working?


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

    Default Re: Java code problem?

    it won't compile
    Please copy the full text of the error messages and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Location
    Ireland
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Java code problem?

    hi
    just making sure you imoported the needed class for your scanner.
    import java.util.*;
    this must be at the very top of your code.
    also keyboard.nextdouble();
    you must use capitals on the doubles
    keyboard.nextDouble();
    i fixed those 4 things and it now compiles
    hope this helps

  4. The Following User Says Thank You to dave_alan For This Useful Post:

    Scorks (January 30th, 2013)

  5. #4
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java code problem?

    Oh, didn't realize I wasn't capitalizing the doubles. I fixed that and it compiled for me too. Thanks a load!

    -Scorks

Similar Threads

  1. [SOLVED] New to JAVA, problem with first code, need to know how to fix.
    By hrenfrow in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2013, 11:17 AM
  2. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  3. Problem with Java code for Project
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 19th, 2011, 03:52 AM
  4. reformatting java code problem
    By Fordy252 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 23rd, 2010, 02:44 PM
  5. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM

Tags for this Thread