Sir/Madam, this is 2nd same question; one of your staff just make own code but i
ID: 3766380 • Letter: S
Question
Sir/Madam, this is 2nd same question; one of your staff just make own code but it didnt satisfied me so i posted it again here. the below, need to FIX and make it CORRECT coz it is error. i am not asking to make own code. THANK YOU!
Chapter 7; Debugging Exercises (DebugSeven2 to FixDebugSeven2), p. 388; Java Programming; 8th edition: Joyce Farrell; THANK YOU!!
a. DebugSeven2.java
1 // Program prompts user to enter a series of integers
2 // separated by spaces
3 // Program converts them to numbers and sums them
4 import java.util.*;
5 public class DebugSeven2
6 {
7 public static void main(String[] args)
8 {
9 String str;
10 int x;
11 int length;
12 int start = 0;
13 int num;
14 int lastSpace = -1;
15 int sum = 0;
16 String partStr;
17 Scanner in = new Scanner(System.in);
18 System.out.print("Enter a series of integers separated by spaces >> ");
19 str = in.nextLine();
20 length = length();
21 for(x = 0; x <= length; ++x)
22 {
23 if(str.charAt(x) == " ")
24 {
25 partStr = str.substring(x, lastSpace + 1);
26 num = Integer.parseInt(partStr);
27 System.out.println(" " + num);
28 sum = num;
29 lastSpace == x;
30 }
31 }
32 partStr = str.substring(lastSpace + 1, length);
33 num = Integer.parseInt(partStr);
34 System.out.println(" " + num);
35 sum = num;
36 System.out.println(" -------------------" +
37 " The sum of the integers is " + sum);
38 }
39 }
Explanation / Answer
tring numbers = "";
num = 123;
digit = num % 10; // digit=3
num /= 10; // num=12
numbers = digit + " " + numbers; // numbers="3 " uses old value on right side of the equals
// next iteration
digit = num % 10; // digit=2
num /= 10; // num=1
numbers = digit + " " + numbers; // numbers="2 3 " see how the digit is put to the left of the old value
// last iteration
digit = num % 10; // digit=1
num /= 10; // num=0
numbers = digit + " " +numbers; // numbers="1 2 3 " notice there is an extra space at the e