Concatenate 4 numbers into one number?

I’m trying to add 4 numbers together as a string but return them as a 4 diget integer how would I go about this?

Send data class


Scanner input = new Scanner(System.in);
		 int InputData;
		
		 System.out.println("Enter data to encrypt");
		 InputData = input.nextInt();
		
		 Incrypt EncryptedData  = new Incrypt(InputData);

Encrypt class

public int Encrypt()
	{
                        [COLOR="Red"] // Number comes in as 4567
		//Get numbers isolated to 4 5 6 7
		num1 = UserInt /1000+7;
		num2 = UserInt % 1000/100+7;
		num3 = UserInt % 100/10+7;
		num4 = UserInt % 10+7;
		
                          //Return them put back together
		UserInt = num1+num2+num3+num4;
		return UserInt;[/COLOR]	}

First a quick Java capitalization convention note:

Classes and only classes have the first letter capitalized. eg: MyClass.

Variables and methods begin with lower case, then follow standard ‘camel hump’ capitalization. eg: aVariable, someMethod.

We do this so things are immediately recognizable. To a Java programmer “MyVariable” is first considered to be a class, confusion ensues and extra time is required to decipher the actual meaning.

On to your question.

When designing an algorithm, first one must consider what the input will be and what the expected output should be.

Your comment notes that 4567 is the input, what is the expected output? Figuring this out is called a “desk check” and is the only reliable way to know if your code is working as expected.

If we desk check your code, we get:


4567 / 1000 + 7       = 11
4567 % 1000 / 100 + 7 = 12
4567 % 100 / 10 + 7   = 13
4567 % 10 + 7         = 14
11 + 12 + 13 + 14     = 50

But, I’m guessing that 11121314 is wanted? Hmm, no, you said you want a 4 digit number…you’ll need to clarify what you want before we can continue.

Thanks for the information. I do need to adhere to the standards as things get more complicated.

You are correct on the output.
11121314

I need to join those numbers not add them. Im guessing i need to add them like a string and then put them back into an integer. But how would you do that.

If you do this:

String userString = “” + num1+num2+num3+num4;

userString’s assigned value will be concatenated as 11121314, due to Java being forced to treat the operands as Strings, thanks to the empty String: “”

If you need to return it as an int use Integer.parseInt( userString )