File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Putting Tokens into an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Putting Tokens into an Array" Watch "Putting Tokens into an Array" New topic

Putting Tokens into an Array

Anica Jovanova

Joined: Apr 03, 2003
Posts: 2
Please somebody help us with our project. We have 4 tokens of different data types and we are trying to put them into an array. How do we do that? There is a Person object with 4 parametars all different types: long, String, and int. We tokenized them and we are trying to put them into an array of this Person object. How do we do that?
Thanks a lot. Your help is really appreciated.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Anica,
Welcome to JavaRanch. If I understand right, you want to put an int, a long and a String in the same array, is that right? Well, you can't do that. What you can do is wrap the two primitives in Object wrappers and declare an Object[] array to store them like this:

Michael Morris

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Anica Jovanova

Joined: Apr 03, 2003
Posts: 2
Thanks a lot for the quick respond. OK, you have inseted the elements into the array manually, but we are reading them from a file using that bufferedReader thing. And once we do the StringTokenizer() method, to split te four types of data we have, we don't know how to insert them into an array of objects.
here is what we've done so far:
import java.util.*;
public class SimpleStringToken {
public static void main(String[] args) throws IOException {
File inputFile = new File(args [0]);
FileReader in = new FileReader(inputFile);
BufferedReader d = new BufferedReader(in);
String line;
while ((line = d.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
long id = Long.parseLong(st.nextToken());
String last = (st.nextToken());
String first = (st.nextToken());
int age = Integer.parseInt(st.nextToken());
System.out.println("id = " + id
+ " last name = " + last
+ " first name = " + first
+ " age = " + age);
And we have also created a Person object having a constructor like this:
Person(long id, String last, String first, int age)
Does the question make sense now?
Thanks a lot, I'm really happy to have found this site today.
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

I don't really know exactly what you need, but I'm just guessing you want an array of Person with attributes that are read from a file.
From your code I'll say it's easier to use ArrayList.
So you'll have:

Hope that's what you need.
I agree. Here's the link:
subject: Putting Tokens into an Array
jQuery in Action, 3rd edition