Skip to content

Instantly share code, notes, and snippets.

@charlesreid1
Last active November 30, 2016 08:14
Show Gist options
  • Save charlesreid1/326d49a4ea5ce555c115fbe6f64ddecb to your computer and use it in GitHub Desktop.
Save charlesreid1/326d49a4ea5ce555c115fbe6f64ddecb to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class MagicReader {
public static void main(String[] args) {
String filename = magicsquare.txt;
Scanner s = new Scanner(new File(filename));
int lines = 1;
while(s.hasNextLine()) {
s.nextLine();
lines++;
}
System.out.println("Size of magic square: " + lines);
int[lines][lines] magicSquare = new int[lines][lines];
s = new Scanner(new File(filename));
for(int i=0; s.hasNextInt(); i++ ) {
int nextNum = s.nextInt();
int r = i/lines;
int c = i%lines;
magicSquare[r][c] = nextNum;
}
for(int k=0; k<lines; k++) {
System.out.println( Arrays.toString(magicSquare[k]) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment