StringTokenizer and substring method

src des cos

011

022

111

123

while(hasMoreTokens)

{

src = nextToken;

des = nextToken;

cost = nextToken;

String a = src + des + cost;

}

int [] [] array = new int[ i ] [ j ]

Since I'm using tokens, I just want to take the cost and put it in a 2D array.

Can I use the substring on a.substring (2,3) to get the cost and assign it to the array?

[432 byte] By [javaeta] at [2007-11-26 15:23:38]
# 1
why bother concatenating unrelated data, only to tokenize it again?
georgemca at 2007-7-8 21:38:54 > top of Java-index,Java Essentials,New To Java...
# 2

> src des cos

> 011

> 022

> 111

> 123

>

> while(hasMoreTokens)

> {

> src = nextToken;

> des = nextToken;

> cost = nextToken;

> String a = src + des + cost;

> }

> int [] [] array = new int[ i ] [ j ]

> Since I'm using tokens, I just want to take the cost

> and put it in a 2D array.

> Can I use the substring on a.substring (2,3) to get

> the cost and assign it to the array?

You already have each individual cost in the cost variable as you loop; why not put them in the array at that time. If you do not know how many there will be before hand, use an ArrayList.

jbisha at 2007-7-8 21:38:54 > top of Java-index,Java Essentials,New To Java...
# 3

This looks to be a graph definition. You can split each line using

String[] splitLine = line.split("\s+");

Then

int src = integer.parseInt(splitLine[0]);

int des = integer.parseInt(splitLine[1]);

int cos = integer.parseInt(splitLine[2]);

You can then use value these to populate your cost matrix.

sabre150a at 2007-7-8 21:38:54 > top of Java-index,Java Essentials,New To Java...