public class StringProcessor{
public String findWordConsistingOfDigits(String[] words) {
int count = 0;
String lastFound = null;
for (String element : words) {
if (isOnlyDigits(element)) {
lastFound = element;
count++;
if (count == 2) {
System.out.println("Число, состоящее только из различных цифр: " + element + " и " + count + "-е по счету");
return element;
}
}
}
if (lastFound != null) {
System.out.println("Число, состоящее только из различных цифр: " + lastFound);
return lastFound;
} else {
System.out.println("Таких чисел нет!");
return null;
}
}
public boolean isOnlyDigits(String word) {
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) < '0' || word.charAt(i) > '9') {
return false;
}
}
return true;
}
}
public class Application {
public static void main(String[] args) {
String[] words = new String[]{"qqqqqqqqqqwe", "qwer", "123", "4321"};
StringProcessor stringProcessor = new StringProcessor();
stringProcessor.displayNumberContainingOnlyDifferentDigits(words);
}
}
4321
public class Application {
public static void main(String[] args) {
Calculator calculator = new Calculator(2);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter the first number: ");
double number1 = calculator.getNumber();
System.out.println("Enter the second number: ");
double number2 = calculator.getNumber();
System.out.println("Enter operator (+, -, *, /):");
char operation = calculator.getOperation();
double result = calculator.calculate(number1, number2, operation);
System.out.println("Result: " + result);
System.out.println("Do you want to continue? (Y/N)");
String inputChoice = scanner.next();
if (inputChoice.equalsIgnoreCase("N")) {
System.out.println("Bye!");
break;
} else if (!inputChoice.equalsIgnoreCase("Y")) {
break;
}
}
}
}
public class Calculator {
Scanner scanner = new Scanner(System.in);
private final int precision;
public Calculator(int precision) {
this.precision = precision;
}
public double add(double a, double b) {
return a + b;
}
public double subtract(double a, double b) {
return a - b;
}
public double multiply(double a, double b) {
double result = a * b;
double scale = Math.pow(10.0, precision);
return Math.round(result * scale) / scale;
}
public double div(double a, double b) {
double result = a / b;
if (b == 0) {
return result;
} else {
double scale = Math.pow(10.0, precision);
return Math.round(result * scale) / scale;
}
}
public double getNumber() {
double num;
if (scanner.hasNextDouble()) {
num = scanner.nextDouble();
} else {
System.out.println("Wrong input");
scanner.next();
num = getNumber();
}
return num;
}
public char getOperation() {
char operation;
if (scanner.hasNext()) {
operation = scanner.next().charAt(0);
} else {
System.out.println("Wrong input");
scanner.next();
operation = getOperation();
}
return operation;
}
public double calculate(double number1, double number2, char operation) {
double result;
switch (operation) {
case '+':
result = add(number1, number2);
break;
case '-':
result = subtract(number1, number2);
break;
case '*':
result = multiply(number1, number2);
break;
case '/':
result = div(number1, number2);
break;
default:
System.out.println("Wrong input");
result = calculate(number1, number2, getOperation());
}
return result;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PhoneContacts phoneContacts = new PhoneContacts();
System.out.println("Введите название группы контактов:");
while (true) {
String groupName = scanner.nextLine();
if(!groupName.equals("нет")) {
phoneContacts.addGroup(groupName);
System.out.println("Добавить еще одну группу (введите название либо нет)");
}
else {
break;
}
}
while (true) {
System.out.println("\nДобавить контакт (введите имя и номер телефона или введите нет)");
String name = scanner.nextLine();
if(name.equals("нет")){
break;
}
String[] arr = name.split(" ");
String name1 = arr[0];
String phoneNumber = arr[1];
Contact contact = new Contact(name1, phoneNumber);
System.out.println("Укажите группы контакта через пробел");
String groupName = scanner.nextLine();
String[] strings = groupName.split(" ");
phoneContacts.addContactToGroup(contact, strings);
}
System.out.println("Группы: ");
phoneContacts.printGroups();
System.out.println("\nОтсортированные группы");
phoneContacts.printSortedContacts();
}
}
import java.util.*;
public class PhoneContacts {
HashMap<String, List<Contact>> groups;
public PhoneContacts() {
this.groups = new HashMap<>();
}
public void addGroup(String name) {
if (!groups.containsKey(name)) {
groups.put(name, new ArrayList<>());
}
}
public void addContactToGroup(Contact contact, String[] strings) {
for(String group : strings) {
if(groups.containsKey(group)) {
groups.get(group).add(contact);
}
}
}
public void printGroups() {
for(Map.Entry<String, List<Contact>> entry : groups.entrySet()) {
System.out.println("- " + entry.getKey());
for (Contact contact : entry.getValue()) {
System.out.println("\t Имя: " + contact.getName() + "\tНомер: " + contact.getPhoneNumber());
}
}
}
public void printSortedContacts() {
List<Contact> allContacts = new ArrayList<>();
for(Map.Entry<String, List<Contact>> entry : groups.entrySet()) {
for (Contact contact : entry.getValue()){
if(!allContacts.contains(contact)){
allContacts.add(contact);
}
}
}
allContacts.sort(Comparator.naturalOrder());
for (Contact contact : allContacts) {
System.out.println("\t Имя: " + contact.getName() + "\tНомер: " + contact.getPhoneNumber());
}
}
}
public class Contact implements Comparable<Contact>{
public String name;
public String phoneNumber;
public Contact(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
@Override
public String toString() {
return "Contact{" +
"name='" + name + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Contact contact = (Contact) o;
return Objects.equals(name, contact.name) && Objects.equals(phoneNumber, contact.phoneNumber);
}
@Override
public int hashCode() {
return Objects.hash(name, phoneNumber);
}
@Override
public int compareTo(Contact contact) {
return name.compareTo(contact.getName());
}
}
public void addContactToGroup(Contact contact, String[] strings) {
groupMap.get(Arrays.toString(strings)).add(contact);
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Программа справочник");
PhoneContacts phoneContacts = new PhoneContacts();
System.out.println("Введите название группы контактов:");
while (true) {
String input = scanner.next();
if (input.equals("no")){
break;
} else {
phoneContacts.addGroup(input);
System.out.println("Создать еще одну группу (введите название или введите нет)?");
}
}
System.out.println(phoneContacts.groupMap);
while (true){
System.out.println("Создать контакт (введите наименование и его номер или введите нет)?");
String nameOfContact = scanner.next();
if(nameOfContact.equals("нет")){
break;
}
System.out.println("Укажите группу контактов через пробел");
String inputGroupNames = scanner.next();
String[] arrayOfInputGroupNames = inputGroupNames.split(" ");
System.out.println("Array String: " + Arrays.toString(arrayOfInputGroupNames));
Contact contact = new Contact();
contact.contactList.add(contact.getName());
phoneContacts.addContactToGroup(contact,arrayOfInputGroupNames);
}
System.out.println("Группы в справочнике: ");
System.out.println(phoneContacts.groupMap);
}
}