Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.NoSuchElementException;
- import java.io.*;
- public class Main {
- private static final Scanner scan = new Scanner(System.in);
- public static int choiceCheck() {
- int choice;
- choice = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- choice = Integer.parseInt(scan.nextLine());
- } catch (Exception err) {
- System.out.println("Error! Input a number");
- isIncorrect = true;
- }
- if (!isIncorrect && (choice < 1 || choice > 2)) {
- System.out.println("Error! Input 1 or 2");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice;
- }
- public static String checkInputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- System.out.println("Input path to the file:");
- isIncorrect = false;
- path = scan.nextLine();
- File inputFile = new File(path);
- if (!inputFile.exists()) {
- isIncorrect = true;
- System.out.println("Could not find the file");
- } else if ((!inputFile.canRead()) || (!inputFile.canWrite())) {
- isIncorrect = true;
- System.out.println("Could not open the file");
- }
- } while (isIncorrect);
- return path;
- }
- public static String inputCheck() {
- boolean isIncorrect;
- String str;
- int i;
- System.out.println("Input the string consisting of numbers, letters of the English alphabet and symbols '-', '+', '.'");
- do {
- isIncorrect = false;
- str = scan.nextLine();
- for (i = 0; i < str.length(); i++) {
- if (!((str.charAt(i) == 43) || (str.charAt(i) == 45) || (str.charAt(i) == 46) || ((str.charAt(i) > 47) && (str.charAt(i) < 58)) ||
- ((str.charAt(i) > 64) && (str.charAt(i) < 91)) || ((str.charAt(i) > 96) && (str.charAt(i) < 123)))) {
- System.out.println("Incorrect string format");
- isIncorrect = true;
- }
- }
- }
- while (isIncorrect);
- return str;
- }
- public static String fileCheckString() {
- String path, str, str0;
- boolean isIncorrect;
- int i;
- do {
- isIncorrect = false;
- path = checkInputFilePath();
- File inputFile = new File(path);
- Scanner scan2 = null;
- try {
- scan2 = new Scanner(inputFile);
- } catch (FileNotFoundException e) {
- throw new RuntimeException(e);
- }
- str = scan2.nextLine();
- if ((!isIncorrect) && (str.length() < 1)) {
- isIncorrect = true;
- System.out.println("The string is empty");
- }
- if (scan2.hasNextLine()) {
- isIncorrect = true;
- System.out.println("There should be only one string");
- }
- if (!isIncorrect) {
- for (i = 0; i < str.length(); i++) {
- if (!((str.charAt(i) == 43 || str.charAt(i) == 45 || str.charAt(i) == 46 || (str.charAt(i) > 47 && str.charAt(i) < 58) ||
- (str.charAt(i) > 64 && str.charAt(i) < 91) || (str.charAt(i) > 96 && str.charAt(i) < 123))))
- isIncorrect = true;
- }
- }
- if (isIncorrect) {
- System.out.println("Incorrect string format");
- }
- scan2.close();
- } while (isIncorrect);
- return str;
- }
- public static String inputChoice() {
- int choice;
- String str;
- System.out.println("Choose input option:\n1.Input through console\n2.Input through file");
- choice = choiceCheck();
- if (choice == 1) {
- str = inputCheck();
- } else {
- str = fileCheckString();
- }
- return str;
- }
- public static String checkOutputFilePath() {
- String path;
- System.out.println("Input file path and the name of the file for\nexample С:\\Projects\\Number\\FileName.txt. If the\nfile does not exist, then it will be created\nautomatically in the given directory");
- path = scan.nextLine();
- File outputFile = new File(path);
- if (outputFile.isFile()) {
- if (!outputFile.canRead()) {
- System.out.println("Could not open the file");
- }
- }
- return path;
- }
- public static void findConsoleSubStr(String str) {
- String subStr;
- int i;
- boolean isFound;
- isFound = false;
- subStr = "";
- for (i = 0; i < str.length(); i++) {
- if (str.charAt(i) == '+' || str.charAt(i) == '-') {
- if (subStr.length() > 1) {
- System.out.println(subStr);
- } else {
- if ((i > 0) && (str.charAt(i - 1) == '0')) {
- System.out.println("0");
- }
- }
- subStr = "";
- subStr += str.charAt(i);
- isFound = true;
- } else {
- if (str.charAt(i) == 46 || (str.charAt(i) > 64 && str.charAt(i) < 91) || (str.charAt(i) > 96 && str.charAt(i) < 123)) {
- isFound = false;
- if (subStr.length() > 1) {
- System.out.println(subStr);
- } else {
- if ((i > 0) && (str.charAt(i - 1) == '0')) {
- System.out.println("0");
- }
- }
- subStr = "";
- } else {
- if (str.charAt(i) == 48) {
- if (isFound && !(subStr.charAt(subStr.length() - 1) == '+' || subStr.charAt(subStr.length() - 1) == '-')) {
- subStr += str.charAt(i);
- }
- } else {
- if (str.charAt(i) > 48 && str.charAt(i) < 58) {
- if (isFound) {
- subStr += str.charAt(i);
- }
- }
- }
- }
- }
- }
- if (isFound && subStr.length() > 1) {
- System.out.println(subStr);
- }
- }
- public static void findFileSubStr(String str) {
- String subStr, path;
- int i;
- boolean isFound;
- isFound = false;
- subStr = "";
- path = checkOutputFilePath();
- try (PrintWriter pw = new PrintWriter((path))) {
- for (i = 0; i < str.length(); i++) {
- if (str.charAt(i) == '+' || str.charAt(i) == '-') {
- if (subStr.length() > 1) {
- pw.println(subStr);
- } else {
- if ((i > 0) && (str.charAt(i - 1) == '0')) {
- pw.println("0");
- }
- }
- subStr = "";
- subStr += str.charAt(i);
- isFound = true;
- } else {
- if (str.charAt(i) == 46 || (str.charAt(i) > 64 && str.charAt(i) < 91) || (str.charAt(i) > 96 && str.charAt(i) < 123)) {
- isFound = false;
- if (subStr.length() > 1) {
- pw.println(subStr);
- } else {
- if ((i > 0) && (str.charAt(i - 1) == '0')) {
- pw.println("0");
- }
- }
- subStr = "";
- } else {
- if (str.charAt(i) == 48) {
- if (isFound && !(subStr.charAt(subStr.length() - 1) == '+' || subStr.charAt(subStr.length() - 1) == '-')) {
- subStr += str.charAt(i);
- }
- } else {
- if (str.charAt(i) > 48 && str.charAt(i) < 58) {
- if (isFound) {
- subStr += str.charAt(i);
- }
- }
- }
- }
- }
- }
- if (isFound && subStr.length() > 1) {
- pw.print(subStr);
- }
- } catch (IOException ex) {
- System.out.println("Unsuccessful output");
- } finally {
- System.out.println("Successful output");
- }
- }
- public static void outputChoice(String str) {
- int choice;
- System.out.println("Choose output option:\n1.Output through console\n2.Output through file");
- choice = choiceCheck();
- if (choice == 1) {
- findConsoleSubStr(str);
- } else {
- findFileSubStr(str);
- }
- scan.close();
- }
- public static void main(String[] args) {
- String str;
- str = inputChoice();
- outputChoice(str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement