2

I need to split a string that is "Kermit D.Frogge", so this is the code I used:

firstName = strTkn.nextToken();
middleInitial = strTkn.nextToken("."); 
//changing the delimiters to a . because there is no space between D and Frogge
lastName = strTkn.nextToken(" "); 
//changing delimiters back to a space
hourlyWage = Double.parseDouble(strTkn.nextToken());

However, the result is:

Kermit
D
.Frogge

How would i use string tokenizer and not keep the period?

Roman C
  • 49,761
  • 33
  • 66
  • 176
xSpartanCx
  • 281
  • 3
  • 10
  • 25

4 Answers4

3

Use the constructor with two arguments. The second argument is a string whose characters are delimiters.

StringTokenizer st = new StringTokenizer(string, " .");
max
  • 4,248
  • 2
  • 25
  • 38
1

One posible solution

strTkn.replace("."," ");
String[] name=strTkn.split(" ");
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
0

If his name were "Kermit D. Frogge" (note the space between the middle initial and the last name), then his first name would be "Kermit", his middle initial would be "D.", and his last name would be "Frogge".

You could then use the replaceAll() method to get rid of "."s.

dshapiro
  • 1,075
  • 14
  • 24
0

Should use the delimiter set on middle token

String middleInitial = strTkn.nextToken(" .");
Roman C
  • 49,761
  • 33
  • 66
  • 176