• Post Reply Bookmark Topic Watch Topic
  • New Topic

substring method  RSS feed

 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Anyone please?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
548
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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: 56570
172
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
You got the point!
And how you suggest to retrieve the second word?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12156
256
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How far have you been able to get using the suggestions above?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
548
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ok i did this!



But it throws : Exception in thread "main" java.lang.NumberFormatException: For input string: ""
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Finally i did it! Thank you all for your replies!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I got it. Thank you!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!