Last active
November 30, 2016 08:14
-
-
Save charlesreid1/326d49a4ea5ce555c115fbe6f64ddecb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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