• 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

finding the character after a specific word in a String

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have a string "3.100.2.100" and I have to read the immediate value of the character which is after the 3rd DOT(.) character.



regards
Prashanth
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what have you tried so far? And what problems are you running into?

Henry
 
Prashanth Chandra
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Tried the below. I am able to capture the count of the character but not able to get the word after the required character
<code>

String name = "3.100.2.100";
char pattern='.';
int occurs=0;
for(int ipAddr = 0; ipAddr < name.length(); ipAddr++)
{
char next = name.charAt(ipAddr);
if(next == pattern)
{
occurs++;
if (occurs==3)
{
System.out.println("Testing::"+next);
}
}
}

</code>


regards
Prashanth
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you definitely found the beginning of the word. Now you just need the code to extract the word. Do you have any ideas? Hint: The charAt() method is still useful here.

Henry
 
Prashanth Chandra
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am not very sure about that. can you please help me out on that.


regards
Prashanth
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you updating the "next" variable?
 
Prashanth Chandra
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
No I am not updating the next value but I am just checking if the value after the DOT is 2 or 1.


regards
Prashanth
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prashanth Chandra:
Hi
No I am not updating the next value but I am just checking if the value after the DOT is 2 or 1.

regards
Prashanth



Hint: maybe you should update the next value... It is pointing to the DOT, and you want it to point to the "value after the DOT".

And BTW, when we say "updating the next value", we don't mean updating the value pointed to by next. We mean update the value of the next variable pointer.

Henry
[ September 23, 2008: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I wrote a little function to get the name of a file from the whole path.
i modified it a little to suit your requirements. you can call the below function by passing the "." separated string along with "." as the delimiter

now, you can call this function like :
String fileName=getFileNameFromPath("3.100.2.500",".");
Hope this helps!
Thanks!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String.split would be a better alternative to StringTokenizer. All you need to remember is that . is a regular expression special character so it will need to be escaped:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic