programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Finding biggest number in a string

Jiyoung Kim
Greenhorn
Posts: 15
I'm trying to get the biggest number in a string that is contained in numbers and operators randomly.
For example, -123456789 will be (98765432-1), 1234567890 will be (9876543210), 123*456+7 will be (765*432 + 1), and so on.
However, I have no idea how to split the operators and the numbers and find the operation that makes the biggest numbers.

This is a method to find and calculate the number.
And, what I'm trying to do is to make separated between operators and numbers using ArrayList above.
But, is it available to make the operations in this approach? I guess that I need to find the operations in a string without a split.
I know how to organize the numbers from the highest to the lowest in a string like below.

The quetsion is kind of messy, but could you provide some instructions how to approach?

Input
12356789
-123456789
123*456+7

Output
987654321
987665432-1 = ?
(765*432)+1 = ?

Campbell Ritchie
Marshal
Posts: 56584
172
Divide and rule. That question looks like something which shou‍ld be broken down into parts. Start by describing how you would work out the largest number formed from the digits 1234567890. Also look at this Unicode link to remind yourself how the ten digits 123456890 are represented in ASCII: do the ASCII numbers an the values of the digits always run in the same order?

Junilu Lacar
Sheriff
Posts: 11494
180
Why would an input of 123*456+7 give an output of 765*432+1? Following normal math operator order of precedence (MDAS), the output expression evaluates to 330481.  However, 7*65432+1, which uses the same digits and operations, will evaluate to 458025, which is greater.

What exactly are the rules you need to follow to determine "largest number formed from the digits"? It seems to me that it's not only just the digits that you have to use but also the operations that would affect how large the number is that you can get.

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Jiyoung Kim wrote:I have no idea how to split the operators and the numbers and find the operation that makes the biggest numbers.

I would suggest you stop worrying about Java entirely until you have this worked out on paper. This is really the crux of the issue. Once you know how to do this, writing the code is the easy part.

Vasyl Lyashkevych
Ranch Hand
Posts: 70
It seems like a sorting task. 1568 - > 8651. You can use different algorithms for that. The easiest is when we use two cycles, one is included in other. Before you need to convert string to int but you need to check token "-".