
12-07-2008, 01:19 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 4
Rep Power: 0
|
|
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);
}
} |
Last edited by Dometic; 12-08-2008 at 01:03 AM.
Reason: Problem was resolved
|
|

12-07-2008, 03:46 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
For one thing, when you decalre local variables, they must be initialised with some sort of value. So
must be changed to something like
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.
|
|

12-07-2008, 04:20 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
|
|
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.
|
|

12-07-2008, 07:37 PM
|
|
Senior Member
|
|
Join Date: Nov 2008
Posts: 275
Rep Power: 2
|
|
|
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
|
|

12-07-2008, 07:38 PM
|
|
Senior Member
|
|
Join Date: Nov 2008
Posts: 275
Rep Power: 2
|
|
|
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!
|
|

12-07-2008, 11:05 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 4
Rep Power: 0
|
|
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) |
|
|

12-07-2008, 11:52 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
|
|
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.
|
|

12-08-2008, 12:43 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
|
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.
|
|

12-08-2008, 12:44 AM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 4
Rep Power: 0
|
|
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
|
|

12-08-2008, 12:52 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 6,499
Rep Power: 8
|
|
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.
|
|

12-08-2008, 12:56 AM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 4
Rep Power: 0
|
|
|
Okay I got it, thank you for all who helped me.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 01:40 AM.
|
|