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 2 of 2

Thread: How to make this code even simpler

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb How to make this code even simpler

    What im trying to do: Trying to make a parking ticket machine that will check if the user is a member if the user is they will be ticketed 2.00 if not 3.00 with a discount of 2.50 after 12hrs, also the machine should round the number of hour Partial hours are rounded up to the next hour. Also trying to add data validation for time parked exp user cant enter 0 hours or a negative hr. I cant get my script to add 2 decimal places it comes out as 1 decimal place.

      //Import Scanner 
    import java.util.Scanner;
    public class Undergroundparking2 {
     public static void main(String[] args) 
     {
      System.out.println("WELCOME TO THE UNDERGROUND PARKING GARAGE");
      System.out.println("-----------------------------------------");
      //Asking user if they own a membership card
      System.out.println("Do you own a membership card (Y or N)?");
      membership = input.nextLine();
     
      //Selection statement for if yes is entered
      if (membership.equalsIgnoreCase("Y"))
      {
                                    System.out.print("Enter Number of hours: ");
                                    hr = input.nextDouble();
     
                                    System.out.println("**********Charges**********");
                                    System.out.println("Parking for Members");
                                    if (hr > 50) {
     
                                      System.out.println("Your total charge is $100.00" + df.format(amount));
                                    } else {
                                            double amount = (hr) * 2.00;
                                            System.out.println("Charges= " + df.format(amount));
                                    }
      }
    Last edited by Freaky Chris; October 30th, 2011 at 03:56 PM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: How to make this code even simpler

    Hi blakmaze,

    Please use [highlight=java][/highlight] when posting java code to preserve formatting.

    You have a problem with what I assume is your decimalformat class, you have miss defined how it should ouput the result. I have attached below and example line which indicates how to do what you are after using the printf command.

    System.out.printf("Hello %.3f", 0.1242);

    Regards,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. How to make code more efficient?
    By Apocalypse in forum Java Theory & Questions
    Replies: 2
    Last Post: October 21st, 2011, 10:07 AM
  2. need help to make my code flexible.
    By sagar474 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2011, 01:11 PM
  3. How to make HyperLink in java code?
    By abhiM in forum Java Theory & Questions
    Replies: 1
    Last Post: August 24th, 2011, 08:16 AM
  4. Make my code follow MVC
    By alpvii in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 29th, 2010, 07:48 AM
  5. The simpler the program - seemingly the more scope for error :p
    By Bacon n' Logic in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 14th, 2010, 07:10 PM