Question

In: Computer Science

Java question - How can the code below be modified to accept multi digit input. String...

Java question - How can the code below be modified to accept multi digit input.

String e = "1+9+8";
int r = e.charAt(0)-'0';
for (int i = 1; i < e.length(); i+=2){
  
if (e.charAt(i) == '+'){
r += e.charAt(i+1)-'0';
}
else{
r -= e.charAt(i+1)-'0';
}

The only built in String methods that can be used are lowercase(), length(), and charAt(). Arrays and parseInt() cannot be used.

So we want to know how we can get an answer if a string such as "123+45+678-...." is entered.

Solutions

Expert Solution

Modified java code :

public class MultiDigitInput {

   public static void main(String[] args) {
       // string
       String e = "123+45+678-46";
      
       // res store the result of string
       int res = 0;
       // r store the numeric value of whole numerical term
       int r ;
      
      
       for (int i = 0; i < e.length(); ){
          
           r = 0;
           if (e.charAt(i) == '+'){
               i++;
               // this loop will calculate the whole numerical term
               while(i < e.length() && e.charAt(i) != '+' && e.charAt(i) !='-' )
               {
                   // every time add unit term after multiplying by 10
                   r = r*10 + e.charAt(i)-'0';
                   i++;
               }
              
               res += r;
           }
           else if (e.charAt(i) == '-'){
               i++;
               while(i < e.length() && e.charAt(i) != '+' && e.charAt(i) !='-')
               {
                   r = r*10 + e.charAt(i)-'0';
                   i++;
               }
              
               res -= r;
           }
           // this will calculate the first term
           else {
               while(i < e.length() && e.charAt(i) != '+' && e.charAt(i) !='-')
               {
                   r = r*10 + e.charAt(i)-'0';
                   i++;
               }
               res += r;
           }

       }
      
       System.out.println(res);
   }

}

Image for clear view :

output :

800

Hope you like it

Any Query? Comment Down!

I have written for you, Please up vote the answer as it encourage us to serve you Best !


Related Solutions

Question: How can the code below be implemented in java? TreapNode* deleteNode(TreapNode* root, int key) {...
Question: How can the code below be implemented in java? TreapNode* deleteNode(TreapNode* root, int key) {     // Base case     if (root == NULL) return root;        // IF KEYS IS NOT AT ROOT     if (key < root->key)         root->left = deleteNode(root->left, key);     else if (key > root->key)         root->right = deleteNode(root->right, key);        // IF KEY IS AT ROOT        // If left is NULL     else if (root->left == NULL)     {         TreapNode *temp = root->right;         delete(root);         root = temp; // Make right...
Java Code!!!! A five-digit number is said to be friendly if: the leftmost digit is divisible...
Java Code!!!! A five-digit number is said to be friendly if: the leftmost digit is divisible by 1 and the leftmost two digits are divisible by 2 and the leftmost 3 digits are divisible by 3 and the leftmost 4 digits are divisible by 4 and leftmost 5 digits (the five-digit number itself) is divisible by 5. For example, the number 42325 is a friendly number: 4 is divisible by 1 and 42 is divisible by 2 and 423 is...
I want the code below to be edited: Rather than giving the input string inside the...
I want the code below to be edited: Rather than giving the input string inside the code, I want the program to ask the user for an input and calculate and complete this code. I have pasted the actual code below, Please edit the input section only so that I can input any string or any sentence as I like. The program must ask the user that "Enter a string/sentence" and take the data to calculate the Huffman code. #include...
Python Write a program that will analyse the string input and print “accept” or “reject” based...
Python Write a program that will analyse the string input and print “accept” or “reject” based on the pattern given Accept if it fulfils the following conditions -String length 9 -3 small alphabet (3 lowercase letter) -3 digits -3 big alphabet (3 uppercase letters) -1st alphabet should be a capital -Last alphabet should be a number -Two consecutive alphabets can't be small Reject if any of the conditions is absent So i want it to accept or reject my input,...
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 do I get the following code to accept a float input, while also fixing the...
How do I get the following code to accept a float input, while also fixing the syntax errors in the code because I can't figure out what is wrong with it. def chemical_strength(pH): if 0<pH>14: if pH <= 7: if pH = 7: ans = str("neutral") else: # ph < 7 if 0<pH>=1: ans = str("very strong acid") else: # 1<pH>7 if 1<pH>4: ans = str("strong acid") else: # 4=<ph>7 if pH = 4: ans = str("plain acid") else: #...
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 -/+
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
How can I write java program code that do reverse, replace, remove char from string without...
How can I write java program code that do reverse, replace, remove char from string without using reverse, replace, remove method. Only string method that I can use are length, concat, charAt, substring, and equals (or equalsIgnoreCase).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT