Results 1 to 11 of 11
Thread: New to java a little confused.
- 12-07-2008, 12:19 PM #1
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.
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); } }Last edited by Dometic; 12-08-2008 at 12:03 AM. Reason: Problem was resolved
- 12-07-2008, 02:46 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
For one thing, when you decalre local variables, they must be initialised with some sort of value. So
must be changed to something likeJava Code:int itemPrice;
Whether or not this will solve your porblem, we can't be 100% sure, as you have not told us what that problem is.Java Code:int itemPrice = 0;
Post the compiler message(s) you are getting.
- 12-07-2008, 03:20 PM #3
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-07-2008, 06:37 PM #4
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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 mistakeNeil Coffey
Javamex - Java tutorials and performance info
- 12-07-2008, 06:38 PM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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
- 12-07-2008, 10:05 PM #6
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.
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)
- 12-07-2008, 10:52 PM #7
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:
Luck,Java Code:[B][COLOR="Red"]int[/COLOR][/B] itemPrice; . . . [B][COLOR="red"]double[/COLOR][/B] itemShip, itemTotal; . . . itemTotal [B][COLOR="red"]=[/COLOR][/B] itemPrice;
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-07-2008, 11:43 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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-07-2008, 11:44 PM #9
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
I got it to work using this:
But the thing is with this, it doesnt round to two decimal places for the money value it has more. How can I round?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); } } }Last edited by Dometic; 12-07-2008 at 11:46 PM. Reason: almost got it
-
Best way to format currency is to use a NumberFormat object, the via its getCurrencyFormat() method:
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. :)Java Code:NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); System.out.println("Enter Price:"); Price = keyboard.nextDouble(); System.out.println("You entered: " + currencyFormat.format(Price));
- 12-07-2008, 11:56 PM #11
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
confused
By updev in forum AWT / SwingReplies: 6Last Post: 11-14-2008, 03:33 PM -
The words *game* in java programming :confused:
By ibmzz in forum Advanced JavaReplies: 1Last Post: 01-23-2008, 09:23 AM -
a lot confused
By vineethraj in forum New To JavaReplies: 4Last Post: 01-18-2008, 12:36 AM -
what does it mean:confused:
By sivasayanth in forum New To JavaReplies: 2Last Post: 01-12-2008, 04:52 AM -
Java confused over Generics?
By Bibendum in forum New To JavaReplies: 3Last Post: 12-26-2007, 06:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks