Question

In: Computer Science

Writing method in Java Given a string, return the sum of the numbers appearing in the...

Writing method in Java

Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.)


sumNumbers("abc123xyz") → 123
sumNumbers("aa11b33") → 44
sumNumbers("7 11") → 18

Solutions

Expert Solution

JAVA program to return the sum of numbers appearing in a string

import java.util.Scanner;

public class sum{

static int sumNumbers(String str)
{
  
String temp = "0";
  
  
int sum = 0;
  

for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
  
  
if (Character.isDigit(ch))
temp += ch;
  
else {

sum += Integer.parseInt(temp);
  

temp = "0";
}
}
  

return sum + Integer.parseInt(temp);
}
  

public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);   
System.out.print("Enter a string: ");
String str= sc.nextLine();
System.out.print("You have entered: "+sumNumbers(str));   
}
}

Please give me a thumbs up if it is helpful------------


Related Solutions

Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
use java Write a program that, given two binary numbers represented as strings, prints their sum...
use java Write a program that, given two binary numbers represented as strings, prints their sum in binary. The binary strings are comma separated, two per line. The final answer should not have any leading zeroes. In case the answer is zero, just print one zero i.e. 0 Input: Your program should read lines from standard input. Each line contains two binary strings, separated by a comma and no spaces. Output: For each pair of binary numbers print to standard...
Java- creat a method that takes two char parameters. Return a String containing all characters, in...
Java- creat a method that takes two char parameters. Return a String containing all characters, in order, from the first char parameter (inclusive) to the last (inclusive). For instance, input('a', 'e'), and return "abcde".
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Program in java 1- Implement an algorithms to calculate the sum of all numbers in an...
Program in java 1- Implement an algorithms to calculate the sum of all numbers in an array.   2- Implement an algorithm to determine if an array has all unique integer values. 3- Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums. Example 1: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
double_vowels Given a string, return a copy of the string with all of the vowels doubled....
double_vowels Given a string, return a copy of the string with all of the vowels doubled. Consider the five letters 'aeiou' as vowels. double_vowels('pencil') → 'peenciil' double_vowels('xyzzy') → 'xyzzy' double_vowels('catalog') → 'caataaloog' middle_hash Given a string, do three hash marks '###' appear in the middle of the string? To define middle, we'll say that the number of chars to the left and right of the '###' must differ by at most one. middle_hash('aa###bb') → True middle_hash('aaa###bb') → True middle_hash('a###bbb') →...
Q2: The following code is supposed to return the sum of the numbers between 1 and...
Q2: The following code is supposed to return the sum of the numbers between 1 and n inclusive, for positive n. An analysis of the code using our "Three Question" approach reveals that: int sum(int n){ if (n == 1)     return 1; else     return (n + sum(n - 1)); } Answer Choices: it passes on all three questions and is a valid algorithm.     it fails the smaller-caller question.     it fails the base-case question.     it fails...
Java Write a valid Java method called printReverse that takes a String as a parameter and...
Java Write a valid Java method called printReverse that takes a String as a parameter and prints out the reverse of the String. Your method should not return anything. Make sure you use the appropriate return type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT