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...
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.
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,...
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...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT