d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Question On Methods
Add Reply New Topic New Poll
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Oct 9 2013 09:40pm
I have a method that reads in names from a .txt file into an array.

The names are full names for example, one String in the array would be, "Billy Harold Jackson"

How do I take that array (in a whole new other method) and split those names up using a StringTokenizer?

The method that I need to split up these names is before the method that reads these names in.

Does that matter?

I am confused.

I did

StringTokenizer gin = new StringTokenizer (names, " ");

that doesnt work

names is the name of the array that the method read in.
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Oct 9 2013 09:43pm
THIS IS IN JAVA BTW
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 9 2013 10:30pm
Did you even read the API?

http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

Quote
The following is one example of the use of the tokenizer. The code:

    StringTokenizer st = new StringTokenizer("this is a test");
    while (st.hasMoreTokens()) {
        System.out.println(st.nextToken());
    }

prints the following output:

    this
    is
    a
    test

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.


Note the bold'd

This post was edited by carteblanche on Oct 9 2013 10:31pm
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Oct 9 2013 10:41pm
Quote (carteblanche @ Oct 9 2013 11:30pm)


yes, but i want to split each string in the array into tokens
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 9 2013 10:45pm
Quote (GetSpanked @ Oct 10 2013 12:41am)
yes, but i want to split each string in the array into tokens


so run it on each string in the array
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Oct 9 2013 10:48pm
Quote (carteblanche @ Oct 9 2013 11:45pm)
so run it on each string in the array


so like
StringTokenizer gin = new StringTokenizer (names[i], " ");
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 10 2013 07:20am
Code
String[] tokens = name.split( " " );


if name = Billy Harold Jackson

tokens[0] = Billy
tokens[1] = Harold
tokens[2] = Jackson

This post was edited by labatymo on Oct 10 2013 07:21am
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 13 2013 02:11pm
Quote (GetSpanked @ Oct 9 2013 11:48pm)
so like
StringTokenizer gin = new StringTokenizer (names[i], " ");


Yes. Or just use String.split instead.
Member
Posts: 36,244
Joined: Nov 29 2005
Gold: Locked
Trader: Scammer
Oct 15 2013 01:28pm
u could split the strings by the "/n" which i think is the new line character, and it may be different if you don't save the .txt file with notepad

This post was edited by the_rest on Oct 15 2013 01:30pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll