Results 1 to 6 of 6
  1. #1
    tmazing is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,884
    Rep Power
    25

    Default Re: Addition and Subtraction Program

    How do I solve this problem
    Re do the logic to handle the floating -
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    tmazing is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default 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!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,884
    Rep Power
    25

    Default 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; 11-28-2013 at 08:19 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 9
    Last Post: 02-06-2012, 07:11 AM
  2. Java Addition Program Help with Action Listener
    By Syanara in forum New To Java
    Replies: 21
    Last Post: 05-12-2011, 08:03 PM
  3. The addition program is providing incorrect sum '0'
    By tabrez_k81 in forum New To Java
    Replies: 4
    Last Post: 12-16-2008, 02:43 PM
  4. Addition java program
    By tabrez_k81 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 11:08 AM
  5. Replies: 0
    Last Post: 03-28-2008, 09:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •