Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Input {
- //Ввод дробных чисел через пробелы
- public static Float[] inputFloatLine() {
- Scanner scanner = new Scanner(System.in);
- String[] string = new String[0];
- boolean isCorrect = false;
- while (!isCorrect) {
- isCorrect = true;
- System.out.print("Введите числа через пробел: ");
- string = scanner.nextLine().split(" ");
- for (String str : string) {
- if(!str.matches("[+-]?([0-9]*[.])?[0-9]+")) {
- System.out.print("Строка содержит не цифры: ");
- isCorrect = false;
- break;
- }
- }
- }
- Float[] array = new Float[string.length];
- for (int i = 0; i < string.length; i += 1) {
- array[i] = Float.parseFloat(string[i]);
- }
- return array;
- }
- //Ввод строки ¯\_(ツ)_/¯
- public static String inputString(String message) {
- System.out.print(message);
- return new Scanner(System.in).nextLine();
- }
- //Ввод int ¯\_(ツ)_/¯
- public static int inputInt(int min, int max, String message) {
- Scanner scanner = new Scanner(System.in);
- int n;
- try {
- System.out.print(message + ">> ");
- n = scanner.nextInt();
- }
- catch (Exception e) {
- System.out.println("Неверный ввод");
- return inputInt(min, max, message);
- }
- if (n < min || n > max){
- System.out.println("Неверный ввод");
- return inputInt(min, max, message);
- }
- return n;
- }
- //Ввод boolean ¯\_(ツ)_/¯
- public static boolean inputBoolean(String message) {
- System.out.println(message);
- Scanner scanner = new Scanner(System.in);
- boolean n;
- try {
- System.out.print(">> ");
- n = scanner.nextBoolean();
- }
- catch (Exception e) {
- System.out.println("Неверный ввод");
- return inputBoolean(message);
- }
- return n;
- }
- public static Numbers getFloatArrFromString(String string) {
- String[] arr = string.split(" ");
- Float[] numbers = new Float[arr.length];
- for (int i = 0; i < arr.length; i++) {
- if(!arr[i].matches("[+-]?([0-9]*[.])?[0-9]+")) {
- System.out.print("Строка содержит не цифры. Будут использованы заготовленные числа");
- return new Numbers();
- }
- numbers[i] = Float.parseFloat(arr[i]);
- }
- return new Numbers(numbers);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement