• 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

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic