• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Manipulating Vectors (code not working)

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry it's formatted so poorly, i didn't realise doing copy paste would do that...
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

at least i learned from my mistake (haha)


That's the best way to learn
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic