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

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.
For this question, you need to implement Java code for the following Modified Ciphertext encryption and...
For this question, you need to implement Java code for the following Modified Ciphertext encryption and attack for known patterns to find the code. Please read the modified Ciphertext carefully and examples before implementing them.   Modified Ciphertext is working as follows: If a plain text (only letters ignore uppercase or lowercase) and sequences of numbers are given, it encrypts the given plain text by shifting each letter in a message the characters based on the given numbers one forward direction...
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,...
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 + "]" );...
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: #...
JAVA Write a program that will accept user input for an initial deposit and a total...
JAVA Write a program that will accept user input for an initial deposit and a total amount the user wants to have, and will output the number of years it will take to reach his/her goal. For the basic program, the user will deposit the initial amount in a new account, and then the account will receive interest, compounded MONTHLY, at a rate of 0.5%.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT