Sir/Madam, this is 3rd same question; one of your staff just make own code but i
ID: 3766522 • Letter: S
Question
Sir/Madam, this is 3rd same question; one of your staff just make own code but it didnt satisfied me. The last was confusing me and didnt meet my satistify so i posted it again here. the below, need to FIX and make it CORRECT coz it is error. please try to run that program u will understand. i am not asking to make own code or just answer. please dont make me posted again. dnt want to waste it! i need it by tonight. 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
Check this one i have made some changes in java we can not call a non static method from static without using string object
import java.util.*;
public class DebugSeven2
{
public static void main(String[] args)
{
String str;
int x;
int length;
int start = 0;
int num;
int lastSpace = -1;
int sum = 0;
String partStr;
Scanner in = new Scanner(System.in);
System.out.print("Enter a series of integers separated by spaces >> ");
str = in.nextLine();
length = str.length();
for(x = 0; x <= length; ++x)
{
if(str.charAt(x) == ' ')
{
partStr = str.substring(x, lastSpace + 1);
num = Integer.parseInt(partStr);
System.out.println(" " + num);
sum = num;
lastSpace = x;
}
}
partStr = str.substring(lastSpace + 1, length);
num = Integer.parseInt(partStr);
System.out.println(" " + num);
sum = num;
System.out.println(" -------------------" +" The sum of the integers is " + sum);
}
}