Results 1 to 11 of 11
- 03-15-2010, 07:01 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
Adding numbers for sales reciept program(Biginner)
hello guys can someone help me... i a program that can add numbers... for example you enter 1 number then when you enter the second number it adds the two inputs.... ive tried my best but this is the only thing i can do... pls help....
Java Code:import java.io.*; public class reciept { public static void main(String []args)throws IOException{ BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in)); String in; int input1, total; do{ System.out.println("enter a number: "); input1=Integer.parseInt(userInput.readLine()); total=+input1; System.out.println(total+"\n"); System.out.println("add another number? [y]yes or [n]no"); in=userInput.readLine(); if(in.equalsIgnoreCase("y")); if(in.equalsIgnoreCase("n")){System.exit(0);} }while(true); } }
- 03-15-2010, 11:11 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Thanks for using CODE tags. Spacing and indentation are very important -- develop good style habits now! If you develop sloppy habits when you are a beginner, they will be very difficult to break, and you just make things so much harder for yourself. Here is your code, not fixed, but simply re-formatted with proper spacing and indentation. See how much easier it is to read?
Give your class a name that starts with an upper-case letter. If it's an English word, spell it correctly. So change "reciept" to "Receipt".Java Code:import java.io.*; public class reciept { public static void main(String []args) throws IOException{ BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); String in; int input1, total; do { System.out.println("enter a number: "); input1 = Integer.parseInt(userInput.readLine()); total [COLOR="Red"]=+[/COLOR] input1; System.out.println(total + "\n"); System.out.println("add another number? [y]yes or [n]no"); in = userInput.readLine(); if (in.equalsIgnoreCase("y")); if (in.equalsIgnoreCase("n")) { System.exit(0); } } while (true); } }
Your real error should almost jump out at you, now that the code is formatted properly. Take a good look, and if you still can't find it, ask again.
Also, this:
is not good style. If that is really what you want to do, consider writing it this way:Java Code:if (in.equalsIgnoreCase("y"));
The way your code is written right now, it will interpret anything other than "n" or "N" as a "Yes" -- is that what you want?Java Code:if (in.equalsIgnoreCase("y")) { // do nothing }
-Gary-Last edited by gcalvin; 03-16-2010 at 01:30 AM.
- 03-16-2010, 01:25 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
hehe thank you very much for the advice... hehe ill remember not to be sloppy next time... hehe xD
TT^TT i still dont know how to add inputs.... Im very slow when it comes to this pls help....Java Code:import java.io.*; public class Receipt { public static void main(String []args) throws IOException{ BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); String in; int input1, total; do { System.out.println("enter a number: "); input1 = Integer.parseInt(userInput.readLine()); total =+ input1; System.out.println(total + "\n"); System.out.println("add another number? [y]yes or [n]no"); in = userInput.readLine(); if (in.equalsIgnoreCase("y"));{//"Go back to enter another number"} if (in.equalsIgnoreCase("n")) { System.exit(0); } } while (true); } }
- 03-16-2010, 01:31 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I edited my above post. Take a good look.
Also,
The curly braces do nothing there -- get rid of them, as they will only confuse you or somebody else. And I still don't think the logic there is quite what you want. Once you fix the other problem, try running this and see what happens when you are prompted for [y] or [n] but you actually type "x" or "q" or even "[n]" or "No".Java Code:if (in.equalsIgnoreCase("y"));{//"Go back to enter another number"}
-Gary-Last edited by gcalvin; 03-16-2010 at 01:35 AM.
- 03-16-2010, 01:39 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Actually, I was wrong. The curly braces do worse than nothing, as the second one is commented out. Get rid of them.
-Gary-
- 03-16-2010, 02:00 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
TT^TT its still the same....
for example i want to enter "5" for the first try then i press "y" so that i can enter the second number "6".... how can i make it so that the display is "11".............Java Code:import java.io.*; public class Receipt { public static void main(String []args) throws IOException{ BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); String in; int input1, total; do { System.out.println("enter a number: "); input1 = Integer.parseInt(userInput.readLine()); total =+ input1; System.out.println(total + "\n"); System.out.println("add another number? [y]yes or [n]no"); in = userInput.readLine(); if (in.equalsIgnoreCase("y"));{//"Go back to enter another number"} if (in.equalsIgnoreCase("n")) { System.exit(0); } } while (true); } }
- 03-16-2010, 02:08 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
That says, for example, if input1 has the value 5, then total should be set to +5 (which is 5 -- we don't display the sign for positive numbers). It does not say that total should have its value increased by 5. For that, you need:Java Code:total =+ input1;
Also,Java Code:total += input1;
You don't want println() there -- you want print().Java Code:System.out.println("enter a number: ");
-Gary-
- 03-16-2010, 02:29 AM #8
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
hehe xD wooot!!! thank you very much for helping.... hehe
Java Code:import java.io.*; public class Receipt { public static void main(String []args) throws IOException{ BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); String in; int input1, total=0; do { System.out.print("enter a number: "); input1 = Integer.parseInt(userInput.readLine()); total+=input1; System.out.println(total + "\n"); System.out.println("add another number? [y]yes or [n]no"); in = userInput.readLine(); if (in.equalsIgnoreCase("y"));//add another number if (in.equalsIgnoreCase("n")) { System.exit(0); } } while (true); } }
- 03-17-2010, 02:13 AM #9
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
can i ask another question Mr. gcalvin... can you tell me how I can make a list of the numbers that I inputted? for example:
-----------------(display)
input1 = 5.00
input2 = 6.00
total = 11.00
- 03-17-2010, 06:03 AM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Study about List<> and ArrayList<>.
-Gary-
- 03-18-2010, 04:09 AM #11
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Biginner! Array to LinkedList
By SungsooKim in forum New To JavaReplies: 3Last Post: 09-28-2009, 08:18 AM -
Adding numbers in an array?
By hawaiifiver in forum New To JavaReplies: 9Last Post: 01-22-2009, 03:50 AM -
prime numbers program
By i contra i in forum New To JavaReplies: 9Last Post: 01-15-2009, 07:22 AM -
Adding numbers in a 2 dimensional array
By j0shizabeast in forum New To JavaReplies: 2Last Post: 11-27-2007, 04:31 AM -
Adding numbers in array
By Shaolin in forum New To JavaReplies: 1Last Post: 11-15-2007, 06:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks