← Vissza
A feladat forráskódja:
package szavak;
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 Szavak {
private static String[] aeiou = {"a", "e", "i", "o", "u"};
private static Character[] aeiouChars = {'a', 'e', 'i', 'o', 'u'};
private static List<String> fiveLetterWords;
private static Scanner sc;
public static void main(String[] args) {
task1();
task23();
task4();
task5();
}
private static void task1() {
taskNum(1);
sc = new Scanner(System.in);
System.out.print("Adjon meg egy szót: ");
String word = sc.nextLine().toLowerCase();
boolean hasAeiou = false;
for (String letter : aeiou) {
if (word.contains(letter)) {
hasAeiou = true;
break;
}
}
System.out.println(hasAeiou ? "Van benne magánhangzó." : "Nincs benne magánhangzó.");
}
private static void taskNum(int i) {
System.out.println("\n" + i + ". feladat");
System.out.println("----------");
}
private static void task23() {
try (BufferedReader br = new BufferedReader(new FileReader("szoveg.txt"))) {
String line = null;
int maxLength = 0;
String maxLengthWord = null;
List<String> moreAeiouWords = new ArrayList<>();
int sumWords = 0;
fiveLetterWords = new ArrayList<>();
while ((line = br.readLine()) != null) {
sumWords++;
if (line.length() > maxLength) {
maxLength = line.length();
maxLengthWord = line;
}
if (moreAeiou(line)) {
moreAeiouWords.add(line);
}
if (line.length() == 5) {
fiveLetterWords.add(line);
}
}
taskNum(2);
System.out.printf("A leghosszabb szó: %s, ez a szó %d karakterből áll.%n",
maxLengthWord, maxLength);
taskNum(3);
System.out.println("Szavak, melyekben több magánhangzó van, mint más:");
for (String moreAeiouWord : moreAeiouWords) {
System.out.print(moreAeiouWord + " ");
}
System.out.printf("%n%d/%d : %.2f%%%n",
moreAeiouWords.size(), sumWords,
Double.valueOf(moreAeiouWords.size()) / sumWords * 100);
} catch (IOException ex) {
Logger.getLogger(Szavak.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
private static boolean moreAeiou(String word) {
int aeious = 0;
int others = 0;
for (int i = 0; i < word.length(); i++) {
boolean otherBool = true;
for (Character letter : aeiouChars) {
if (word.charAt(i) == letter) {
otherBool = false;
break;
}
}
if (otherBool) {
others++;
} else {
aeious++;
}
}
return aeious > others;
}
private static void task4() {
taskNum(4);
System.out.print("Kérek egy 3 karakteres szórészletet: ");
String middle = sc.nextLine();
System.out.println("Az ehhez tartozó szórészletek:");
boolean found = false;
if (middle.length() == 3) {
for (String word : fiveLetterWords) {
if (word.substring(1, 4).equals(middle)) {
System.out.print(word + " ");
found = true;
}
}
if (found) {
System.out.println();
} else {
System.out.println("A szórészletet nem tartalmazza egy szó sem.");
}
} else {
System.err.println("Hibás megadott szó!");
}
}
private static void task5() {
taskNum(5);
System.out.println("(állomány kiíratása)");
List<List<String>> groupList = new ArrayList<>();
while (fiveLetterWords.size() > 0) {
String middle = fiveLetterWords.get(0).substring(1, 4);
List<String> ladder = new ArrayList<>();
for (int i = 0; i < fiveLetterWords.size(); i++) {
if (fiveLetterWords.get(i).substring(1, 4).equals(middle)) {
ladder.add(fiveLetterWords.get(i));
fiveLetterWords.remove(i);
i--;
}
}
if (ladder.size() > 1) {
groupList.add(ladder);
}
}
try (PrintWriter pw = new PrintWriter("letra.txt")) {
for (List<String> list : groupList) {
for (String word : list) {
pw.print(word + "\r\n");
}
pw.println();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Szavak.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Hiba! " + ex.getMessage());
}
}
}