Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Need assistance

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I have a string "sgdgs34dfjg123hfgj7" which has 3 number groups 34,123, and 7. I need to write a program to find the largest number in the string which is 123.

Can anyone help me with this problem?
 
Saloon Keeper
Posts: 6798
162
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you familiar with regular expressions? If so, you can split the string with a regexp like "[^0-9]+" to get an array of the contained numbers. That would be a good starting point.
 
Mui Goku
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, I am not that knowledgeable in regular expressions. So I have to first know about regular expressions to solve this problem?
 
Marshal
Posts: 71682
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find out about regular expressions (often abbreviated to regex) in the Java™ Tutorials. The simplest regex to match integers is probably [0-9]+ and the simplest regex to match everything else is probably what Tim showed: [^0-9]+
What Tim showed allows you to divide the String into an array matching numbers. Check the documentation and see whether there is any risk of the array starting or finishing with an empty (=zero‑length) String.
There is a Scanner method Scanner#findInLine() which looks for the first match to a particular regex. There are also methods in the regex classes (probably Matcher) to produce a Stream<Something> from all the regex matches in the text.

Another way to do it is to iterate the text char by char. Start assembling a number whenever you reach a char in the range '0'...'9'(inclusive) and stop when you encounter anything different. But that isn't really a good technique because it is using low‑level data access.
 
Saloon Keeper
Posts: 23005
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a reminder  to all our dispensers of wisdom: this is the Beginning Java forum. Some would argue that introducing regexes at this level is a potential violation of the Geneva Conventions.

We'd probably all use regexes, but for a beginner, I'd probably recommend doing a character-by-character scan. It's probably not that much slower - and maybe faster - than using a regex here anyway. The regex processor is a finite-state machine (VM) running a compiled regex as its program and it's required to allow for all sorts of things that aren't in this particular problem.
 
lowercase baba
Posts: 12954
65
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mui Goku wrote:So I have to first know about regular expressions to solve this problem?


No.  A regular expression (or "regex") is ONE way to solve this problem, but there are plenty more. Campbell pointed out one, where you iterate character by character.
You could build every possible substring with a couple of loops, and test each to see if each is numeric, and if so, save the largest.
You may be able to find a webservice call that does exactly what you want, and all you'd have to do is connect, pass the string, and get a result.  (this is unlikely to exist, but is theoretically possible).

Remember that programming is not about typing, but about thinking. Think about how you, personally, with pencil and paper, would tackle this problem. Break it down into steps, then break each of THOSE steps down into simpler steps...and keep repeating this until you have a very detailed list of directions. THEN start trying to covert that into code.
 
Tim Holloway
Saloon Keeper
Posts: 23005
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A web service???

That's DEFINITELY overkill here.

Here's a simple approach:

1. set a high-watermark value for your result. -1 will do for a start, since no mention was made of signed numbers here.

2. Start examining the string character-by-character. As long as isDigit() is false for the character, ignore it.

3. Once you reach a digit, convert it to a binary working number.

4. For subsequent digits, multiply the results of the working number by 10 and add the binary value of the digit. This is exactly how the built-in ASCII/Unicode-to-to-int functions do it.

5. Once you've reach the end or a non-digit, compare the working number to the current value of result. If it's higher, replace result with this new value.

6. Loop back to step 2 until you've processed all characters.
 
Campbell Ritchie
Marshal
Posts: 71682
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:A web service???

That's DEFINITELY overkill here. . . .

Not if we write one
So everybody seems to be advocating what I dismissed as too low‑level
 
Tim Holloway
Saloon Keeper
Posts: 23005
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Tim Holloway wrote:A web service???

That's DEFINITELY overkill here. . . .

Not if we write one
So everybody seems to be advocating what I dismissed as too low‑level



Several years ago, my boss bought a brand-new Porsche. The dealer made her take a driving course on their private track. Presumably because the beast had so much horsepower (probably as much as a modern-day Hinda Civic ) that if you sneezed at a traffic light, you'd find yourself in the middle of a busy intersection. Then again, it could have been getting her used to turbo lag, if it was anything like my T-Bird. I used to be paranoid because if there was a gap in traffic  I'd stomp the gas, it would wind up, and then take off like a shot right as some clown with a more linear acceleration decided to switch lanes.

Point being, sometimes it's best to start with the basics before using the fancy accessories. If nothing else, it gives you an appreciation of what kind of work went into those accessories. And maybe some insight as to how they work and how they behave.
 
fred rosenberger
lowercase baba
Posts: 12954
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:A web service???

That's DEFINITELY overkill here.


I was not seriously suggesting anyone take that approach.  My (apparently poorly made) point was that there is not a single, right way to do it.  There are many, many ways to solve this problem, and those were just some of the first few that came to mind.
 
Bartender
Posts: 2777
134
Google Web Toolkit Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP
This looks like a homework assignment.
We have suggested two approaches so far (regex, character by character iteration) do you know how to proceed with any one of them ?
If yes, can you share your code ?
 
Mui Goku
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know about the regex, but I am trying to code with character by character iteration. I will share the code in a few days regardless of being correct or wrong.

I still want to try solving it on my own.
 
salvin francis
Bartender
Posts: 2777
134
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mui Goku wrote:I still want to try solving it on my own.


That's lovely !!  

Mui Goku wrote:... but I am trying to code with character by character iteration. I will share the code in a few days regardless of being correct or wrong.


Rather than writing code, you can also share your thought process about how you are going to solve this problem. Draft your ideas on a piece of paper, you can share your pseudo code here.

 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic