Java Code Help

Discussion in 'OT Technology' started by Kingfisher, Jun 24, 2006.

  1. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    Code:
    import java.util.Scanner;
    /**
        This program tests the CashRegister.java using console inputs
     */
    
    public class CashRegisterTester 
    {
        public static void main(String[] args) 
        {        
            Scanner in = new Scanner(System.in);
            CashRegister register = new CashRegister();
            
            System.out.print("Enter Price: ");
            float price = in.nextFloat();
            register.recordPurchase(price);
            
            System.out.print("Enter dollars: ");
            int dollars = in.nextInt();
            System.out.print("Enter quarters: ");
            int quarters = in.nextInt();
            System.out.print("Enter dimes: ");
            int dimes = in.nextInt();
            System.out.print("Enter nickels: ");
            int nickels = in.nextInt();
            System.out.print("Enter pennies: ");
            int pennies = in.nextInt();
            register.enterPayment(dollars, quarters, dimes, nickels, pennies);
            
            System.out.print("Your change is: $" + register.giveChange());       
        } 
    }
    For some reason this code will not let me enter any values (It will not display 'Enter price: ', it just hangs saying this: [im using netBeans btw]

    init:
    deps-jar:
    Compiling 1 source file to C:\Java\bin\CashRegister\build\classes
    compile-single:
    run-single:

    WHen I type in 6 junk values (the amount of inputs I need for the program), it displays:

    Enter Price: Enter dollars: Enter quarters: Enter dimes: Enter nickels: Enter pennies: Your change is: $-20.18
    BUILD SUCCESSFUL (total time: 1 minute 16 seconds)

    All on one line. I don't know what could be causing this. It won't even execute System.out.print("Enter Price: "); This was one of the examples in the text and it won't work on my computer. I have the latest version of Java on my computer and have installed correctly as well. Any ideas? :x:
     
  2. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
  3. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Are you sure your IDE has this class as the one to execute? What IDE are you using?
     
  4. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    I'm using netBeans 5.0

    My Java version is: 1.5.0_06

    I can't figure this out. The compiler just hangs (doesn't freeze) until I input 6 different values in the output window, then it just displays what is should. When it displays it looks like C++ code when the newline characters are messing thigns up, for example:

    Enter price: $Enter sales tax: $Enter quantity:

    Etc.. :sad:
     
  5. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Post the initializer for CashRegister.
     
  6. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    use println and they wont all be on one line. its not printing the values you as you enter? thats kinda weird
     
  7. Clarity

    Clarity New Member

    Joined:
    Feb 14, 2006
    Messages:
    3,309
    Likes Received:
    0
    lol @ java's verbosity
     
  8. Clarity

    Clarity New Member

    Joined:
    Feb 14, 2006
    Messages:
    3,309
    Likes Received:
    0
    system.out.print == cout<<
     
  9. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    That verbosity comes in handy when you're:

    1) Working with high level concepts and libraries. Try to make a SOAP client in C++. Then try it in Java. The Java one you can do in a day. The C++ one you can do in a week. If you're good at both languages. The Java version will have fewer bugs too.

    2) Working with other people. Verbosity is a good thing when you're working in groups.
     
  10. tyrionlannister

    tyrionlannister New Member

    Joined:
    Jun 13, 2006
    Messages:
    710
    Likes Received:
    0
    Location:
    New York
  11. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Its a problem with the initializer.
     
  12. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    Even when I comment out the CashRegister references, it still won't allow me to enter values. The code for CashRegister.java is below


    Code:
    public class CashRegister 
    {
        /**
            Constructs a CashRegister with no money in it.
         */
        public CashRegister()
        {
            purchase = 0;
            payment = 0;
        }
        /**
            Records the purchase price of an item.
            @param amount the price of the purchased item
         */
        public void recordPurchase(double amount)
        {
            purchase = purchase + amount;       
        }
        /**
            Enters payment received from the customer.
            @param dollars the number of dollars in the payment
            @param quarters the number of quarters in the payment
            @param dimes the number of dimes in the payment
            @param nickels the number of nickels in the payment
            @param pennies the number of pennies in the payment
         */
        public void enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies)
        {
            payment = dollars + (quarters * QUARTER_VALUE) + (dimes * DIME_VALUE) + (nickels * NICKEL_VALUE)  + (pennies * PENNY_VALUE);
        }
        /**
            Computes the change due to the customer and resets it.
            @return the change due to the customer
         */
        public double giveChange()
        {
            double change = payment - purchase;
            purchase = 0;
            payment = 0;
            return change;
        }
        
        public static final double QUARTER_VALUE = 0.25;
        public static final double DIME_VALUE = 0.10;
        public static final double NICKEL_VALUE = 0.05;
        public static final double PENNY_VALUE = 0.01;
            
        private double purchase;
        private double payment;
    }
    What do you mean by 'initializer'?
     
  13. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    The constructor:

    public CashRegister()
    {
    purchase = 0;
    payment = 0;
    }

    But its not.

    Listen, why do you define constants and declare variables at the bottom?
     
  14. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    Ok, I have included a screenshot of the IDE to give you guys an idea of what's going on here.

    Notice the code I am using, it is really simple and should work no problems, it is just testing if it will take input. This screenshot was taken after I Ran the file.
    http://img.photobucket.com/albums/v221/steadyballin/untitled.jpg

    Below is another screenshot of the IDE after I enter a value in, using the Input box on the bottom of the IDE. Notice I entered '3' and pressed enter, THEN the program ended after displaying what it should have initially displayed.
    http://img.photobucket.com/albums/v221/steadyballin/untitled2.jpg
     
  15. tyrionlannister

    tyrionlannister New Member

    Joined:
    Jun 13, 2006
    Messages:
    710
    Likes Received:
    0
    Location:
    New York
    I ran your exact code in IDEA and it allows the input without producing any errors or odd formatting.

    Code:
     "C:\Program Files\Java\jdk1.5.0_06\bin\java" -Didea.launcher.port=7537 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 5.0\bin" -Dfile.encoding=windows-1252 -classpath "C:\Program Files\Java\jdk1.5.0_06\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\jce.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext\sunpkcs11.jar;C:\Documents and Settings\Administrator\IdeaProjects\CashRegisterTest\classes;C:\Program Files\JetBrains\IntelliJ IDEA 5.0\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain CashRegisterTester
    Enter Price: 15.00
    Enter dollars: 12
    Enter quarters: 12
    Enter dimes: 20
    Enter nickels: 5
    Enter pennies: 25
    Your change is: $2.5
    Process finished with exit code 0
     
  16. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    Yea I don't know why NetBEans isn't allowing me to do that.

    How is IDEA? Is it a plugin for Eclipse or something? I also have Eclipse on this computer, but I don't like it because it's not very easy to use. Are you comfortable with it? I might give it a look if I can't get netBeans to support such a simple (standard??) feature
     
  17. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    Instance variables? :dunno:
    That's how this textbook is teaching it to me. (Java Concepts by Cay Horstmann)
     
  18. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Its much clearer when they're at the top.
     
  19. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    I agree. Well that's how I learned it for C++. I am starting to hate Java.
     
  20. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Its not Java's fault.

    Look, read the HOWTOs and documentation on getting started doing Java in Eclipse. It has a great debugger built in that will give you lots of details about what is doing wrong when things don't work.
     
  21. tyrionlannister

    tyrionlannister New Member

    Joined:
    Jun 13, 2006
    Messages:
    710
    Likes Received:
    0
    Location:
    New York
    http://www.jetbrains.com/idea/

    It's a complete commercial IDE, not a plugin. I'm sure netBeans and the others have most of the same features at this point, so the IDE choice is really just a matter of personal preference. I don't think I'd ever switch, though.
     

Share This Page