Filage

lab3_1

Nov 21st, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.PrintWriter;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     static public Scanner scan = new Scanner(System.in);
  8.  
  9.     public static int takeFlag(){
  10.         final String error = "Проверьте корректность введенных данных!";
  11.         int num = 0;
  12.         boolean isIncorrect;
  13.         do{
  14.             isIncorrect = false;
  15.             try{
  16.                 num = Integer.parseInt(scan.nextLine());
  17.             }
  18.             catch(Exception e){
  19.                 isIncorrect = true;
  20.                 System.out.println(error);
  21.             }
  22.             if(!isIncorrect && (num < 0 || num > 1)){
  23.                 isIncorrect = true;
  24.                 System.out.println(error);
  25.             }
  26.         }while(isIncorrect);
  27.         return num;
  28.     }
  29.  
  30.     public static String takeFileWay() {
  31.         String way;
  32.         boolean isIncorrect;
  33.         do
  34.         {
  35.             isIncorrect = false;
  36.             System.out.println("Введите путь к файлу");
  37.             way = scan.nextLine();
  38.             File file = new File(way);
  39.             if (!file.exists() || !file.canWrite() ||!file.canRead() || !way.endsWith(".txt") || file.isDirectory()) {
  40.                 isIncorrect = true;
  41.                 System.out.println("Файл не найден, или неверный формат файла; повторите попытку ввода");
  42.             }
  43.         } while (isIncorrect);
  44.         return way;
  45.     }
  46.  
  47.  
  48.     public static int sortSpace(String text, String[] some){
  49.         int i = 0;
  50.         int s;
  51.         int k = 0;
  52.         while (i < text.length()) {
  53.             s = 0;
  54.             while (i < text.length() && text.charAt(i) != ' ') {
  55.                 if(some[k] == null) {
  56.                     some[k] = "";
  57.                 }
  58.                 some[k] = some[k] + text.charAt(i);
  59.                 i++;
  60.                 s++;
  61.             }
  62.             i++;
  63.             if (s != 0) {
  64.                 k++;
  65.             }
  66.         }
  67.         return k;
  68.     }
  69.     public static String takeFromConsole(){
  70.         String text;
  71.         text = scan.nextLine();
  72.         return text;
  73.     }
  74.     public static String takeFromFile(){
  75.         String way = takeFileWay();
  76.         String text = "";
  77.         try{
  78.             Scanner br = new Scanner(new File(way));
  79.             while(br != null) {
  80.                 text = text + br.nextLine();
  81.             }
  82.         } catch (Exception e){
  83.             System.out.println("Incorrect data in File");
  84.         }
  85.         return text;
  86.     }
  87.  
  88.     public static void conventText(String[] some, int k){
  89.         for (int i = 0; i < k; i++) {
  90.             if (i % 2 == 0) {
  91.                 some[i] = "(" + some[i] + ")";
  92.             }
  93.             else {
  94.                 some[i] = some[i].toUpperCase();
  95.             }
  96.         }
  97.     }
  98.     public static void resultInConsole(String[] some){
  99.         int i = 0;
  100.         while(some[i] != null) {
  101.             System.out.print(some[i] + " ");
  102.             i++;
  103.         }
  104.     }
  105.     public static void resultInFile(String[] some){
  106.         String way = takeFileWay();
  107.         try(PrintWriter pw = new PrintWriter(new FileWriter(way, false))){
  108.             int i = 0;
  109.             while(some[i] != null) {
  110.                 pw.print(some[i] + " ");
  111.                 i++;
  112.             }
  113.         } catch(Exception e){
  114.             System.err.println("Ошибка файла");
  115.             System.out.println(e.getMessage());
  116.         }
  117.     }
  118.  
  119.     public static void choiseEnter(String[] some){
  120.         System.out.println("1 - консоль, 0 - файл");
  121.         int choise = takeFlag();
  122.         if(choise == 1){
  123.             String text = takeFromConsole();
  124.             int k = sortSpace(text, some);
  125.             conventText(some, k);
  126.         }
  127.         else{
  128.             String text = takeFromFile();
  129.             int k = sortSpace(text, some);
  130.             conventText(some, k);
  131.         }
  132.     }
  133.  
  134.     public static void choiseResult(String[] some){
  135.         System.out.println("1 - консоль, 0 - файл");
  136.         int choise = takeFlag();
  137.         if(choise == 1){
  138.             resultInConsole(some);
  139.         }
  140.         else{
  141.             resultInFile(some);
  142.         }
  143.     }
  144.  
  145.     public static void main(String[] args) {
  146.         String[] some = new String[1000];
  147.         choiseEnter(some);
  148.         choiseResult(some);
  149.     }
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment