Question

In: Computer Science

Given a string like the following one in Java "123+45+678", how can I pull out the...

Given a string like the following one in Java "123+45+678", how can I pull out the characters that make up the separate integers and add them together. I'm not allowed to use parseInt().

So for this one I would need to add 123 + 45 + 678 to get the total(846)

Solutions

Expert Solution

Program:

public class Main
{
        public static int convertStr(String str)  
    {  
      
        // Initializing a variable  
        int num = 0;  
        // Returns length of the given string
        int len = str.length();  
      
        // Iterate till length of the string  
        for(int x = 0; x < len; x++)  
            // Subtract 48 from the current digit  
            // Converting string to number
            num = num * 10 + ((int)str.charAt(x) - 48);  
        // Returns number
        return num; 
    }
        
        public static void main(String[] args) {
                String givenStr = "123+45+678";
                // Splitting the given string based on +
                String[] splittedStr = givenStr.split("\\+");
                // Converting the strings to intergers and adding them
                int total = convertStr(splittedStr[0]) + convertStr(splittedStr[1]) + convertStr(splittedStr[2]);
                // Printing the output as total
                System.out.println("Total: "+splittedStr[0]+" + "+splittedStr[1]+" + "+splittedStr[2]+" = "+total);
        }
}

Output:


Related Solutions

In a java program: a) Given the following list of numbers: 90 8 7 56 123...
In a java program: a) Given the following list of numbers: 90 8 7 56 123 235 9 1 653 trace the execution for: Selection Sort (only the first 5 steps) MergeSort. (b) Given the following list of numbers: 3 1 4 1 5 9 2 6 5 3 5 trace the execution for quicksort with median-of-three partitioning and a cutoff of 3.
How can I write a separate java class to change a given name or a given...
How can I write a separate java class to change a given name or a given email of a user that was already saved (keep in mind that there may be numerous names and emails). Note: It should change the name or email that is saved in the txt file. Here is my code so far: User class public class User { private String fName; private String lName; private String UserEmail; public User(String firstName, String lastName, String email) { this.fName...
How can I identify the PUN (Pull Up Network) and PDN (Pull Down Network), if shown...
How can I identify the PUN (Pull Up Network) and PDN (Pull Down Network), if shown a schematic diagram of each.
Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
How would I work the following problems out? I would like someone to show their work...
How would I work the following problems out? I would like someone to show their work so I could better understand the answers and thought process. Genetics Problems For the first couple of problems, you will be working with guinea pigs. Their coat color shows an example of complete dominance - black (B) is dominant over brown (b). 1. A heterozygous black male is crossed with a heterozygous black female. What would be the resulting phenotypic ratio in the offspring?...
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
How can I make a method to check out a customer given their name and booking...
How can I make a method to check out a customer given their name and booking number in Java. I have provided the Book class as well as the customer class. I have done the method to add a customer into the array however im failng to implement a method that checks out the customer. If you need more classes associated with the system please let me know ASAP and I will provide them, ----------------------------------------------------------------------------------------------------------------------------------- public class Book{    String...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
given the following questions, i would like to ... Question Given the following questions, I would...
given the following questions, i would like to ... Question Given the following questions, I would like to know the answers to c, d, and e. located at the bottom (due to 4 answers per post limit). Thanks! Platinum (Pt) is used in part of your car's catalytic converter to reduce carbon monoxide emissions by the following chemical reaction: 2CO(g) + O2(g) Pt 2CO2(g) ---> 1) A matrix contains 7.1mg Pt/g of matrix. What is the %Pt in the matrix...
IN JAVA: I am using binary and linear search methods in java. How can I Generate...
IN JAVA: I am using binary and linear search methods in java. How can I Generate a new array with 10000 elements, and initialize the elements to random values using Math.random() and how can i sort the array using Arrays.sort(). I just need examples, no exact code is necessary. The array must be of doubles.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT