Token arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Token arrays

  1. #1
    Join Date
    Nov 2004
    Posts
    14

    Token arrays

    Is there a way to make an array of tokens from StringTokenizer? If so, how would I use it in a for or while loop to increase the size of the array.

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    could you give an example of what you want to do, sorry im a bit confused
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Nov 2004
    Posts
    14
    I am trying to read in 2 lines from a text file. The first line is the numbers of the dogs who came in 1st-4th place. The second line is the number of each dog and its breed(i.e. 24 Collie 9 Beagle). I want to know the easiest way to compare the numbers on the first line to the number and breed on the second line and output the rank and breed in order from 1st-4th. I was thinking an array of tokens gotten from using StringTokenizer might be a shorter way of doing this.

  4. #4
    Join Date
    Sep 2004
    Posts
    223
    the String class has a split() method which will return an array of strings equal to the number of strings in the line seperated by a specified delimeter.

    ie, if you read in the line of the text file into a varaible called "line" you could do:

    stringArray = line.split(" ");

    where stringArray is an array of strings. The only problem i can see is that the number of elements in the stringArray may have to be calculated first. Becuase once you define an array, you cannot change its size.

    so I would firstly count the number of spaces in the line, then create the stringArray with the number of spaces as the size. Then use the split() method.

    Once you have the array defined, do the same with the second line, and then compare elements in both arrays.

    does this help
    A kram a day keeps the doctor......guessing

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    This is the easiest solution I could think of, there is no checking on valid input, if you dont have the dogs and rank line in sync, it just dies...

    import java.util.*;

    class Dog implements Comparator {
    public int number=-1;
    public String breed=null;
    public int rank=-1;
    public Dog () {}
    public Dog (int number, String breed) {
    this.number=number;
    this.breed=breed;
    }
    public void setRank(int rank) {
    this.rank=rank;
    }
    public String toString() {
    return "rank: "+rank+"\tnumber: "+number+"\tbreed: "+breed;
    }
    public int compare(Object o1, Object o2) {
    Dog dog1=(Dog)o1;
    Dog dog2=(Dog)o2;
    return dog1.rank-dog2.rank;
    }
    public boolean equals(Object obj) {
    Dog aDog=(Dog)obj;
    return aDog.rank==this.rank;
    }
    }
    public class DogRun {
    public static String [] lines={ // a mock file...
    "9 12 7 2",
    "2 Collie 12 Pitbull 9 Mutt 7 Wolf"
    };
    public static void main(String[] args) {
    DogRun dogRun1 = new DogRun(lines);
    }
    public DogRun(String [] lines) {
    ArrayList dogList=new ArrayList();
    Hashtable ht=new Hashtable();
    String [] ranks=lines[0].split(" ");
    String [] numBreed=lines[1].split(" ");

    // make dogs
    for (int i=0; i<numBreed.length; i+=2) {
    Integer number=new Integer(numBreed[i]);
    String breed=numBreed[i+1];
    Dog aDog=new Dog(number.intValue(),breed);
    ht.put(number,aDog);
    dogList.add(aDog);
    }
    // rank dogs
    for (int i=0; i<ranks.length; i++) {
    Integer number=new Integer(ranks[i]);
    Dog aDog=(Dog)ht.get(number);
    aDog.setRank(i+1);
    }
    // sort ranked dogs
    java.util.Collections.sort(dogList,new Dog());
    // print results
    for (int i=0; i<dogList.size(); i++) {
    System.out.println(dogList.get(i));
    }
    }
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center