bayfasad.blogg.se

Blue pelican java textbook answers key exercise 16
Blue pelican java textbook answers key exercise 16





blue pelican java textbook answers key exercise 16
  1. BLUE PELICAN JAVA TEXTBOOK ANSWERS KEY EXERCISE 16 CODE
  2. BLUE PELICAN JAVA TEXTBOOK ANSWERS KEY EXERCISE 16 PLUS

However, this code breaks on the third pass, because "1345 - 137" is clearly not an integer value. first pass, temp = "1345 - 137", code breaks at Integer.parseInt(temp) You would typically use Integer.parseInt() to turn a String to an int, like so: // start with + In the above example, temp would first be "8" (note that it's a String here), then "33" on the next pass, then "1345 - 137" on the third pass. You can still pull this off with one loop, but it will require some intermediate steps. How can you account for mixed operators? int sum = 0

blue pelican java textbook answers key exercise 16

Now, let's look at your sample input: 8 + 33 + 1345 - 137.

blue pelican java textbook answers key exercise 16

BLUE PELICAN JAVA TEXTBOOK ANSWERS KEY EXERCISE 16 PLUS

Since these values don't change (they are constants), and (in a real program) would be used multiple times in different places, we typically store these in their own final variables: final String PLUS = "\\s*\\+\\s*" // by convention, constants in java are named with all caps Since you are only dealing with + and -, your delimiters are "\\s*\\+\\s*" and "\\s*\\-\\s*", respectively. and so on additionally, you're probably not worried about input checking (making sure there are no letters/symbols/etc) or negative numbers, so we'll ignore those scenarios. Since you haven't learned about arrays yet and are working with a Scanner with a delimiter pattern, we'll have to work around this constraint.įirst, for the purposes of a school assignment, we can probably safely assume that your inputs will follow a particular pattern, along the lines of: (number)(operator)(number)(operator)(number). I'm going to help you answer this in a way that's (hopefully) consistent with what you've learned so far. Now we convert the String to a scanner because we will be using Scanner methodsĬan anybody help me solve the problem (First comment in my source code)? Here is my source code: //I can't get it to work if I use subtraction and addition at the same time but they work separately Now modify the program as to allow either plus or minus signs a minus sign surrounded by any amount of white space. Set delimiters to a plus sign surrounded by any amount of white space.or. object otherwise, it can get stuck waiting for input. String s = kb.nextLine( ) //Best to store in a String and then create a new Scanner So my teacher gave me an assignment to do (will be shown below) and I tried to do it but just could not figure it out, here's the assignment:Ĭonsider 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 white space) as delimiters and produces the sum of these numbers (1523).







Blue pelican java textbook answers key exercise 16