Results 1 to 11 of 11
  1. #1
    Dometic is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default New to java a little confused.

    ****Problem was resolved****







    Hi I am new to this so bare with me. I am taking an online course for java programming we have to make a program that will calculate whether an online purchase is eligable for free shipping or not. Here is what I have when I try to run it it has a problem with "itemPrice = keyboard.nextInt();" Is there something else I should use? It wont build it if I dont have something in there for the itemPrice integer.


    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package lab3;
    import java.util.Scanner;
    /**
     *
     * @author Jonathon
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            int itemPrice;
            double itemShip, itemTotal;
            System.out.println("Enter Price:");
            itemPrice = keyboard.nextInt();
            if (itemPrice >= 99.99){
                itemShip = 0;
                System.out.println("You are eligable for free shipping!");
                itemTotal = itemPrice;
                System.out.println("Your total is:" + itemTotal);
            }else
                itemShip = 8.95;
                itemTotal = itemPrice + itemShip;
                System.out.println("Your total is:" + itemTotal);
            }
            }
    Attached Files Attached Files
    Last edited by Dometic; 12-08-2008 at 12:03 AM. Reason: Problem was resolved

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    For one thing, when you decalre local variables, they must be initialised with some sort of value. So
    Java Code:
    int itemPrice;
    must be changed to something like
    Java Code:
    int itemPrice = 0;
    Whether or not this will solve your porblem, we can't be 100% sure, as you have not told us what that problem is.

    Post the compiler message(s) you are getting.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Suggestions...

    A couple of suggestions:
    • the "else" statement is missing brackets.

    Java Code:
    else [B][COLOR="Red"]{[/COLOR][/B]
                itemShip = 8.95;
                itemTotal = itemPrice + itemShip;
                System.out.println("Your total is:" + itemTotal);[B][COLOR="red"]}[/COLOR][/B]
    • "Main" is not a good name for a program. Change it to something more descriptive like "shipping" or "CalcShipCost"... something like that.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    masijade -- that's not actually true:

    (1) it's perfectly fine to declare a local variable without assigning it a value -- but the compiler will throw an error if you try to read from the variable before you first assign to it
    (2) for that reason, I'd recommend that you DON'T assign a "dummy" value to variables -- if you set a dummy value and then in your code forget to assign the "proper" value to it, the compiler won't pick up the mistake

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    To the original poster: in this case, you may as well declare the variable when you first assign to it:

    int itemPrice = keyboard.nextInt();

    Remember this isn't C: you don't need to have a big list of declared variables at the top of your method -- you can declare them as you need them!

  6. #6
    Dometic is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    This is what comes up when I enter the value. The integer itemPrice is a user imputed variable. I can't set it to 0 because it will automatically skip to the last part and tell me the total is 8.95.

    Java Code:
    init:
    deps-jar:
    compile:
    run:
    Enter Price:
    10.99
    Exception in thread "main" java.util.InputMismatchException
            at java.util.Scanner.throwFor(Scanner.java:819)
            at java.util.Scanner.next(Scanner.java:1431)
            at java.util.Scanner.nextInt(Scanner.java:2040)
            at java.util.Scanner.nextInt(Scanner.java:2000)
            at lab3.Main.main(Main.java:22)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 5 seconds)

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default No problem...

    In this case, if you want you can assign it a value because it gets initialized again with the keyboard.nextInt() method, although when I compile it it doesn't choke on it not being assigned a value. I also agree with Neil... if you forget to assign a value later on in the program, it will cause you grief. It's your call.

    Maybe your problem is that your trying to assign an int to a double:
    Java Code:
    [B][COLOR="Red"]int[/COLOR][/B] itemPrice;
    .
    .
    .
    [B][COLOR="red"]double[/COLOR][/B] itemShip, itemTotal;
    .
    .
    .
    itemTotal [B][COLOR="red"]=[/COLOR][/B] itemPrice;
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    I can only claim temporary insanity.

    In any case, it's "skipping to the end" because your else is not braketed (as pointed out earlier).

    Your exception, though, is coming because itemPrice is an int, you're using nextInt and you're probably entering a decimal number (which would be a float or a double). And, judging by your if statement that comes after that line, I'm assuming that itemPrice should probably be a double anyway, and that you should be using nextDouble.

  9. #9
    Dometic is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    I got it to work using this:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package lab3;
    import java.math.*;
    import java.text.*;
    import java.util.Scanner;
    /**
     *
     * @author Jonathon
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            double Price, Ship, Total;
            System.out.println("Enter Price:");  
            Price = keyboard.nextDouble();
            System.out.println("You entered: $" + Price);
            Ship = 8.950;
            if (Price >= 99.990){
                Ship = 0;
                Total = Price;
                System.out.println("Your total is: $" + Total);
            }else {
                Ship = 8.950;
                Total = Price + Ship;
                System.out.println("Your total is: $" + Total);
                
            }
            }
    }
    But the thing is with this, it doesnt round to two decimal places for the money value it has more. How can I round?
    Last edited by Dometic; 12-07-2008 at 11:46 PM. Reason: almost got it

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Best way to format currency is to use a NumberFormat object, the via its getCurrencyFormat() method:

    Java Code:
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
        System.out.println("Enter Price:");
        Price = keyboard.nextDouble();
        System.out.println("You entered: " + currencyFormat.format(Price));
    Also, it's a minor sin to start your variables with capital letters, and it's a major sin to use double to represent currency. :)

  11. #11
    Dometic is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

Similar Threads

  1. confused
    By updev in forum AWT / Swing
    Replies: 6
    Last Post: 11-14-2008, 03:33 PM
  2. The words *game* in java programming :confused:
    By ibmzz in forum Advanced Java
    Replies: 1
    Last Post: 01-23-2008, 09:23 AM
  3. a lot confused
    By vineethraj in forum New To Java
    Replies: 4
    Last Post: 01-18-2008, 12:36 AM
  4. what does it mean:confused:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 04:52 AM
  5. Java confused over Generics?
    By Bibendum in forum New To Java
    Replies: 3
    Last Post: 12-26-2007, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •