11 Replies - 3289 Views - Last Post: 03 July 2012 - 07:33 AM Rate Topic: -----

#1 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

How to read a grid from a text file into a 2d array?

Posted 02 July 2012 - 09:52 AM

Hey guys i want to solve problem 11 of Project Euler.It has a 20 x 20 grid.I want to read that input into my code so i can start solving but i dont know how to do it.I googled it and came to know that i have to copy the numbers into a text file and then read that file.I am still not sure how to do it.I use eclipse so for now i have just created a Numbers.txt file inside the Class name folder inside workspace directory.I have some understanding of FileInputSream but not that well that i can use it.I tried but I am kinda confused.Can someone help me out with this??

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: How to read a grid from a text file into a 2d array?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5906
  • View blog
  • Posts: 12,811
  • Joined: 16-October 07

Re: How to read a grid from a text file into a 2d array?

Posted 02 July 2012 - 10:07 AM

Reading data from a file is not a requirement. Having the data in your program to process it is.

How would you like the data? A 2D array sounds logical. How would that look if you just typed it? A lot like what you have, only with some commas and braces.
Was This Post Helpful? 0
  • +
  • -

#3 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Re: How to read a grid from a text file into a 2d array?

Posted 02 July 2012 - 10:16 AM

View Postbaavgai, on 02 July 2012 - 10:37 PM, said:

Reading data from a file is not a requirement. Having the data in your program to process it is.

How would you like the data? A 2D array sounds logical. How would that look if you just typed it? A lot like what you have, only with some commas and braces.


yea i could manually type all the data but that wouldnt be a good way to go for it right?What if in the future i come across a much bigger data that is impossible to enter manually.What would i do in such a situation??
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: How to read a grid from a text file into a 2d array?

Posted 02 July 2012 - 10:55 AM

No, you don't want to type it manually, but if you copy it to a file, it's not hard to turn spaces into commas, and replace each line break with },\n{ - you can do it in Java, or even easier in perl or python, or sed if you swing that way.

Voila, now you just need to fix up the start and the end, and you're set.

Or you can do it by reading the file.

Make a 2D array of ints.

Copy the grid to a file, call it grid.txt or whatever.
Open a scanner on that file and get the first line.

Open a new scanner on the resulting String and while hasNext(), get next() and put it into an array of int.
At the end of the line, put that array into your 2D array, and repeat.



Quote

I want to read that input into my code so i can start solving

But isn't this the wrong way around? Shouldn't you solve it before you write the code?

This post has been edited by jon.kiparsky: 02 July 2012 - 11:01 AM

Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5906
  • View blog
  • Posts: 12,811
  • Joined: 16-October 07

Re: How to read a grid from a text file into a 2d array?

Posted 02 July 2012 - 11:16 AM

View PostPosiedon, on 02 July 2012 - 01:16 PM, said:

yea i could manually type all the data but that wouldnt be a good way to go for it right?


You never manually type anything, just edit. As Jon noted.


View PostPosiedon, on 02 July 2012 - 01:16 PM, said:

What if in the future i come across a much bigger data that is impossible to enter manually.


Well, that isn't your problem now...

Again, you never enter manually. If you have a screen full of text, you find the most expedient way to get it into your program. You might put the text in a file to read, you might put the text in a string to parse it, you might must throw it in an editor and directly created code from it.

You need to know where you finally want it, regardless.
Was This Post Helpful? 1
  • +
  • -

#6 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Re: How to read a grid from a text file into a 2d array?

Posted 02 July 2012 - 10:20 PM

guys this is how i did it:

public class Demo
{
	public static void main(String args[])
	{
	
		int grid[][] = new int[20][20];
		int i=0,j=0;
		try{
		FileReader reader = new FileReader("Numbers.txt");
		BufferedReader br = new BufferedReader(reader);
		String line;
		while((line=br.readLine())!=null)
		{
			j=0;
			String delim = "[ ]+";
			String tokens[] = line.split(delim);
	
	for(String a : tokens)
	{
		grid[i][j] = Integer.parseInt(a);
		j++;
	
	}
	i++;
		}
		
		
		}catch(Exception e){
			e.printStackTrace();
		}
		for(i=0;i<20;i++)
		{
			for( j=0;j<20;j++)
			{
				System.out.print("  "+grid[i][j]);
			}
			System.out.println();
		}
	}
}


Any further improvements i can make here??
Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5906
  • View blog
  • Posts: 12,811
  • Joined: 16-October 07

Re: How to read a grid from a text file into a 2d array?

Posted 03 July 2012 - 04:22 AM

Use methods, don't put everything all in main. Indeed, put very little in main.

I'd use the scanner, as suggested. Your code could look like:
public class Demo {
	private int [][] getData(Scanner scanner) {
		final int rows = 20;
		final int cols = 20;
		int a[][] = new int[rows][cols];
		for(int row=0; row<rows; row++) {
			for(int col=0; col<cols; col++) {
				a[row][col] = scanner.nextInt();
			}
		}
		return a;
	}
	
	private int [][] getData(String filename) throws FileNotFoundException {
		return getData(new Scanner(new File(filename)));
	}
	
	private int [][] getData() { return getData("Numbers.txt"); }
	
	private void showData(int [][] a) {
		final int rows = a.length;
		final int cols = a[0].length;
		for(int row=0; row<rows; row++) {
			for(int col=0; col<cols; col++) {
				System.out.print("  " + a[row][col]);
			}
			System.out.println();
		}
		System.out.println();
	}
	
	public static void main(String args[]) {
		int [][] grid = getData();
		showData(grid);
	}
}



Note the showData. You'll want to keep that.

Now, what I was talking about... Well, you could still parse:
private int [][] getData() {
	final String data = " 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08"
		+ " 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00"
		+ " 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65"
		+ " 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91"
		+ " 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80"
		+ " 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50"
		+ " 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70"
		+ " 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21"
		+ " 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72"
		+ " 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95"
		+ " 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92"
		+ " 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57"
		+ " 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58"
		+ " 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40"
		+ " 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66"
		+ " 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69"
		+ " 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36"
		+ " 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16"
		+ " 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54"
		+ " 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
	return getData(new Scanner(data));
}



No file, all the data in one place, minimal editing.

Or, just edit then paste:
private int [][] getData() {
	return new int [][] {
		{ 8,2,22,97,38,15,0,40,0,75,4,5,7,78,52,12,50,77,91,8 },
		{ 49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,4,56,62,0 },
		{ 81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,3,49,13,36,65 },
		{ 52,70,95,23,4,60,11,42,69,24,68,56,1,32,56,71,37,2,36,91 },
		{ 22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80 },
		{ 24,47,32,60,99,3,45,2,44,75,33,53,78,36,84,20,35,17,12,50 },
		{ 32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70 },
		{ 67,26,20,68,2,62,12,20,95,63,94,39,63,8,40,91,66,49,94,21 },
		{ 24,55,58,5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72 },
		{ 21,36,23,9,75,0,76,44,20,45,35,14,0,61,33,97,34,31,33,95 },
		{ 78,17,53,28,22,75,31,67,15,94,3,80,4,62,16,14,9,53,56,92 },
		{ 16,39,5,42,96,35,31,47,55,58,88,24,0,17,54,24,36,29,85,57 },
		{ 86,56,0,48,35,71,89,7,5,44,44,37,44,60,21,58,51,54,17,58 },
		{ 19,80,81,68,5,94,47,69,28,73,92,13,86,52,17,77,4,89,55,40 },
		{ 4,52,8,83,97,35,99,16,7,97,57,32,16,26,26,79,33,27,98,66 },
		{ 88,36,68,87,57,62,20,72,3,46,33,67,46,55,12,32,63,93,53,69 },
		{ 4,42,16,73,38,25,39,11,24,94,72,18,8,46,29,32,40,62,76,36 },
		{ 20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,4,36,16 },
		{ 20,73,35,29,78,31,90,1,74,31,49,71,48,86,81,16,23,57,5,54 },
		{ 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52,1,89,19,67,48 }
	};
}



In the interests of full disclosure, I didn't really edit either of those chunks of data. Well, not much. I threw them into Python and let code do the work.
Spoiler


Right, so you have your grid. You're ready to actually work on the problem. Good luck.
Was This Post Helpful? 1
  • +
  • -

#8 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Re: How to read a grid from a text file into a 2d array?

Posted 03 July 2012 - 04:41 AM

^^Hey bro ty so much.The first code in your post is so amazing....all neat and easily readable.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: How to read a grid from a text file into a 2d array?

Posted 03 July 2012 - 06:53 AM

There's a lot to learn from that code. Notice the triple overloading of getData(). That's a really good way to keep modularity without jamming your namespace.

One fix, however, since we're into the actual code

private int [][] getData(Scanner scanner) {
	ArrayList<String> lines = new ArrayList<String>();

	while (scanner.hasNextLine())  lines.add(scanner.nextLine());
	int[][] returnArray = new int[lines.size()][]; 
	for(int i = 0; i <returnArray; i ++) {
		returnArray[i] = lines.get(i).split(" ");
	}
	return returnArray;
}
	


This way you don't have to worry about someone at projecteuler changing the problem on you, maybe bumping it up to a 40X40 grid or something.
Was This Post Helpful? 1
  • +
  • -

#10 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Re: How to read a grid from a text file into a 2d array?

Posted 03 July 2012 - 07:22 AM

ty jon.

i think i am gonna need some help with the code..i just dont understand what i am doing wrong.

Qt is i have to find the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 2020 grid.

The main code is (after taking the grid):

for loop 1 calculates all horizontal products.
for loop 2 calculates all vertical products.
for loop 3 calculates all diagonal products.

I am displaying the max product but still i am getting the wrong answer.Please give me a hint.

int prod=1,max=0;
	
	for (i=0;i<20;i++)
	{
		for(j=0;j<17;j++)
		{
		prod = grid[i][j] * grid[i][j+1] * grid[i][j+2] * grid[i][j+3];
		
		if(prod>max)
		{
			max=prod;
		}
		}
		
	}  
	
	
	
 	for(i=0;i<17;i++)
	{
		for(j=0;j<20;j++)
		{
			prod = grid[i][j] * grid[i+1][j] * grid[i+2][j] * grid [i+3][j];
			if(prod>max)
			{
				max=prod;
				
			}
		}
	}     
	
	
	
	for(i=0;i<17;i++)
	{
		for(j=0;j<17;j++)
		{
			
			prod = grid[i][j] * grid[i+1][j+1] * grid[i+2][j+2] * grid[i+3][j+3];
			if(prod>max)
			{
				max=prod;
			}
			
		}
	}      
		
	System.out.println(max);	

Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: How to read a grid from a text file into a 2d array?

Posted 03 July 2012 - 07:25 AM

What about the other diagonal?
Was This Post Helpful? 1
  • +
  • -

#12 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Re: How to read a grid from a text file into a 2d array?

Posted 03 July 2012 - 07:33 AM

View Postjon.kiparsky, on 03 July 2012 - 07:55 PM, said:

What about the other diagonal?

omg how did i miss that :sweatdrop: ... ty ty so much....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1