java - Blue Pelican Add 'Em Up Project -


i've been working on blue pelican java project called add 'em several hours now, , can't figure out how work. project description this:

consider following program allows 8 + 33 + 1,345 +137 entered string input keyboard. scanner object uses plus signs (and adjoining whitespace) delimiters , produces 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 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 typically this: enter 8 + 33 + 1,345 +137 : 8 + 33 + 1,345 + 137 sum is: 1523 modify program allow either plus or minus signs. don’t forget allow leading plus or minus sign on first number in sequence. if leading number has no sign, assume number positive. output should typically appear follows: enter 8 + 33 + 1,345 -137 : 8 + 33+ 1,345 -137 sum is: 1249

the code below have. program works fine adding numbers, reason subtraction isn't working. example, if enter 5-2, answer comes out -7 instead of 3.

public class addemup {      public static void main(string[] args) {          scanner kb = new scanner(system.in);          system.out.print("enter 8 + 33 + 1345 - 137 : ");         string s = kb.nextline();          scanner sc = new scanner(s);         int sum = 0;          if (s.contains("+")) {             sc.usedelimiter("\\s*\\+\\s*");             while (sc.hasnextint()) {                 sum = sum + sc.nextint();             }         }          if (s.contains("-")) {             sc.usedelimiter("\\s*\\-\\s*");             while (sc.hasnextint()) {                 sum = sum - sc.nextint();             }         }         system.out.println("sum is: " + sum);     } 

any on should trying in order fix appreciated. thanks!

the subtraction case going require little more work addition in code! think once understand happening, you'll see how need adjust strategy.

this happening right input 5 - 2.

int sum = 0 s.contains("-") true. sum = sum - sc.nextint() occurs, remember @ top of code sum set zero! sum = 0 - 5  sum = -5. s.contains("-") true. sum = sum - sc.nextint() occurs sum = -5 - 2.  @ end of program, sum = -7. 

you going need restructure logic of program bit. right now, first thing check see if there either '+' or '-' symbol in input. if there '+' symbol, start sum=0 , add every number see. works pretty addition, , you'll right answer. if there '-' symbol, start sum=0 , subtract every number see. that's not quite intention subtraction though. in case of 5 - 2, rather sum=0 '+ 5' , '- 3'.

your code fine in sense compiles , runs - have here known 'logic bug'. steps have asked program not same actual steps want do. give careful thought happening @ each point of program make sure behaves correctly.


Popular posts from this blog