Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Main {
- static public Scanner scan = new Scanner(System.in);
- public static int takeFlag(){
- final String error = "Проверьте корректность введенных данных!";
- int num = 0;
- boolean isIncorrect;
- do{
- isIncorrect = false;
- try{
- num = Integer.parseInt(scan.nextLine());
- }
- catch(Exception e){
- isIncorrect = true;
- System.out.println(error);
- }
- if(!isIncorrect && (num < 0 || num > 1)){
- isIncorrect = true;
- System.out.println(error);
- }
- }while(isIncorrect);
- return num;
- }
- public static String takeFileWay() {
- String way;
- boolean isIncorrect;
- do
- {
- isIncorrect = false;
- System.out.println("Введите путь к файлу");
- way = scan.nextLine();
- File file = new File(way);
- if (!file.exists() || !file.canWrite() ||!file.canRead() || !way.endsWith(".txt") || file.isDirectory()) {
- isIncorrect = true;
- System.out.println("Файл не найден, или неверный формат файла; повторите попытку ввода");
- }
- } while (isIncorrect);
- return way;
- }
- public static int sortSpace(String text, String[] some){
- int i = 0;
- int s;
- int k = 0;
- while (i < text.length()) {
- s = 0;
- while (i < text.length() && text.charAt(i) != ' ') {
- if(some[k] == null) {
- some[k] = "";
- }
- some[k] = some[k] + text.charAt(i);
- i++;
- s++;
- }
- i++;
- if (s != 0) {
- k++;
- }
- }
- return k;
- }
- public static String takeFromConsole(){
- String text;
- text = scan.nextLine();
- return text;
- }
- public static String takeFromFile(){
- String way = takeFileWay();
- String text = "";
- try{
- Scanner br = new Scanner(new File(way));
- while(br != null) {
- text = text + br.nextLine();
- }
- } catch (Exception e){
- System.out.println("Incorrect data in File");
- }
- return text;
- }
- public static void conventText(String[] some, int k){
- for (int i = 0; i < k; i++) {
- if (i % 2 == 0) {
- some[i] = "(" + some[i] + ")";
- }
- else {
- some[i] = some[i].toUpperCase();
- }
- }
- }
- public static void resultInConsole(String[] some){
- int i = 0;
- while(some[i] != null) {
- System.out.print(some[i] + " ");
- i++;
- }
- }
- public static void resultInFile(String[] some){
- String way = takeFileWay();
- try(PrintWriter pw = new PrintWriter(new FileWriter(way, false))){
- int i = 0;
- while(some[i] != null) {
- pw.print(some[i] + " ");
- i++;
- }
- } catch(Exception e){
- System.err.println("Ошибка файла");
- System.out.println(e.getMessage());
- }
- }
- public static void choiseEnter(String[] some){
- System.out.println("1 - консоль, 0 - файл");
- int choise = takeFlag();
- if(choise == 1){
- String text = takeFromConsole();
- int k = sortSpace(text, some);
- conventText(some, k);
- }
- else{
- String text = takeFromFile();
- int k = sortSpace(text, some);
- conventText(some, k);
- }
- }
- public static void choiseResult(String[] some){
- System.out.println("1 - консоль, 0 - файл");
- int choise = takeFlag();
- if(choise == 1){
- resultInConsole(some);
- }
- else{
- resultInFile(some);
- }
- }
- public static void main(String[] args) {
- String[] some = new String[1000];
- choiseEnter(some);
- choiseResult(some);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment