Skip to content

Instantly share code, notes, and snippets.

@mariiaKolokolova
Created June 2, 2020 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariiaKolokolova/31e95b8ef626963880a2c3fdf630f5cd to your computer and use it in GitHub Desktop.
Save mariiaKolokolova/31e95b8ef626963880a2c3fdf630f5cd to your computer and use it in GitHub Desktop.
package maricka.kolokolova;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Input file name:");
String fileName = sc.nextLine();
inputArr(getSizeArray(fileName), fileName);
sc.close();
}
static int[][] getSizeArray(String fileName) {
String text = "";
int countColm = 0;
int countRow = 0;
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String temp = "";
for (; (temp = br.readLine()) != null;) {
text = temp;
if (text.length() > countColm) {
countColm = text.length();
}
countRow++;
}
} catch (IOException e) {
System.out.println("Error");
}
int[][] mass = new int[countRow][countColm];
return mass;
}
static void inputArr(int[][] mass, String fileName) {
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String text = "";
int row = 0;
for (; (text = br.readLine()) != null;) {
char[] t = text.toCharArray();
for (int j = 0; j < t.length; j++) {
mass[row][j] = Character.getNumericValue(t[j]);
}
row++;
}
System.out.println("Your array is:");
for (int i = 0; i < mass.length; i++) {
for (int j = 0; j < mass[0].length; j++) {
System.out.print(mass[i][j]);
}
System.out.println();
}
} catch (IOException e) {
System.out.println("Error!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment