简体   繁体   中英

How can I calculate the difference of chars between two strings and make them equal in Java?

I'm starting Java at school and I was given this exercise where I'm stuck. I have two numbers and I have to find the difference of chars between these two, and to make it equal in number of chars I have to add many zeros as the missing chars. So for example if I have 10 and 100 the output will be 010 and 100. I'd like to have support on understanding how, having the string.length of these two numbers I can detect the chars missing and add the 0.

import java.util.Scanner;

public class exercise {

    public static void main(String[] args) {
        Scanner myObj = new Scanner(System.in);
        System.out.println("Enter n1");
        System.out.println("Enter n2");


        String n1 = myObj.nextLine();
        String n2 = myObj.nextLine();
        System.out.println("n1 is: " + n1);
        System.out.println("n2 is: " + n2);
        System.out.println("length n1: "+ n1.length());
        System.out.println("length n2: "+ n2.length());

    }

}

I'm not going to give you the solution code, but I will provide you with information about the String.length method with context.

Suppose you have two variables that are Strings:

n1 = "18214120"
n2 = "100"

n1.length() will yield 8 and n2.length() will yield 3. With this information you are aware that n1 is the number which contains more characters and you want n2 to be padded with 0's until n2.length() = n1.length(), how many zeros will you add to left of n2?

Well the number of zeros to pad must be equal to:

  n1.length()-n2.length() 

8-3=5.

So adding 5 0's to the left of n2, will make it 00000100 which I believe is what you're after.

get the missing chars

int charsMissing = n1.length() - n2.length();

add extra zeroes as many times as its missing.

if (charsMissing > 0) // check if there actually are missing in  {
  for (int i = 0; i < charsMissing; i++) {
  n1 = "0" + n1
  }
}

Important to know when changing String objects is the fact you can use the + operator to concatenate two strings together. For example:

String s1 = "hello";
String s2 = "world";
String combined = s1 + s2;
System.out.println(combined);

This will output:

helloworld

If you want to prepend the number 0 to a String, you can thus use this code:

String zeroAdded = "0" + someString;

Because you don't want to create new Strings for every zero you add, Java allows you to concatenate a String and assign the result to itself. The example above can also be written as:

String someString = "0" + someString; //The value of someString will be overwritten with the result of the concatenation.

Now all you need is to repeat this step X times, where Xis the difference in length. a for loop or a while loop can be used for that. Which one to pick is a whole topic on its own, I suggest googling these terms to get examples and comparisons. For these kind of exercises though the difference in performance or readability between is negligible. Here's an example of a for loop:

int difference = Math.abs(n1.length() - n2.length()) //Taking absolute value, because we don't know which string was the longest.
for(int i = 0; i < difference; i++){
    if(n1.length() > n2.length()){ //n1 is longer, so prepend the 0 to n2
         n2 = "0" + n2;
    }
    if(n1.length() < n2.length()){ //n2 is longer, so prepend the 0 to n1
         n1 = "0" + n1;
    }
}

System.out.println("n1 is now: " + n1);
System.out.println("n2 is now: " + n2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM