aspose file tools*
The moose likes Beginning Java and the fly likes Manipulating Vectors (code not working) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Manipulating Vectors (code not working)" Watch "Manipulating Vectors (code not working)" New topic
Author

Manipulating Vectors (code not working)

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I'm trying to read data from a text file and manipulate a vector accordingly. The first two digits correspond to row and column of the vector to be created. Each number afterwards is supposed to be stored, each string skipped (producing an error message), and in the case of 6*-7 (stores -7, 6 times). The only thing i'm getting out of my code is a vector containing all zeroes. None of the integers are being stored, nor is it handling the special case, or producing an error message when it encounters a string. You can compile the following code in an editor, it's a working example (well not technically a "working" example). This is the first time i've worked with Tokenizer, and thats the part which i think is screwing things up.



Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Here are a few tips to begin with

1. You're not reading the line at the right place. I think you should read the line like:
while ((line = input.readLine()) != null)
2. You have to rebuild a new tokenizer each time
tokenizer = new StringTokenizer(line);

I didn't look at the logic where you put values in the array
(we usually don't call [][] vectors, but two-dimension arrays)


[My Blog]
All roads lead to JavaRanch
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
#1. ((line = input.readLine()) != null) is exactly what i have (written in a diffrent fashion). I have line = input.readLine(); and then underneath it I have the while loop using line as a condition.

#2.tokenizer = new StringTokenizer(line); I have that line of code directly underneath my while loop, so that's exactly what happens every time.

I appologise for the confusion, i meant two-dimensional arrays.

The biggest problem i have is creating the two dimensional array based on the first two tokens from the file (5 and 3), then reading the rest of the tokens and storing them.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Ok, forget about #1, as I got a bit confused

About #2, you still need to rebuild a StringTokenizer after reading a line.
If I were you, I'd the other way around :



Tips: if you can't use a debugger, add a few println here and there to see what your program is doing.
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
ya i noticed that, i strung together the program and compiled it, made a few modifications after I noticed how I wrote it. That was one of the modifications. After using the print statements, all of my tokens come out fine. Now i'm confronted with, using the first two ints as parameters for my two dimensional array. and then storing the rest of them (dealing with strings and the case i mentioned). I'm not sure whats wrong with the code after the while(x<matrix.length-1).
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Here is a sample to put the numbers into the array.
As this sounds like an assignment, I'll leave you deal with the special tokens. If you use a few println, you should be able to figure it out

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I've almost got it working, but i'm having difficulty with my coordinate system x and y. The way you wrote it matrix[y][x++] is the same way i did meaning x will always start from 1 not 0. It needs to start from 0,0 and proceed filling all the columns (if max column met, increase row and restart column). And if a string is encountered, skip it, and store a value in its place in the matrix. Or if the special case encountered, store that value n times.
Mike Mc Afee
Greenhorn

Joined: Jan 31, 2007
Posts: 19
Originally posted by John Lockheart:
I've almost got it working, but i'm having difficulty with my coordinate system x and y. The way you wrote it matrix[y][x++] is the same way i did meaning x will always start from 1 not 0. It needs to start from 0,0 and proceed filling all the columns (if max column met, increase row and restart column). And if a string is encountered, skip it, and store a value in its place in the matrix. Or if the special case encountered, store that value n times.


Since x++ would increment x after it's evaluated for matrix[y][x], I would have thought it would have stored at 0,0 and then x would be incremented to 1.
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I didn't realise that, that's good to know. I actually wrote it slightly diffrent so thats probably why I had a problem. Here's the code I have so far. When it runs into a string and since it's not storing it, it will decrease the index value (if y=#of columns decrease y, if y=0 reset y and decrease rows) x=rows, y=columns. I'm supposed to get the following output. Also if a string or special case(with strings) is encountered, i can't just skip ahead index values and store a null value so I have to decrease if that happens.



John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
sorry it's formatted so poorly, i didn't realise doing copy paste would do that...
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
NEVERMIND. first time using try and catch statments, didn't realise there was absolutely no need to DECREASE columns and rows because it will try it, and if it catches it, columns and rows won't be increased in the first place. I'm sorry guys...at least i learned from my mistake (haha). Thanks for all the input though.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

at least i learned from my mistake (haha)

That's the best way to learn
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[John Lockheart]: sorry it's formatted so poorly, i didn't realise doing copy paste would do that...

For what it's worth, you can edit your own posts after you post them by clicking on the at the top of a given post.
[ February 28, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Manipulating Vectors (code not working)