Project…+Add+‘em+Up

Consider the following program that allows something like 8 + 33 + 1,345 +137 to be entered as String input from the keyboard. A Scanner object then uses the plus signs (and any adjoining whitespace) as delimiters and produces the sum of these numbers(1523).

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*\\+\\s*");

int sum = 0; while(sc.hasNextInt) { sum = sum + sc.nextInt; } System.out.println("Sum is: " + sum); } }

The output will typically look like this: Enter something like 8 + 33 + 1,345 +137 : 8 + 33 + 1,345 + 137 Sum is: 1523

leading plus or minus sign on the first number in the sequence. If the leading number has no sign, assume the number is positive. Your output should typically appear as follows: Enter something like 8 + 33 + 1,345 -137 : 8 + 33+ 1,345 -137 Sum is: 1249
 * Now modify this program so as to allow either plus or minus signs**. Don’t forget to allow for a

**Finish** **the Code** import java.io.*; import java.util.*; public class Tester { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter something like 8 + 33 + 1,345 -137 : "); String s = sc.nextLine; if((s.charAt(0)!='+') && (s.charAt(0)!='-')) s = "+" + s;

Scanner sc1 = new Scanner(s); sc1.useDelimiter("\\s*\\+\\s*|\\s*-\\s*"); int sum = 0;

while(sc1.hasNextInt) { if(sc1.findInLine("\\s*\\+\\s*")!=null) }

Scanner sc2 = new Scanner(s); sc2**. ... **

while(sc2.hasNextInt) { if(sc2.findInLine("\\s*-\\s*")!=null) } System.out.println("Sum is: " + sum); } }