• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

substring method

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all!

i have a string like that "Hello World" and i want to print only the "Hello" using only substring method.
But i have a problem with space.
Any help?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite sure I understand the problem. You need to work out exactly what you intend to do, and explain it.
Are you using substring() to split at the first space? How are you finding that space? Why not use split and "\\s" as a delimiter? If you look at the split documentation, you see you can limit the number of pieces it breaks the String into, but you would presumably wand the first element anyway.
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets explain it again.
I have this string "MODEL_ID Audi TT"
and i want to retrieve the "Audi" word.
In my code i have :



How to do it?
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone please?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use split() as suggested above. It automatically tokenizes and stores the result in an array. Then you just choose the index you want.

Also, please be patient. It's still Sunday in the United States. Two hours isn't long to expect someone to see your post.
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply!

But the setBrand() method returns String and not an array of Strings and so it has compile errors.

Another solution?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you've been given the solution twice already.

read the documentation and learn how to
use that solution.

if you still have problems, post what you've tried.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:Lets explain it again.
I have this string "MODEL_ID Audi TT" . . .

That is quite different from what you asked first. First you asked about the first “word” and now you appear to want the second word. You need to define exactly what you want, before you try to get it.
And don’t use StringTokenizer; it is a legacy class.
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got the point!
And how you suggest to retrieve the second word?
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How far have you been able to get using the suggestions above?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:You got the point!
And how you suggest to retrieve the second word?


You have an array, right? How do you get the second element of an array?
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i did this!



But it throws : Exception in thread "main" java.lang.NumberFormatException: For input string: ""
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:But it throws : Exception in thread "main" java.lang.NumberFormatException: For input string: ""


Well, that's because you're trying to convert something to an integer that isn't an integer. What's the value of str?

And why are you combining a StringTokenizer with String.split()? You use String.split() instead of StringTokenizer, not alongside it. And then adding substring(), which means you appear to be trying to use three ways of doing the same thing at the same time.
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally i did it! Thank you all for your replies!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how? That last code you posted looks very complicated, whereas it is really quite a simple task
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:


This will work, until the variable str is incorrectly formatted and their aren't 3 tokens in it.
This will raise a very unhelpfull exception (I don't know which one but know you would be back here asking what it meant).

A much better solution would be to use split, this returns an array (have you covered arrays yet, I would hope so)


now you can easily test how many tokens you have as each one is in a part of the array.
so in the array you now have

token[0] = "";
token[1] = make
token[2] = model

see how the array exactly matches onto your 3 variables token, token1,token2, but with a lot less code.
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it. Thank you!
 
BWA HA HA HA HA HA HA! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic