954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

2D Array Help!!!

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!!!

yazz110
Newbie Poster
18 posts since Jul 2011
Reputation Points: 10
Solved Threads: 1
 

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?

stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 

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 :(

yazz110
Newbie Poster
18 posts since Jul 2011
Reputation Points: 10
Solved Threads: 1
 

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.

stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 

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 :(

yazz110
Newbie Poster
18 posts since Jul 2011
Reputation Points: 10
Solved Threads: 1
 

Well Ive already told you how to extract the data.

try this tut:
http://www.java2s.com/Tutorial/Java/0180__File/SettingDelimitersforScanner.htm

stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: