Hi

Suppose I have my program reading from a file with the following contents:

0.000,-0.073,-0.127,0.034,-0.034,0.146,0.098,0.029,-0.093,0.088,0.107
-0.005,-0.049,-0.112,0.039,-0.039,-0.059,-0.083,-0.020,-0.103,-0.068,-0.073
0.000,0.015,-0.049,0.010,0.308,-0.020,-0.054,0.225,-0.029,0.288,0.132
-0.005,0.020,0.068,0.088,-0.005,0.098,-0.044,0.098,0.073,0.098,-0.151
-0.005,-0.005,0.020,-0.015,-0.239,0.010,-0.024,0.181,-0.015,0.312,0.103
-0.005,-0.049,-0.044,0.044,-0.117,0.010,-0.054,0.244,0.034,-0.024,-0.103
0.000,-0.137,-0.122,-0.107,-0.601,0.093,0.020,-0.068,-0.205,-0.078,-0.039

and then I use StringTokenizer (delimiter ",") and feed the numbers into a vector.

File myFile = new File(strFileName);      //Pass the file content to strFileName
      BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(myFile))); 
      
      StringTokenizer st1 = new StringTokenizer (inputLine, ",");

      while ((inputLine = br.readLine()) != null) 
      {
        v.addElement(inputLine.trim());        //passes the content of the file to a vector
      }
      br.close();
      for (int i = 0; i < v.size(); i++)
      {
          System.out.println("Vector " + i +": " + v.get(i));   //returns content of the file as a Vector

I seem to be getting an error for the StringTokenizer(inputLine). Anyone know the problem?

My other question is how to extract the file into columns.
For example, the first column highlighted is one column and so on...

[B]0.000[/B],-0.073,-0.127,0.034,-0.034,0.146,0.098,0.029,-0.093,0.088,0.107
[B]-0.005[/B],-0.049,-0.112,0.039,-0.039,-0.059,-0.083,-0.020,-0.103,-0.068,-0.073
[B]0.000[/B],0.015,-0.049,0.010,0.308,-0.020,-0.054,0.225,-0.029,0.288,0.132
[B]-0.005[/B],0.020,0.068,0.088,-0.005,0.098,-0.044,0.098,0.073,0.098,-0.151
[B]-0.005[/B],-0.005,0.020,-0.015,-0.239,0.010,-0.024,0.181,-0.015,0.312,0.103
[B]-0.005[/B],-0.049,-0.044,0.044,-0.117,0.010,-0.054,0.244,0.034,-0.024,-0.103
[B]0.000[/B],-0.137,-0.122,-0.107,-0.601,0.093,0.020,-0.068,-0.205,-0.078,-0.039

Thanks in advance to those who helped. Appreciate it. =)

Recommended Answers

All 14 Replies

This statement must be inside the while loop.

StringTokenizer st1 = new StringTokenizer (inputLine, ",");

and if you want to represent each token as an element of Vector - Tokenize it.

while ((inputLine = br.readLine()) != null) 
      {
        StringTokenizer st1 = new StringTokenizer (inputLine, ",");
        while(st1.hasMoreTokens()) {
        v.addElement(st1.nextToken());       
         }
      }

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

StringTokenizer seems not to be doing the trick. Instead of parsing the rows of numbers and inputting each individual numbers into the vector, it inputs an entire row (without parsing it) into a vector
eg,
0.000,-0.073,-0.127,0.034,-0.034,0.146,0.098,0.029,-0.093,0.088,0.107
is inputted in a vector.

Anyone know the problem?

I'm going to try the advice offered by adatapost as well.

Update: so the stringtokenizer does work, I just needed to reboot my pc to see the effects. But now my problem is extracting those numbers into columns, I have no clue?

Use of StringTokennizer and Vector isn't recommended; use String.split and ArrayList as their alternatives.

What do you mean by "extract file into columns"? You can represent the entire file in-memory as a list of lists or a list of objects of your custom class where each object would represent one line of the text file. Retrieving the `n'th column would be just a matter of accessing the `n'th item of the list.

Well, my assignment requires me to use vectors, so I'm stuck using it.

by extracting, check post one for a better explanation.

All the lines consists of 11 elements each and I want to target and display a column of numbers. For example, I only want numbers from the 0 index in each line.

You'd need to make use of the knowledge that each line is 11 columns long. So every first number you read in goes in the first column and so on. You can use the modulus % operator to figure out which column things go in. If the number of columns in your file can vary, then you will need to count how many items are on the first line. You could do this a number of different ways - I'd personally go with reading the entire line into a Scanner Object, then figuring out how many were in that line from there.

Vectors can hold only Objects and not primitive types (eg, int). If you want to put a primitive type in a Vector, put it inside an object (eg, to save an integer value use the Integer class or define your own class). If you use the Integer wrapper, you will not be able to change the integer value, so it is sometimes useful to define your own class.

How would I go about doing that?

To make an Integer Object, use

int myInt = 0;
Integer myInteger = new Integer(myInt);

Also read about automatic boxing and unboxing. To make you own class that allows you to change the int value, it is extremely simple:

public class ChangableInteger{
int theInt = 0;
public ChangableInteger(int intPassed){
theInt = intPassed;
}
//Put your set method here, I'll leave it to you to write.
}

To make an Integer Object, use

int myInt = 0;
Integer myInteger = new Integer(myInt);

Also read about automatic boxing and unboxing.

For the past few hours I've tried your advice, but I keep getting disastrous results.

I'm not sure as to how to auto-box a bunch of values.

I have numbers separated by commas. Basically it's 11 columns long and 16384 lines long in one particular file.

Can anyone start me off? I don't want to have to input each manually. =S

Update: the autoboxing wasn't necessary, so I did the following

public void displaySummary (Vector vColumn, int nElectrodeNumber)
  {
      
      Hashtable returnedHash = new Hashtable();
      FastFourierTransform fft = new FastFourierTransform();
      
      
       for (int i = 0; i < vColumn.size(); i++)
      {      
         String number = "vColumn.get(i)";
         float newColumn = Float.valueOf(number).floatValue();
         
         returnedHash = fft.powerSpectrum(newColumn);
      }

But I get the following error:

powerSpectrum(float[]) in FastFourierTransform cannot be applied to (float)

I think to solve this, I would need to convert float to float[], but how would that work?

It expects an array of values. Why are you passing just one?

It expects an array of values. Why are you passing just one?

Is this the one I messed up with?

float newColumn = Float.valueOf(number).floatValue();

I'm not quite sure how to add the numbers into the array?

I'm sure google would be pretty helpful with that question.

int[] array = new int[10];
array[0] = 0;
array[1] = 1;
array[2] = 2;
array[3] = 3;
.
.
.
array[10] = 10; //ERROR! An array declared as having 10 can only be indexed at 0-9.

powerSpectrum(float[]) in FastFourierTransform cannot be applied to (float)

I don't have access to the API doc for the FFT code you are using, but I have to assume that it's declared that way because the logic requires that a complete set of numbers need to be passed to that method. If you are trying to pass only one number then I suspect that you have not properly understood what the method is all about. Simply putting your one value into a 1 element array will allow the code to compile, but I doubt that it will do anything useful.
Time to go back to the FFT class documentation?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.