Results 1 to 15 of 15
Thread: Please HELP Java small program
- 11-07-2008, 12:16 PM #1
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
Please HELP Java small program
Hello there,
I need to do a small program which is:
Write a program called ShoppingCalculator.java that allows the user to
enter the prices of several items they have bought and then tells them the
total amount. The user can enter the prices for as many items as they
want, until they click Cancel. The program should then output the total
amount they spent, in a message dialog box. Use pounds for all the
amounts.
Unfortunately, after spending many hours trying to figure out how to make this programme work correctly! I couldn’t.
I guess it’s because I am a very beginner at java programming :-)
I would really appreciate it if you could help me out.
Thanks
Here is what I have done so far:
Java Code:import javax.swing.JOptionPane; public class ShoppingCalculator { public static void main(String[] args) { boolean done = false; double amount; String input; double total = 0; while (!done) { input = JOptionPane.showInputDialog("Enter the amount you spent on the next item:"); amount = Double.parseDouble (input); { if (input == null)total = total + amount; } JOptionPane.showMessageDialog (null, "You spent a total amound of " + total ); } } }
- 11-07-2008, 01:04 PM #2
Member
- Join Date
- Jul 2008
- Posts
- 35
- Rep Power
- 0
I could see you've written
I think it should beJava Code:if(input == null) total = total + amount
because you'll add amount to total if and only if input has some value and not nullJava Code:if(input != null) total = total + amount
New to Java/PHP/Javascript development?
For free help go to- www.techcubetalk.com
- 11-07-2008, 05:37 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually you should deal with the input, null check.
Java Code:public static void main(String[] args) { double amount = 0.0; while(true) { String str = JOptionPane.showInputDialog(null, "Enter the value"); if(str != null) { amount += Double.parseDouble(str); } else break; } JOptionPane.showMessageDialog(null, "Amount is " + amount); }
- 11-07-2008, 05:51 PM #4
Some comments..
I have some comments on your usage of curly brackets (or is it braces?)... anyway...
Java Code:{ if (input == null)total = total + amount; }- The {} above, don't do anything (not needed, unless I missed their purpose)
- Always use {} for if-else statements when (even if) it's a one-liner. This way you can always add code to your if-else statements without having to worry about remembering to add the {}.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-07-2008, 05:54 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-07-2008, 06:07 PM #6
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
thank you
thank you so much , its working now ..:-)
- 11-08-2008, 01:58 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you solve the problem please mark it as solved lol. It's really helpful to all other members.
- 11-08-2008, 02:03 AM #8
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
Here it is fully working:
import javax.swing.JOptionPane;
public class ShoppingCalculator
{
public static void main(String[] args) {
double amount = 0.0;
while(true) {
String str = JOptionPane.showInputDialog(null, "Enter the value");
if(str != null) {
amount += Double.parseDouble(str);
}
else
break;
}
JOptionPane.showMessageDialog(null, "Amount is £" + amount);
}
}
Thanks alot Eranga :)Last edited by afrttoh; 11-08-2008 at 02:06 AM.
- 11-08-2008, 02:06 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ya, that's what I explain in my code segment.
By the way, are you working on Notepad? Why are you not using line indent there? If you use it, much easy to read the code.
- 11-08-2008, 02:09 AM #10
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
- 11-08-2008, 02:14 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-08-2008, 02:17 AM #12
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
yes , i think so , ill Google it and download it now .
thank you once aging you have been very helpful!
- 11-08-2008, 02:20 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-08-2008, 02:21 AM #14
Member
- Join Date
- Nov 2008
- Location
- London
- Posts
- 39
- Rep Power
- 0
- 11-08-2008, 02:29 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yep, it's free. If you want to work on with NetBeans download the latest version 6.1, there is another version 6.5 but it's a beta version.
Here is the download link,
Choose the correct one you need and download.
Actually for line indenting you need to use NetBeans, you can use Tab space for that in editplus. Only thing is you should have a clear idea how to put tab spaces correctly.
Similar Threads
-
Proofreading this small Java program
By almina in forum New To JavaReplies: 5Last Post: 10-23-2009, 07:42 AM -
java annotations - small prog
By sebo in forum New To JavaReplies: 5Last Post: 10-24-2008, 05:48 AM -
small issues with a program
By jimJohnson in forum New To JavaReplies: 6Last Post: 04-25-2008, 08:28 AM -
Small scale Java Editor
By Greenfrog99 in forum AWT / SwingReplies: 0Last Post: 01-27-2008, 08:46 PM -
Small tennis simulation in Java
By diego in forum New To JavaReplies: 1Last Post: 12-02-2007, 01:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks