Diskuze: Pomoc
V předchozím kvízu, Online test znalostí Java, jsme si ověřili nabyté zkušenosti z kurzu.


Jindřich Máca:24.1.2020 11:32
Ahoj, takhle to tady úplně nefunguje, protože nikdo za tebe nebude dělat
domací úkol nebo úkol k pohovoru. Jedna věc je poslat zadání a druhá věc
je položit dobrý dotaz. To znamená např. ukázat svoje dosavadní řešení,
zeptat se co přesně ti v tom není jasné, jak dál postupovat nebo
prodiskutovat na čem jsi se zasekl. Pak se určitě najde někdo ochotný a
poradí.
Závěr: Ty v dotazu voláš o pomoc a já se ptám s čím konkrétně?
Jan Švec:25.1.2020 14:57
private String analyzuj(String text){
for(int i = 0; i < text.length(); i++){
char c = text.charAt(c);
if(Character.isLetter(c)){
Pismena++;
}
if(Character.isLowerCase(c)) {
Mpismena++;
}
if(Character.isUpperCase(c)){
Vpismena++;
}
if(Character.isDigit(c)){
Cisla++;
}
}
return "";
}
public static void main(String[] args) {
public int Cisla;
public int Vpismena;
public int Mpismena;
public int Mezery;
public int ISO;
public int Pismena;
public int Getcisla(){
return Cisla;
}
public int Getvpismena(){
return Vpismena;
}
public int Getmpismena(){
return Mpismena;
}
public int Getmezery(){
return Mezery;
}
public int ISO (){
return ISO;
}
}
}
ZATÍM MÁM TOHLE ALE DAL UŽ NEVÍM
Ahoj keďže som cestoval vlakom a tu na Slovensku v nich nemáme WiFi a
mobilný signál nestojí za nič som niečo z nudy napísal. Môj názor je, že ak ti rovno napíšem
hotové riešenie tak to bude mať pre teba rovnaký význam ako keby som
žiadne nenapísal, ale takto sa aspoň môžeš inšpirovať ak by si náhodou
chcel zbúchať niečo aj ty sám
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TextAnalyzer {
/**
* C0 Controls and Basic Latin, C1 Controls and Latin-1 Supplement, Latin Extended-A
*/
private static final int ASCII_TABLE_SIZE = 384;
private int digitsCount;
private int lettersCount;
private int lowerCaseLettersCount;
private int upperCaseLettersCount;
private int whiteSpacesCount;
private int isoControlCharactersCount;
private List<String> theLongestLinesList;
private int theLongestLineLength;
private double averageLineLength;
private int totalLinesLengthSum;
private int totalLinesCount;
private List<Character> theMostFrequentLettersList;
private List<Character> theMostFrequentCharactersList;
private int frequentLetterCount;
private int frequentCharacterCount;
private int[] historgram;
public TextAnalyzer() {
init();
}
private void init() {
resetStatistics();
}
public void readText(String text) {
analyzeText(text);
}
private void analyzeText(String text) {
if (text == null || text.isEmpty()) {
return;
}
String[] lines = text.split(System.lineSeparator());
if (System.lineSeparator().equals("\r\n")) {
isoControlCharactersCount += lines.length * 2;
if(!(text.length() > 1 && text.charAt(text.length() - 2) == '\r' && text.charAt(text.length() - 1) == '\n')){
isoControlCharactersCount -= 2;
}
} else {
isoControlCharactersCount += lines.length;
if(!(text.charAt(text.length() - 1) == '\n')){
isoControlCharactersCount -= 1;
}
}
for (String line : lines) {
analyzeLine(line);
totalLinesLengthSum += line.length();
}
totalLinesCount += lines.length;
averageLineLength = (double) totalLinesLengthSum / totalLinesCount;
for(int i = 0; i < ASCII_TABLE_SIZE; i++){
// if((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z')){
if(Character.isLetter(i)){ //easier for UTF-8 letters
frequentLetterCount = updateFrequencyList(frequentLetterCount, historgram[i], theMostFrequentLettersList, (char) i);
} else {
frequentCharacterCount = updateFrequencyList(frequentCharacterCount, historgram[i], theMostFrequentCharactersList, (char) i);
}
}
}
private void analyzeLine(String line) {
char[] charArray = line.toCharArray();
theLongestLineLength = updateFrequencyList(theLongestLineLength, line.length(), theLongestLinesList, line);
for (char ch : charArray) {
if(ch >= ASCII_TABLE_SIZE) {
throw new NotImplementedException();
}
if (Character.isDigit(ch)) {
digitsCount++;
} else if (Character.isLetter(ch)) {
lettersCount++;
if (Character.isLowerCase(ch)) {
lowerCaseLettersCount++;
} else {
upperCaseLettersCount++;
}
} else if (Character.isWhitespace(ch) || Character.isISOControl(ch)) { //because some whitespace character can be also isoControl
if (Character.isWhitespace(ch)) {
whiteSpacesCount++;
}
if (Character.isISOControl(ch)){
isoControlCharactersCount++;
}
}
historgram[ch]++;
}
}
private <T> int updateFrequencyList(int actualMaxCount, int newComparingCount, List<T> frequencyList, T newValue){
if (actualMaxCount < newComparingCount) {
actualMaxCount = newComparingCount;
frequencyList.clear();
frequencyList.add(newValue);
} else if (actualMaxCount == newComparingCount) {
frequencyList.add(newValue);
}
return actualMaxCount;
}
public void resetStatistics() {
digitsCount = 0;
lettersCount = 0;
lowerCaseLettersCount = 0;
upperCaseLettersCount = 0;
whiteSpacesCount = 0;
isoControlCharactersCount = 0;
theLongestLinesList = new ArrayList<>();
theLongestLineLength = -1;
averageLineLength = 0;
totalLinesLengthSum = 0;
totalLinesCount = 0;
theMostFrequentLettersList = new ArrayList<>();
theMostFrequentCharactersList = new ArrayList<>();
frequentLetterCount = -1;
frequentCharacterCount = -1;
historgram = new int[ASCII_TABLE_SIZE];
Arrays.fill(historgram, 0);
}
@Override
public String toString() {
return "TextAnalyzer{" +
"\n digitsCount=" + digitsCount +
"\n, lettersCount=" + lettersCount +
"\n, lowerCaseLettersCount=" + lowerCaseLettersCount +
"\n, upperCaseLettersCount=" + upperCaseLettersCount +
"\n, whiteSpacesCount=" + whiteSpacesCount +
"\n, controlCharactersCount=" + isoControlCharactersCount +
"\n, theLongestLinesList='" + theLongestLinesList +
"\n, averageLineLength=" + averageLineLength +
"\n, theMostFrequentLettersList=" + theMostFrequentLettersList +
"\n, theMostFrequentCharactersList=" + theMostFrequentCharactersList +
"\n}\n";
}
public int getDigitsCount() {
return digitsCount;
}
public int getLettersCount() {
return lettersCount;
}
public int getLowerCaseLettersCount() {
return lowerCaseLettersCount;
}
public int getUpperCaseLettersCount() {
return upperCaseLettersCount;
}
public int getWhiteSpacesCount() {
return whiteSpacesCount;
}
public int getIsoControlCharactersCount() {
return isoControlCharactersCount;
}
public List<String> getTheLongestLinesList() {
return theLongestLinesList;
}
public double getAverageLineLength() {
return averageLineLength;
}
public List<Character> getTheMostFrequentLettersList() {
return theMostFrequentLettersList;
}
public List<Character> getTheMostFrequentCharactersList() {
return theMostFrequentCharactersList;
}
}
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
TextAnalyzer textAnalyzer = new TextAnalyzer();
try {
String text = new String(Files.readAllBytes(Paths.get("cesta k suboru")));
textAnalyzer.readText(text);
System.out.println(textAnalyzer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
+20 Zkušeností
+2,50 Kč

Zobrazeno 4 zpráv z 4.