Jump to content

  • Log In with Google      Sign In   
  • Create Account

Read 2D array map from text file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 FantasyVII   Members   -  Reputation: 299

Like
0Likes
Like

Posted 28 July 2012 - 04:47 PM

Hello everyone,

I have this text file
1234
5678

I can read this text file into a 2D int array and print it into the screen without any problem.

class Program
    {
	    static int MapX = 26, MapY = 19;

	    static int[,] map;


	    static void Main(string[] args)
	    {
		    string[] buffer = File.ReadAllLines("C:/map.txt");

		    map = new int[buffer.Length, MapX];


		    string line;

		    for (int i = 0; i < buffer.Length; i++)
		    {
			    line = buffer[i];
			    for (int j = 0; j < line.Length; j++)
			    {
				    map[i, j] = (int)(line[j] - '0');
				    Console.Write(map[i, j]);
			    }
		    }
		    Console.ReadKey();
	    }
    }

However this limits me to numbers from 0-9. So I can not store number 10 in the int array, because it will think its 1 and 0 not 10.

so I have been trying to read the same text file but with spaces between the numbers.
1 2 3 4
5 6 7 8

so I want to be able to read all numbers from 0 - 100 or what ever and then store these numbers into a 2D integer array which is called map.

can you guys please help me? How can I store these numbers into an array? keep in mind I also want to store numbers above 9.

1 2 3 4
5 6 7 8 
9 10 11 12 


Sponsor:

#2 Neometron   Members   -  Reputation: 396

Like
1Likes
Like

Posted 28 July 2012 - 08:10 PM

I don't program in c# but would something like this work?

1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12

string[] astrNumbers = buffer.Split(',')

foreach( string strNumber in astrNumbers )
{
int numVal = Convert.ToInt32(strNumber);
}

#3 FantasyVII   Members   -  Reputation: 299

Like
0Likes
Like

Posted 29 July 2012 - 06:06 AM

I don't program in c# but would something like this work?

1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12

string[] astrNumbers = buffer.Split(',')

foreach( string strNumber in astrNumbers )
{
int numVal = Convert.ToInt32(strNumber);
}


thx, i'll try it out and see if it works




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS