Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- package csd.uoc.gr.A13;
- import csd.uoc.gr.A12.MagicSquareChecker;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.swing.JOptionPane;
- import javax.swing.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- class MagicSquareCheckerFile {
- public static void main(String[] args) throws IOException {
- System.out.println("Would you like to choose a file or download one?\nPress \"1\" to choose a file or \"2\" to download a file and hit enter.");
- int choice;
- Scanner in = new Scanner(System.in);
- choice = in.nextInt();
- in.close();
- String[] all = new String[0];
- if(choice < 1 || choice > 2){
- System.out.println("Wrong input!");
- System.exit(1);
- }
- String filepath = "";
- if(choice == 1){
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setDialogTitle("Select a file");
- int userSelection = fileChooser.showSaveDialog(null);
- if (userSelection == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- filepath = file.getAbsolutePath();
- System.out.println("The path of the selected file is: " + filepath);
- all = fillArray(filepath);
- }
- }else {
- filepath =
- JOptionPane.showInputDialog("The URL is: ","");
- System.out.println(filepath);
- all = downloadANDwrite(filepath);
- }
- int commas = 0;
- char c;
- int index = 0;
- ArrayList<Integer>numOfcommas = new ArrayList<>();
- for(int i=0;i<all.length;i++) {
- commas = 0;
- for (int j = 0; j < all[i].length(); j++) {
- c = all[i].charAt(j);
- if(c == ','){
- commas++;
- }
- if(j == all[i].length()-1){
- numOfcommas.add(index,commas);
- index++;
- }
- }
- }
- for(int i=0;i< numOfcommas.size();i++){
- if(i <= numOfcommas.size()-2) {
- if (!numOfcommas.get(i).equals(numOfcommas.get(i + 1))) {
- System.out.println("The structure of the matrix is not appropriate");
- System.exit(69420);
- }
- }
- }
- int size = all.length;
- int[][] MagicSquare = new int[size][size];
- StringBuilder s1 = new StringBuilder();
- int counter = 0;
- int k;
- StringBuilder s;
- ArrayList<Integer>Numbers = new ArrayList<>();
- for (int i = 0; i < size; i++) {
- s = new StringBuilder();
- for(int j=0; j < all[i].length(); j++){
- c = all[i].charAt(j);
- if( c == ',' || c==' ' || c=='\n'){
- int L = Integer.parseInt(s.toString());
- Numbers.add(counter,L);
- counter++;
- s = new StringBuilder();
- }else{
- s.append(c);
- if(j == all[i].length()-1){
- int L = Integer.parseInt(s.toString());
- Numbers.add(counter,L);
- counter++;
- s = new StringBuilder();
- }
- }
- }
- }
- counter =0;
- for(int i = 0;i<size;i++){
- for(int j =0; j<size; j++){
- MagicSquare[i][j] = Numbers.get(counter);
- counter++;
- }
- }
- for (int i = 0; i < size; i++) {
- System.out.print("-------");
- }
- System.out.print("\n");
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- System.out.print("| "+MagicSquare[i][j]);
- }
- System.out.print("| \n");
- for (int m = 0; m < size; m++) {
- System.out.print("-------");
- }
- System.out.println();
- }
- boolean check = MagicSquareChecker.checkIsMagic(MagicSquare);
- if(check){
- String newfilepath = "";
- System.out.println("The square is magic and the magic number is: " + MagicSquareChecker.getMagicNumber(MagicSquare)+".");
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setDialogTitle("Select a folder");
- fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int userSelection = fileChooser.showSaveDialog(null);
- if (userSelection == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- newfilepath = file.getAbsolutePath();
- System.out.println("The path of the selected file is: " + newfilepath);
- }
- System.out.println(newfilepath);
- PrintWriter writer;
- try {
- writer = new PrintWriter(newfilepath+"\\magic.txt","UTF-8");
- for (int i = 0; i < size; i++) {
- writer.print("-------");
- }
- writer.println();
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- writer.print("| "+MagicSquare[i][j]);
- }
- writer.print("| \n");
- for (int m = 0; m < size; m++) {
- writer.print("-------");
- }
- writer.println();
- }
- writer.println("Magic square with magic number "+ MagicSquareChecker.getMagicNumber(MagicSquare));
- writer.close();
- } catch (Exception e) {
- System.out.println("Problem: "+e);
- }
- }else{
- System.out.println("The square isn't magic.");
- }
- }
- public static String[] fillArray(String filename) throws FileNotFoundException {
- Scanner inputFile = new Scanner(new File(filename));
- int count = 0;
- while (inputFile.hasNextLine()) {
- inputFile.nextLine();
- count++;
- }
- inputFile.close();
- String[] array = new String[count];
- inputFile = new Scanner(new File(filename));
- for (int i = 0; i < count; i++) {
- array[i] = inputFile.nextLine();
- }
- inputFile.close();
- return array;
- }
- static String[] downloadANDwrite(String address) throws IOException {
- int count = 0;
- URL url = new URL(address);
- Scanner in= new Scanner(
- new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
- while (in.hasNextLine()) {
- in.nextLine();
- count++;
- }
- in.close();
- String[] array = new String[count];
- in= new Scanner(
- new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
- for (int i = 0; i < count; i++) {
- array[i] = in.nextLine();
- }
- in.close();
- return array;
- }
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement