Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.43 KB | None | 0 0
  1. /*
  2. package csd.uoc.gr.A13;
  3. import csd.uoc.gr.A12.MagicSquareChecker;
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.*;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14. import java.nio.charset.StandardCharsets;
  15. import java.util.ArrayList;
  16. import java.util.Arrays;
  17. import java.util.Scanner;
  18.  
  19. class MagicSquareCheckerFile {
  20.     public static void main(String[] args) throws IOException {
  21.         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.");
  22.         int choice;
  23.         Scanner in = new Scanner(System.in);
  24.         choice = in.nextInt();
  25.         in.close();
  26.         String[] all = new String[0];
  27.         if(choice < 1 || choice > 2){
  28.             System.out.println("Wrong input!");
  29.             System.exit(1);
  30.         }
  31.         String filepath = "";
  32.         if(choice == 1){
  33.             JFileChooser fileChooser = new JFileChooser();
  34.             fileChooser.setDialogTitle("Select a file");
  35.             int userSelection = fileChooser.showSaveDialog(null);
  36.             if (userSelection == JFileChooser.APPROVE_OPTION) {
  37.                 File file = fileChooser.getSelectedFile();
  38.                 filepath = file.getAbsolutePath();
  39.                 System.out.println("The path of the selected file is: " + filepath);
  40.                 all = fillArray(filepath);
  41.             }
  42.         }else {
  43.             filepath =
  44.                     JOptionPane.showInputDialog("The URL is: ","");
  45.             System.out.println(filepath);
  46.             all = downloadANDwrite(filepath);
  47.  
  48.         }
  49.  
  50.         int commas = 0;
  51.         char c;
  52.         int index = 0;
  53.         ArrayList<Integer>numOfcommas = new ArrayList<>();
  54.         for(int i=0;i<all.length;i++) {
  55.             commas = 0;
  56.             for (int j = 0; j < all[i].length(); j++) {
  57.                 c = all[i].charAt(j);
  58.                 if(c == ','){
  59.                     commas++;
  60.                 }
  61.                 if(j == all[i].length()-1){
  62.                     numOfcommas.add(index,commas);
  63.                     index++;
  64.                 }
  65.             }
  66.         }
  67.  
  68.         for(int i=0;i< numOfcommas.size();i++){
  69.             if(i <= numOfcommas.size()-2) {
  70.                 if (!numOfcommas.get(i).equals(numOfcommas.get(i + 1))) {
  71.                     System.out.println("The structure of the matrix is not appropriate");
  72.                     System.exit(69420);
  73.                 }
  74.             }
  75.         }
  76.         int size = all.length;
  77.         int[][] MagicSquare = new int[size][size];
  78.         StringBuilder s1 = new StringBuilder();
  79.         int counter = 0;
  80.         int k;
  81.         StringBuilder s;
  82.         ArrayList<Integer>Numbers = new ArrayList<>();
  83.         for (int i = 0; i < size; i++) {
  84.             s = new StringBuilder();
  85.             for(int j=0; j < all[i].length(); j++){
  86.                 c = all[i].charAt(j);
  87.                 if( c == ',' || c==' ' || c=='\n'){
  88.                     int L = Integer.parseInt(s.toString());
  89.                     Numbers.add(counter,L);
  90.                     counter++;
  91.                     s = new StringBuilder();
  92.                 }else{
  93.                     s.append(c);
  94.                     if(j == all[i].length()-1){
  95.                         int L = Integer.parseInt(s.toString());
  96.                         Numbers.add(counter,L);
  97.                         counter++;
  98.                         s = new StringBuilder();
  99.                     }
  100.                 }
  101.             }
  102.  
  103.         }
  104.         counter =0;
  105.         for(int i = 0;i<size;i++){
  106.             for(int j =0; j<size; j++){
  107.                 MagicSquare[i][j] = Numbers.get(counter);
  108.                 counter++;
  109.             }
  110.         }
  111.         for (int i = 0; i < size; i++) {
  112.             System.out.print("-------");
  113.         }
  114.         System.out.print("\n");
  115.         for (int i = 0; i < size; i++) {
  116.             for (int j = 0; j < size; j++) {
  117.                 System.out.print("|     "+MagicSquare[i][j]);
  118.             }
  119.             System.out.print("| \n");
  120.             for (int m = 0; m < size; m++) {
  121.                 System.out.print("-------");
  122.             }
  123.             System.out.println();
  124.         }
  125.         boolean check = MagicSquareChecker.checkIsMagic(MagicSquare);
  126.  
  127.         if(check){
  128.             String newfilepath = "";
  129.             System.out.println("The square is magic and the magic number is: " + MagicSquareChecker.getMagicNumber(MagicSquare)+".");
  130.             JFileChooser fileChooser = new JFileChooser();
  131.             fileChooser.setDialogTitle("Select a folder");
  132.             fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  133.             int userSelection = fileChooser.showSaveDialog(null);
  134.             if (userSelection == JFileChooser.APPROVE_OPTION) {
  135.                 File file = fileChooser.getSelectedFile();
  136.                 newfilepath = file.getAbsolutePath();
  137.                 System.out.println("The path of the selected file is: " + newfilepath);
  138.             }
  139.             System.out.println(newfilepath);
  140.             PrintWriter writer;
  141.             try {
  142.                 writer = new PrintWriter(newfilepath+"\\magic.txt","UTF-8");
  143.                 for (int i = 0; i < size; i++) {
  144.                     writer.print("-------");
  145.                 }
  146.                 writer.println();
  147.                 for (int i = 0; i < size; i++) {
  148.                     for (int j = 0; j < size; j++) {
  149.                         writer.print("|     "+MagicSquare[i][j]);
  150.                     }
  151.                     writer.print("| \n");
  152.                     for (int m = 0; m < size; m++) {
  153.                         writer.print("-------");
  154.                     }
  155.                     writer.println();
  156.                 }
  157.                 writer.println("Magic square with magic number "+ MagicSquareChecker.getMagicNumber(MagicSquare));
  158.                 writer.close();
  159.             } catch (Exception e) {
  160.                 System.out.println("Problem: "+e);
  161.             }
  162.         }else{
  163.             System.out.println("The square isn't magic.");
  164.         }
  165.     }
  166.  
  167.     public static String[] fillArray(String filename) throws FileNotFoundException {
  168.         Scanner inputFile = new Scanner(new File(filename));
  169.         int count = 0;
  170.         while (inputFile.hasNextLine()) {
  171.             inputFile.nextLine();
  172.             count++;
  173.         }
  174.         inputFile.close();
  175.  
  176.         String[] array = new String[count];
  177.         inputFile = new Scanner(new File(filename));
  178.         for (int i = 0; i < count; i++) {
  179.             array[i] = inputFile.nextLine();
  180.         }
  181.         inputFile.close();
  182.         return array;
  183.     }
  184.  
  185.     static String[] downloadANDwrite(String address) throws IOException {
  186.         int count = 0;
  187.         URL url = new URL(address);
  188.         Scanner in= new Scanner(
  189.                 new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
  190.         while (in.hasNextLine()) {
  191.             in.nextLine();
  192.             count++;
  193.         }
  194.         in.close();
  195.         String[] array = new String[count];
  196.         in= new Scanner(
  197.                 new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
  198.         for (int i = 0; i < count; i++) {
  199.             array[i] = in.nextLine();
  200.         }
  201.         in.close();
  202.  
  203.         return array;
  204.     }
  205.  
  206. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement