Lesson 2
String Manipulation: Parsing and Multiplying Numbers in Java
Introduction

Welcome! In this unit, we have an exciting and practical task that will test your Java programming skills. We will be parsing strings and making type conversions. So, let's dive into it!

Task Statement and Description

Our task for the day involves creating a Java method called parseAndMultiplyNumbers(). This method is designed to accept a string as an input. However, it's not just any string — the input we'll consider is a playful mix of numbers and words.

The purpose of this method is to analyze the input string, extract all the numbers, convert these numbers (currently string types) into integer data types, and then multiply all these numbers together. The final output? It's the product of all those numbers!

Here's an illustration for clarification. Given the input string "I have 2 apples and 5 oranges," our method should return the product of 2 and 5, which is 10.

Step-by-Step Solution Building: Step 1

The primary task is to parse the string and identify the numbers. To do that, let's create an empty string, num, to accumulate digits and an ArrayList<Integer> to collect all the numbers we find:

Java
1String inputString = "I have 2 apples and 5 oranges"; 2String num = ""; 3ArrayList<Integer> numbers = new ArrayList<>();
Step-by-Step Solution Building: Step 2

The next step requires iterating through the input string character by character. When we encounter a digit, we append it to our num string. If a character isn’t a digit and num isn’t empty, it means we've reached the end of a number.

At this point, we convert num to an integer, add it to the numbers list, and reset num to an empty string. If the character isn’t a digit and num is empty, we simply skip and progress.

Java
1for (char ch : inputString.toCharArray()) { 2 if (Character.isDigit(ch)) { 3 num += ch; 4 } else if (!num.isEmpty()) { 5 numbers.add(Integer.parseInt(num)); 6 num = ""; 7 } 8} 9// After the loop, we must check if 'num' is not empty 10// because it indicates that the last part of the string contains a number. 11if (!num.isEmpty()) { 12 numbers.add(Integer.parseInt(num)); 13} 14for (int number : numbers) { 15 System.out.print(number + " "); 16}

After running this code, the output should be 2 5.

Step-by-Step Solution Building: Step 3

Finally, we multiply all the numbers in the numbers list together. The multiplication result gets stored in the result variable.

Java
1int result = 1; 2for (int number : numbers) { 3 result *= number; 4} 5System.out.println(result);

After executing this code, the console output should be 10.

Full Solution

Bringing together all the steps, our final Java solution manifests as follows:

Java
1import java.util.ArrayList; 2 3public class StringParser { 4 5 public static int parseAndMultiplyNumbers(String inputString) { 6 String num = ""; 7 ArrayList<Integer> numbers = new ArrayList<>(); 8 9 for (char ch : inputString.toCharArray()) { 10 if (Character.isDigit(ch)) { 11 num += ch; 12 } else if (!num.isEmpty()) { 13 numbers.add(Integer.parseInt(num)); 14 num = ""; 15 } 16 } 17 if (!num.isEmpty()) { 18 numbers.add(Integer.parseInt(num)); 19 } 20 21 int result = 1; 22 for (int number : numbers) { 23 result *= number; 24 } 25 return result; 26 } 27 28 public static void main(String[] args) { 29 // Call the method 30 System.out.println(parseAndMultiplyNumbers("I have 2 apples and 5 oranges")); 31 } 32}

This solution also caters to numbers situated at the end of the input string.

Lesson Summary

Applaud yourself! You've successfully developed a Java method that deftly navigates strings to identify numbers, performs a data type conversion, and then conducts an arithmetic operation on those numbers. You've truly demonstrated admirable skill in orchestrating these coding concepts!

However, as always in coding, practice is key to improvement. With this solution, you could try to perform different operations on the numbers or change the condition for identifying valid numbers, thereby further sharpening your Java skills. Here's to coding greatness!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.