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 Java, how can I convert a string user input for example a student starts a...
In Java, how can I convert a string user input for example a student starts a uni degree in "Winter/2022", to a date type, and then once I have done that, add 3 years to that value, and changing the season, so for example Student Started: Winter/2022 and Student Finished: Summer/2025. Is there any possible way to do this? Thank you.
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.
how can I save a character stack to a string and then save that string into...
how can I save a character stack to a string and then save that string into a new string arraylist in java? so if the character stack is h, e, l, l, o i want it to save "hello" to a string and then put that string into an array list.
I need convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
Java Write a method that counts how many times one string appears in another string. The...
Java Write a method that counts how many times one string appears in another string. The method takes three input parameters: two Strings and one Boolean. The Boolean value determines whether the words are allowed to overlap each other. For example: When the method is called with input parameters (“balloon”, “oo”, false), it returns 1. When the method is called with input parameters (“hh”, “hhhhhh”, true) returns 5. When the method is called with input parameters (“cc”, “abcdefg”, true), returns...
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?...
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...
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 -/+
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT