// Program prompts user to enter a series of integers// separated by spaces// Program converts them to numbers and sums themimport java.util.*;public class DebugSeven2{public static void main(String[] args){String str;int x;int length;int start;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(" -------------------" +"\nThe sum of the integers is " + sum);}}

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter2: Using Data
Section: Chapter Questions
Problem 15PE
icon
Related questions
Question
100%

// Program prompts user to enter a series of integers
// separated by spaces
// Program converts them to numbers and sums them
import java.util.*;
public class DebugSeven2
{
public static void main(String[] args)
{
String str;
int x;
int length;
int start;
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(" -------------------" +
"\nThe sum of the integers is " + sum);
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT