Separate Java Ints from each other?

Hello everyone

I'm currently writing a program in Java and am faced with the challenge of having an int, say 35, and wanting to split it into two ints: 3 and 5. These numbers should then be stored in separate ints. Is there a solution, such as a method, for this? Best regards

PS: Statements like "nobody uses Java anymore" or "program in another language" are out of place here. Thanks.

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
3 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
MrAmazing2
2 years ago
int x = 35;

int a = x/10; // = 3
int b = x%10; // = 5

In the case of parts by 10, the rearmost point is cut away, from which 35 only the 3, which is then assigned a, remains. (Actually 35/10 would be yes 3.5, but because the integer (= integers) the rest behind the comma is simply falling away)
At Modulo 10 exactly the opposite is made, namely the rearmost point, i.e. 5, is transferred.

RoadRage3
2 years ago

Hello,

here are some possible solutions for your problem. Depending on how you want to program, you can choose one of them.

https://stackoverflow.com/questions/24641882/is-there-a-function-in-java-that-works-like-substring-function-but-for-integers

Ben

gfntom
2 years ago

I don’t know Java well enough, but in general:

Int by 10 parts (Result as integer) -> you have your 3 (int1)

Int – 10 * Int1 -> your 5