Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-07-2008, 01:19 PM
Member
 
Join Date: Dec 2008
Posts: 4
Rep Power: 0
Dometic is on a distinguished road
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.


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:
File Type: txt code.txt (936 Bytes, 0 views)

Last edited by Dometic; 12-08-2008 at 01:03 AM. Reason: Problem was resolved
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-07-2008, 03:46 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
For one thing, when you decalre local variables, they must be initialised with some sort of value. So
Code:
int itemPrice;
must be changed to something like
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-07-2008, 04:20 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default Suggestions...
A couple of suggestions:
  • the "else" statement is missing brackets.
Code:
else {
            itemShip = 8.95;
            itemTotal = itemPrice + itemShip;
            System.out.println("Your total is:" + itemTotal);}
  • "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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-07-2008, 07:37 PM
Senior Member
 
Join Date: Nov 2008
Posts: 275
Rep Power: 2
neilcoffey is on a distinguished road
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
__________________
Neil Coffey
Javamex - Java tutorials and performance info
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-07-2008, 07:38 PM
Senior Member
 
Join Date: Nov 2008
Posts: 275
Rep Power: 2
neilcoffey is on a distinguished road
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!
__________________
Neil Coffey
Javamex - Java tutorials and performance info
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-07-2008, 11:05 PM
Member
 
Join Date: Dec 2008
Posts: 4
Rep Power: 0
Dometic is on a distinguished road
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.

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)
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-07-2008, 11:52 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
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:
Code:
int itemPrice;
.
.
.
double itemShip, itemTotal;
.
.
.
itemTotal = itemPrice;
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-08-2008, 12:43 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-08-2008, 12:44 AM
Member
 
Join Date: Dec 2008
Posts: 4
Rep Power: 0
Dometic is on a distinguished road
Default
I got it to work using this:
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-08-2008 at 12:46 AM. Reason: almost got it
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-08-2008, 12:52 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,499
Rep Power: 8
Fubarable is on a distinguished road
Default
Best way to format currency is to use a NumberFormat object, the via its getCurrencyFormat() method:

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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-08-2008, 12:56 AM
Member
 
Join Date: Dec 2008
Posts: 4
Rep Power: 0
Dometic is on a distinguished road
Default
Okay I got it, thank you for all who helped me.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
confused updev AWT / Swing 6 11-14-2008 04:33 PM
The words *game* in java programming :confused: ibmzz Advanced Java 1 01-23-2008 10:23 AM
a lot confused vineethraj New To Java 4 01-18-2008 01:36 AM
what does it mean:confused: sivasayanth New To Java 2 01-12-2008 05:52 AM
Java confused over Generics? Bibendum New To Java 3 12-26-2007 07:23 AM


All times are GMT +2. The time now is 01:40 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org