Two Laptop Bag
The moose likes Performance and the fly likes Tokens into array, datatype?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Tokens into array, datatype??" Watch "Tokens into array, datatype??" New topic

Tokens into array, datatype??

Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
hi. could some one help me. I am trying to cast the the tokens into the array but the datatypes are incompatible. what should i do to cast it into the same thing. the numbers from my text file are actually floats. But i can make do with just the integers if it aint possible.

import java.util.*;

public class BasisGenesis {

//Mu (the model structures)

public static void main(String[] args) //main method

//File Directory //path (java programme in same folder as directory)

File dir = new File("1a00.gz.txt"); //set file
String path = dir.getAbsolutePath(); //get file path
int lala = path.lastIndexOf("\\"); //get index of '\'
String howPath = path.substring(0,lala+1); //extract directory name
File usePath = new File(howPath); //assign directory for reading file
String[] fileDir = usePath.list(); //store file in dit to array
if(fileDir == null) //check if dir exists
System.out.println("Directory Does Not Exist");
for(int j=0; j<fileDir.length;j++)

String fileName = fileDir[j];


int count = 0;


BufferedReader in = new BufferedReader(new FileReader(fileDir[j])); //reading files in specified directory
//no need writer

String str;

while ((str = in.readLine()) != null) //file reading

StringTokenizer s = new StringTokenizer(str," "); //tokenize string
int counter=0; //every line has 2 tokens
String line="";

//2D intialising
int row = 0;
int col = 0;
int [][] array = null;


String ss = s.nextToken();

//read into 2D array

if (counter==1)

row = Integer.parseInt(ss);//take the first line and put into the row:


else if (counter == 2)
//take the second line and put it into the col:

col = Integer.parseInt(ss);

array = new int[row][col];


}//end of while
System.out.println(line); //must print outside the while loop.
line +="\0";

}//end of while
//closing of writer
in.close(); //closing of reader

}//end of try
catch (IOException e) { }


}//end of for
}//end of else

}//end of main method =============================================================================================================================

thanks much!!!

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Normally I would really try to help you out because your code is a mess and you didn't format it or use the code tags. Please UseCodeTags. But I tried it anyway to get some experience with other peoples code and refactoring.

Some remarks about your code:

Some comments are useless:
public static void main(String[] args) //main method
What does the comment add?
If you have a hardcoded filename then you know the directory thus no need to search for it. Just use new File(".");
Use meaningful variables names. lala for the last index of \ is not really explanatory.
Some variables are not used: fileName and count for example.
Break up long methods into smaller methods.
Tokenizing the entire String is a bit overkill if you just want to split the String.
The printing of variable line will always print an empty String.

Resulting in this code:

Then there is the problem that I don't really understand your problem. You say that the contents of the files are floats. But you can't use floats as the size of an array. It doesn't make sense. You can't have an array with size 1.5. You have a wrong understanding of arrays or you're not explaining yourself in a way that I can understand you correctly. Do you mean that you want to store the float values in an array? Because that would make sense.

Good luck.

// Edit: why is this in the performance category?

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
Yup. After extracting the coordinate values from the text file i would have to store them in a 2D array..

there is no comma, just a whitespace in between two coordinate values of each line.

first 9 lines go like this,...

0.0 0.0
0.0 0.0
101.601 38.534
103.062 38.513
103.354 38.323
103.025 37.252
103.8 37.438
105.274 37.822
103.166 37.064

1)I am trying to use a Double.valueOf(String X) to cast the string into a double value.

but the runtime error states "empty string.. number format exception.. Floating Decimal. I got it checked out and its a problem with the leading and trailing spaces. but i cant seem to accomplish running it though i have added

Is there any way to overcome this problem. maybe by throwing a number format exception?

2) I have to use a directory because i am running this programme through a database of files.

3) Ok. I have noted that I shouldnt be using the StringTokenizer but just a split String triggered by the whitespace right. Cool.
My apologies, my previous text file extraction was done by the StringTokenizer, I thought i could recycle the idea.

and also noted that the variable line is an empty string.

And I'm sorry my code is confusing you.. I am doing a very long algorithm.. so i only took out the parts that I wanted to debug, thus the unused variables, and the "lala" was done by my group member on another programme that I incorporated.

Thank you for your help/advice.

p.s. I'm sorry for the category error!!!

I agree. Here's the link:
subject: Tokens into array, datatype??
It's not a secret anymore!