Advertisement
lithie_oce

lab 3.1 java

Nov 30th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.NoSuchElementException;
  3. import java.io.*;
  4.  
  5. public class Main {
  6. private static final Scanner scan = new Scanner(System.in);
  7.  
  8. public static int choiceCheck() {
  9. int choice;
  10. choice = 0;
  11. boolean isIncorrect;
  12. do {
  13. isIncorrect = false;
  14. try {
  15. choice = Integer.parseInt(scan.nextLine());
  16. } catch (Exception err) {
  17. System.out.println("Error! Input a number");
  18. isIncorrect = true;
  19. }
  20. if (!isIncorrect && (choice < 1 || choice > 2)) {
  21. System.out.println("Error! Input 1 or 2");
  22. isIncorrect = true;
  23. }
  24.  
  25. } while (isIncorrect);
  26. return choice;
  27. }
  28.  
  29. public static String checkInputFilePath() {
  30. String path;
  31. boolean isIncorrect;
  32. do {
  33. System.out.println("Input path to the file:");
  34. isIncorrect = false;
  35. path = scan.nextLine();
  36. File inputFile = new File(path);
  37. if (!inputFile.exists()) {
  38. isIncorrect = true;
  39. System.out.println("Could not find the file");
  40. } else if ((!inputFile.canRead()) || (!inputFile.canWrite())) {
  41. isIncorrect = true;
  42. System.out.println("Could not open the file");
  43. }
  44. } while (isIncorrect);
  45. return path;
  46. }
  47.  
  48. public static String inputCheck() {
  49. boolean isIncorrect;
  50. String str;
  51. int i;
  52. System.out.println("Input the string consisting of numbers, letters of the English alphabet and symbols '-', '+', '.'");
  53. do {
  54. isIncorrect = false;
  55. str = scan.nextLine();
  56. for (i = 0; i < str.length(); i++) {
  57. if (!((str.charAt(i) == 43) || (str.charAt(i) == 45) || (str.charAt(i) == 46) || ((str.charAt(i) > 47) && (str.charAt(i) < 58)) ||
  58. ((str.charAt(i) > 64) && (str.charAt(i) < 91)) || ((str.charAt(i) > 96) && (str.charAt(i) < 123)))) {
  59. System.out.println("Incorrect string format");
  60. isIncorrect = true;
  61. }
  62. }
  63. }
  64. while (isIncorrect);
  65. return str;
  66. }
  67.  
  68. public static String fileCheckString() {
  69. String path, str, str0;
  70. boolean isIncorrect;
  71. int i;
  72. do {
  73. isIncorrect = false;
  74. path = checkInputFilePath();
  75. File inputFile = new File(path);
  76. Scanner scan2 = null;
  77. try {
  78. scan2 = new Scanner(inputFile);
  79. } catch (FileNotFoundException e) {
  80. throw new RuntimeException(e);
  81. }
  82. str = scan2.nextLine();
  83. if ((!isIncorrect) && (str.length() < 1)) {
  84. isIncorrect = true;
  85. System.out.println("The string is empty");
  86. }
  87. if (scan2.hasNextLine()) {
  88. isIncorrect = true;
  89. System.out.println("There should be only one string");
  90. }
  91. if (!isIncorrect) {
  92. for (i = 0; i < str.length(); i++) {
  93. if (!((str.charAt(i) == 43 || str.charAt(i) == 45 || str.charAt(i) == 46 || (str.charAt(i) > 47 && str.charAt(i) < 58) ||
  94. (str.charAt(i) > 64 && str.charAt(i) < 91) || (str.charAt(i) > 96 && str.charAt(i) < 123))))
  95. isIncorrect = true;
  96. }
  97.  
  98. }
  99. if (isIncorrect) {
  100. System.out.println("Incorrect string format");
  101. }
  102. scan2.close();
  103. } while (isIncorrect);
  104. return str;
  105. }
  106.  
  107. public static String inputChoice() {
  108. int choice;
  109. String str;
  110. System.out.println("Choose input option:\n1.Input through console\n2.Input through file");
  111. choice = choiceCheck();
  112. if (choice == 1) {
  113. str = inputCheck();
  114. } else {
  115. str = fileCheckString();
  116. }
  117. return str;
  118. }
  119.  
  120. public static String checkOutputFilePath() {
  121. String path;
  122.  
  123. 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");
  124. path = scan.nextLine();
  125. File outputFile = new File(path);
  126. if (outputFile.isFile()) {
  127. if (!outputFile.canRead()) {
  128. System.out.println("Could not open the file");
  129. }
  130. }
  131. return path;
  132. }
  133.  
  134. public static void findConsoleSubStr(String str) {
  135. String subStr;
  136. int i;
  137. boolean isFound;
  138. isFound = false;
  139. subStr = "";
  140. for (i = 0; i < str.length(); i++) {
  141. if (str.charAt(i) == '+' || str.charAt(i) == '-') {
  142. if (subStr.length() > 1) {
  143. System.out.println(subStr);
  144. } else {
  145. if ((i > 0) && (str.charAt(i - 1) == '0')) {
  146. System.out.println("0");
  147. }
  148. }
  149. subStr = "";
  150. subStr += str.charAt(i);
  151. isFound = true;
  152. } else {
  153. if (str.charAt(i) == 46 || (str.charAt(i) > 64 && str.charAt(i) < 91) || (str.charAt(i) > 96 && str.charAt(i) < 123)) {
  154. isFound = false;
  155. if (subStr.length() > 1) {
  156. System.out.println(subStr);
  157. } else {
  158. if ((i > 0) && (str.charAt(i - 1) == '0')) {
  159. System.out.println("0");
  160. }
  161. }
  162. subStr = "";
  163. } else {
  164. if (str.charAt(i) == 48) {
  165. if (isFound && !(subStr.charAt(subStr.length() - 1) == '+' || subStr.charAt(subStr.length() - 1) == '-')) {
  166. subStr += str.charAt(i);
  167. }
  168. } else {
  169. if (str.charAt(i) > 48 && str.charAt(i) < 58) {
  170. if (isFound) {
  171. subStr += str.charAt(i);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. if (isFound && subStr.length() > 1) {
  179. System.out.println(subStr);
  180. }
  181. }
  182.  
  183. public static void findFileSubStr(String str) {
  184. String subStr, path;
  185. int i;
  186. boolean isFound;
  187. isFound = false;
  188. subStr = "";
  189. path = checkOutputFilePath();
  190. try (PrintWriter pw = new PrintWriter((path))) {
  191. for (i = 0; i < str.length(); i++) {
  192. if (str.charAt(i) == '+' || str.charAt(i) == '-') {
  193. if (subStr.length() > 1) {
  194. pw.println(subStr);
  195. } else {
  196. if ((i > 0) && (str.charAt(i - 1) == '0')) {
  197. pw.println("0");
  198. }
  199. }
  200. subStr = "";
  201. subStr += str.charAt(i);
  202. isFound = true;
  203. } else {
  204. if (str.charAt(i) == 46 || (str.charAt(i) > 64 && str.charAt(i) < 91) || (str.charAt(i) > 96 && str.charAt(i) < 123)) {
  205. isFound = false;
  206. if (subStr.length() > 1) {
  207. pw.println(subStr);
  208. } else {
  209. if ((i > 0) && (str.charAt(i - 1) == '0')) {
  210. pw.println("0");
  211. }
  212. }
  213. subStr = "";
  214. } else {
  215. if (str.charAt(i) == 48) {
  216. if (isFound && !(subStr.charAt(subStr.length() - 1) == '+' || subStr.charAt(subStr.length() - 1) == '-')) {
  217. subStr += str.charAt(i);
  218. }
  219. } else {
  220. if (str.charAt(i) > 48 && str.charAt(i) < 58) {
  221. if (isFound) {
  222. subStr += str.charAt(i);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. if (isFound && subStr.length() > 1) {
  230. pw.print(subStr);
  231. }
  232. } catch (IOException ex) {
  233. System.out.println("Unsuccessful output");
  234. } finally {
  235. System.out.println("Successful output");
  236. }
  237. }
  238.  
  239. public static void outputChoice(String str) {
  240. int choice;
  241. System.out.println("Choose output option:\n1.Output through console\n2.Output through file");
  242. choice = choiceCheck();
  243. if (choice == 1) {
  244. findConsoleSubStr(str);
  245. } else {
  246. findFileSubStr(str);
  247. }
  248. scan.close();
  249. }
  250.  
  251. public static void main(String[] args) {
  252. String str;
  253. str = inputChoice();
  254. outputChoice(str);
  255. }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement