Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ForMens {
- public String[] mas;
- private char[] separators = { '.', ',', ' ' };
- int[] res;
- public void createMas() {
- int n = 0;
- Scanner sc = new Scanner(System.in);
- System.out.print("Введите количество строк: ");
- if (sc.hasNextInt()) {
- n = sc.nextInt();
- } else {
- System.out.print("Введены некорректные данные!");
- return;
- }
- if (n <= 10) {
- mas = new String[n];
- Scanner sc1 = new Scanner(System.in);
- for (int i = 0; i < mas.length; i++) {
- System.out.print("Введите строку номер " + (i + 1) + ": ");
- mas[i] = sc1.nextLine();
- }
- }
- }
- private boolean isSeparator(char symbol) {
- for (int i = 0; i < separators.length; i++) {
- if (separators[i] == symbol) {
- return true;
- }
- }
- return false;
- }
- public int countWords(String line) {
- boolean insideWord = false;
- int result = 0;
- for (int i = 0; i < line.length(); i++) {
- if (!isSeparator(line.charAt(i))) {
- if (!insideWord) {
- result++;
- insideWord = true;
- }
- } else {
- insideWord = false;
- }
- }
- return result;
- }
- public void sorting() { // вот тут уже на основе твоих методов я сделал сортировку массива строк
- res = new int[mas.length]; // ну и слегка изменил синтаксис твой, потому что кое что видимо в Java по-другому
- for (int i = 0; i < mas.length; i++) {
- res[i] = countWords(mas[i]);
- }
- for (int i = 0; i < mas.length; i++) {
- for (int j = i + 1; j < mas.length; j++) {
- if (res[i] > res[j]) {
- String temp = mas[i];
- mas[i] = mas[j];
- mas[j] = temp;
- }
- }
- }
- }
- public void showMas() {
- for (int i = 0; i < mas.length; i++) {
- System.out.println(mas[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement