Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Doubt

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI ,
can anyone help me regarding this java program For example, s="12 some text 3 7", result: 22 (12+3+7=22)

i want logic for this program please help me...
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Split the entire String into a char array. Convert each character into its numeric equivalent and add up all the numbers.
 
Roopa Bhuvaneswari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Convert each character into its numeric equivalent ..it is not clear can you please explain me clearly
 
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roopa Bhuvaneswari wrote: . . . i want logic for this program please help me...

You will have to work out the logic for your programs yourself, I am afraid. Write it down on paper, then you can see what you are doing. And are yoiu sure that

"12 some text 3 7"

. . . actually gives 22 by any straightforward means?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't (easily) do it character by character, because of multi-digit numbers (e.g. 12).

Once thing you could do is use String.split() to split on non-numeric characters. That will give you an array of strings that can easily be converted to a number. That would work as long as all the values you're looking for are non-negative integers with simple formatting. If you need to cope with decimals, or with negative numbers, then the problem gets quite a bit more complicated.
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to convert the Strings "12", "3" and "7" into numbers (ints) 12, 3 and 7. java.lang.Integer has a nice static method for this; check out the class' Javadoc page. Be aware that you should catch the exception that will be thrown when you pass "some" or "text" to this method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic