← Vissza
A feladat forráskódja:
package sms;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author comtur
*/
public class Sms {
private static Scanner sc;
private static List<String> words;
public static void main(String[] args) {
sc = new Scanner(System.in);
task12(1);
task12(2);
task3();
task4();
task5();
task6();
task7();
task89();
}
private static void task12(int n) {
taskNum(n);
System.out.print("Adj meg egy " + (n == 1 ? "betűt" : "szót") + ": ");
String word = sc.nextLine();
System.out.printf("%s számkódja: %s%n", word, wordToCode(word));
}
private static void taskNum(int i) {
System.out.println("\n" + i + ". feladat\n----------");
}
private static String wordToCode(String word) {
String num = "0";
StringBuilder sb = new StringBuilder();
boolean correct = true;
for (int i = 0; i < word.length(); i++) {
switch (word.toLowerCase().charAt(i)) {
case 'a': case 'b': case 'c':
sb.append("2");
break;
case 'd': case 'e': case 'f':
sb.append("3");
break;
case 'g': case 'h': case 'i':
sb.append("4");
break;
case 'j': case 'k': case 'l':
sb.append("5");
break;
case 'm': case 'n': case 'o':
sb.append("6");
break;
case 'p': case 'q': case 'r': case 's':
sb.append("7");
break;
case 't': case 'u': case 'v':
sb.append("8");
break;
case 'w': case 'x': case 'y': case 'z':
sb.append("9");
break;
default:
sb.append("0");
correct = false;
}
}
if (sb.length() > 0 && correct) {
num = sb.toString();
}
return num;
}
private static void task3() {
taskNum(3);
System.out.println("(Adatok beolvasása)");
words = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("szavak.txt"))) {
String line = null;
while ((line = br.readLine()) != null) {
words.add(line);
}
} catch (IOException ex) {
Logger.getLogger(Sms.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
private static void task4() {
taskNum(4);
int maxLength = words.get(0).length();
String maxLengthWord = words.get(0);
for (String word : words) {
if (word.length() > maxLength) {
maxLength = word.length();
maxLengthWord = word;
}
}
System.out.printf("A leghosszabb szó (ha több van, a listából az első): %s, hossza: %d.%n",
maxLengthWord, maxLength);
}
private static void task5() {
taskNum(5);
int shortCounter = 0;
for (String word : words) {
if (word.length() <= 5) {
shortCounter++;
}
}
System.out.printf("A fájlban %d rövid szó található.%n", shortCounter);
}
private static void task6() {
taskNum(6);
System.out.println("(\"kodok.txt\" állomány létrehozása)");
try (PrintWriter pw = new PrintWriter("kodok.txt")) {
for (String word : words) {
pw.println(wordToCode(word));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Sms.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
private static void task7() {
taskNum(7);
System.out.print("Kérek egy számsort: ");
String numSequence = sc.nextLine();
List<String> passedList = new ArrayList<>();
for (String word : words) {
if (wordToCode(word).equals(numSequence)) {
passedList.add(word);
}
}
if (passedList.isEmpty()) {
System.out.println("A megadott számsorhoz nem tartozik szó a listából.");
} else {
System.out.print("A megadott számsorhoz tartozó szavak: ");
for (int i = 0; i < passedList.size(); i++) {
System.out.print(passedList.get(i) + (i == passedList.size() - 1 ? ".\n" : ", "));
}
}
}
private static void task89() {
taskNum(8);
List<List<String>> multiplesList = new ArrayList<>();
while (!words.isEmpty()) {
String code = wordToCode(words.get(0));
List<String> actualList = new ArrayList<>();
for (int i = 0; i < words.size(); i++) {
if (wordToCode(words.get(i)).equals(code)) {
actualList.add(words.get(i));
words.remove(i);
i--;
}
}
if (actualList.size() > 1) {
multiplesList.add(actualList);
}
}
System.out.println("Amelyik szavakhoz ugyanazok a kódok tartoznak:");
StringBuilder sb = new StringBuilder();
for (List<String> list : multiplesList) {
for (String string : list) {
sb.append(String.format("%s : %s; ", string, wordToCode(string)));
}
}
System.out.println(sb.substring(0, sb.length() - 2) + ".");
taskNum(9);
int maxSize = 0;
int maxPos = 0;
for (int i = 0; i < multiplesList.size(); i++) {
if (multiplesList.get(i).size() > maxSize) {
maxSize = multiplesList.get(i).size();
maxPos = i;
}
}
System.out.printf("A(z) %s kódhoz tartozik a legtöbb szó "
+ "(legalábbis ez az egyik kód a lehetséges több közül).%n",
wordToCode(multiplesList.get(maxPos).get(0)));
System.out.println("A szavak pedig:");
for (String string : multiplesList.get(maxPos)) {
System.out.println(" - " + string);
}
}
}