Results 1 to 6 of 6
Thread: Addition and Subtraction Program
 11282013, 06:35 PM #1Member
 Join Date
 Nov 2013
 Posts
 2
 Rep Power
 0
Addition and Subtraction Program
I am making a program that adds and subtracts numbers. I need to be able to get the leading sign to be recognized, but this is causing problems. When I have a space between the leading sign and the leading number ( 2  3) it is not working, but when I have no space (2  3) it is working. How do I solve this problem. Here is the code, and thanks for the help!
Java Code:import java.io.*; import java.util.*; public class Tester { public static void main(String args[]) { Scanner kb = new Scanner(System.in); System.out.print("Enter something like 8 + 33 + 1,345 + 137 : "); String s = kb.nextLine(); Scanner sc = new Scanner(s); sc.useDelimiter("\\s+"); String operation; int num; int sum = 0; int count = 0; operation = ""; char ch = s.charAt(0); while(sc.hasNextInt( )) { if(!(Character.isDigit(ch))) { operation = sc.next(); } num = sc.nextInt(); if (count == 0) { sum = sum + num; } else if (operation.contains( "+" )) { sum = sum + num; } else if (operation.contains("")) { sum = sum  num; } if(sc.hasNext()) { operation = sc.next(); } count++; } System.out.println("Sum is: " + sum); } }
 11282013, 06:41 PM #2
Re: Addition and Subtraction Program
How do I solve this problemIf you don't understand my response, don't ignore it, ask a question.
 11282013, 08:02 PM #3Member
 Join Date
 Nov 2013
 Posts
 2
 Rep Power
 0
Re: Addition and Subtraction Program
I'm sorry, but I'm a beginner. Can you please tell me what it means to redo the logic to handle the floating?
Thanks for the help!
 11282013, 08:14 PM #4
Re: Addition and Subtraction Program
What are the steps the program needs to do to parse its input?
Make a list describing what it should do.
Something like:
begin loop to scan through tokens on the expression
get next token
if token is number do this
if token is sign do that
end loop
evaluate what was found
The logic will need to consider what "state" it is in.
Did it just get a binary operator? What tokens are valid after an operator?
Did it just get a number? What tokens are valid after an operator? What are not valid?Last edited by Norm; 11282013 at 08:19 PM.
If you don't understand my response, don't ignore it, ask a question.
 11282013, 08:39 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
Re: Addition and Subtraction Program
You need to distinguish between unary operators and binary operators; your expression has the following syntax: <unary>? <number <binary> {<unary> <number>*; your <number>s can be unsigned.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 11292013, 02:40 AM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,248
 Rep Power
 9
Re: Addition and Subtraction Program
Are you allowed to use regular expressions? If so, you can use reformat the string to allow proper parsing by the scanner. If you are not allowed to use that then here is a hint. Don't think of the string as a mathematical expression. Just think of it as a list of positive or negative numbers that need to be added together. This assumes of course that you are not checking for input errors such as "5 + 10 10 4". Here, the second 10 has an implied + in front of it.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Problem with simple addition and subtraction calculator
By Nano in forum New To JavaReplies: 9Last Post: 02062012, 07:11 AM 
Java Addition Program Help with Action Listener
By Syanara in forum New To JavaReplies: 21Last Post: 05122011, 07:03 PM 
The addition program is providing incorrect sum '0'
By tabrez_k81 in forum New To JavaReplies: 4Last Post: 12162008, 02:43 PM 
Addition java program
By tabrez_k81 in forum New To JavaReplies: 5Last Post: 12152008, 11:08 AM 
Addition program that displays the sum of two numbers
By Java Tip in forum Java TipReplies: 0Last Post: 03282008, 09:46 PM
Bookmarks