Ok so I'm using a buffered reader to read a file and it works alright but my problem is I have integers in this file I have to find and place into an array and I don't know how to do this(feel so stupid). I'm trying to work on a travelling salesman problem using a 2d array where each integer found is stored in the array but I have no idea how to implement this. The sorting bit I've got but the other part I don't please Help!!!

Recommended Answers

All 6 Replies

If you know the order of the matrix, then everything should be straight forward...
Instead of using a BufferedReader, use a Scanner to "decorate" the File stream.

Now you can use nextInt() method to read ints one by one.

And as you do use the simple nested forloop to store the values u read onto a 2d array..

Was this useful? Could u temme which part u need help with?

The thing is I'm working on a file, and it has to been read into a 2d array. My problem is that this file contains some numbers organised in random places separated by "," and or a space or Strings and thats what I actually need to input into the 2D array but I don't know how to extract this information :(

Is there any specific format?

if each row was writtin in each line and each element seperated by "," then you can read the entire line using "nextLine()" method of scanner and then you can tokeninze your string using "," as the delimiter. and you can parse the string to get the data and you can load em into your array.

As I said before If it was a space you can use nextInt() to read the integers one by one easily and you can load em.


If you could post the file, we would be more helpful.

NAME = testcase,
SIZE = 8,
2,6,8,9,8,6,3,
4,8,10,10,7,5,
5,6,8,8,9,
4,6,7,8,
3,5,9,
3,6,
4

thats the file but I used a scanner to read it. All I really want to extract is the information from line 3. I want to input each number into a 2d array of of dimentions 8. But I've tried so many ways and its not working :(

whoops sorry about that. I didn't really get it back then. I completely understand now :), thanks. I guess I needed sometime to keep inproving my programming knowledge.

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.