In: Computer Science
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of x followed by the second-to-the-last digit of y. And so on.
You should write weaveNumber method that works as following examples weaveNumber( 271 ,389).should return 237819. If one of the numbers has more digits than the other, you should imagine that leading zeros are used to make the numbers of equal length. For example, weaveNumber(2384, 12) should return 20308142 (as if it were a call on weaveNumber(2384, 0012)). Similarly, weaveNumber(9, 318) should return 30198 (as if it were a call on weaveNumber(009, 318)).
Complete java program:
import java.util.Scanner;
public class WeaveNumberFinder {
public static void main(String[] args) {
//Scanner class object to read
inputs
Scanner scan = new
Scanner(System.in);
//asking inputs
System.out.print("Enter first
integer:");
int num1 = scan.nextInt();
System.out.print("Enter second
integer:");
int num2 = scan.nextInt();
//calling weaveNumber()
method
int weaveNum =
weaveNumber(num1,num2);
System.out.println("Weave
number:"+weaveNum);
}
//method implementation
public static int weaveNumber(int num1,int num2)
{
//convert two numbers to two
different strings
String numStr1 =
String.valueOf(num1);
String numStr2 =
String.valueOf(num2);
StringBuilder sb1= new
StringBuilder();
StringBuilder sb2= new
StringBuilder();
String finalString = new
String();
int diffDigits=0;
if(numStr1.length() >
numStr2.length()) {
//find out the
difference of digits
diffDigits =
numStr1.length() - numStr2.length();
//append
diffDigits number of '0' to the sb2
for(int
i=1;i<=diffDigits;i++) {
sb2.append("0");
}
//append all
digits of num2 to sb2
for(int
i=0;i<numStr2.length();i++) {
sb2.append(numStr2.charAt(i));
}
//preparing
final string-concatenating both strings
for(int
i=0;i<numStr1.length();i++) {
finalString = finalString +numStr1.charAt(i)+
sb2.charAt(i);
}
}else if(numStr2.length() >
numStr1.length()) {
//find out the
difference of digits
diffDigits =
numStr2.length() - numStr1.length();
//append
diffDigits number of '0' to the sb1
for(int
i=1;i<=diffDigits;i++) {
sb1.append("0");
}
//append all
digits of num1 to sb1
for(int
i=0;i<numStr1.length();i++) {
sb1.append(numStr1.charAt(i));
}
//preparing
final string-concatenating both strings
for(int
i=0;i<numStr2.length();i++) {
finalString = finalString+
sb1.charAt(i)+numStr2.charAt(i);
}
}else {
//if both
numbers have equal number of digits
//preparing
final string-concatenating both strings
for(int
i=0;i<numStr1.length();i++) {
finalString = finalString+numStr1.charAt(i)+
numStr2.charAt(i);
}
}
//converting string to int and
return the same value to main
return
Integer.parseInt(finalString);
}
}
Output: