• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading in a file with BufferedReader, using Tokenizer for adding into an adjacency matrix  RSS feed

 
mayumi yamada
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am having trouble adding numbers read from a file with BufferedReader, using Tokenizer to split the numbers up by " " - space and adding them to an adjacency matrix.
Thanks for your help in advance!

Below is the text file and my code, that I have at the moment.







this is the output without the two for loops putting the tokens into the adjMatrix


output with the for loops commented out
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can make this a lot simpler by not using StringTokenizer, but by just using line.split("\\s+") to split the values on a line.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 36-40 attempt to get the next x*j tokens when you are already pulling out the next token on line 31. You might want to make 36-40 your main loop and do away with the while() loop.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is in this line:



You've already set token = tokenizer.nextToken();. The .nextToken() method moves the pointer. So what should the above line be?
 
mayumi yamada
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So SORRY for the extremely late replies...I had a death in the family and didn't take it very well. But back to normal now. Thank you so much for the replies and suggestions!

@Jesper de Jong: how would I go about doing a split() so it can be in the adjMatrix

@Carey Brown: thanks I will try that

@Knute Snortum: ah, thank you for pointing that out! That should be just



I'm also looking for a split() since Jesper suggested that it would be better. I will update my progress
 
mayumi yamada
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I fixed that. With something else. NOW, I am trying to save the first encounters of DFS into an array so it can be printed for later.
By first encounters : I mean what is first pushed on the stack or visited first.
Here is my now code.




This is the output



** edit.
Just found out that my traversal is WRONG so I am now trying to figure out what's going on here
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is the algorithm for the traversal? you should turn your computer off and write the algorithm on a piece of paper. Then you can turn it into code. You will probably have to delete all the old code if it is not working correctly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!