Cześć wszystkim. Mam taki błąd jak w temacie. Nie mam jeszcze odpowiedniej wiedzy w tym temacie, ale na internecie nie znalazłem, tego, że ktoś miał podobny błąd. Pomożecie z rozwiązaniem tego błędu ?
Błąd jest taki:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Iterator
at main.FileContent.charIterator(FileContent.java:40)
at main.CharIterator.<init>(CharIterator.java:17)
at main.FileContent.main(FileContent.java:84)
Mam:
interfejs IterableText - który posiada dwie metody
import java.util.Iterator;
public interface IterableText {
Iterator<String> charIterator();
Iterator<String> wordIterator();
}
klasa FileContent - odpowiedzialna za czytanie plików tekstowych i dostarczanie jej treści poprzez implementację interfejsu IterableText. Treść będzie dostarczana za pomocą metod charIterator () i wordIterator ().
To właśnie w niej umieściłem metodę statyczną main, w celu przetestowania programu, chciałem sprawdzić czy iteracja działa
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
public class FileContent implements IterableText{
public String pathToFile;
public ArrayList<String> splittedLetters;
public ArrayList<String> splittedWords;
public FileContent(String pathToFile){
this.pathToFile = pathToFile;
this.splittedLetters = new ArrayList<String>();
this.splittedWords = new ArrayList<String>();
}
public Iterator<String> charIterator(){
try (BufferedReader br = new BufferedReader(new FileReader(this.pathToFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] splittedText = line.split("");
for(String letter: splittedText){
if(!letter.equals(" ")){
splittedLetters.add(letter);
}
}
Iterator itr = splittedLetters.iterator();
while(itr.hasNext()){
return (Iterator<String>) itr.next();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public Iterator<String> wordIterator(){
try (BufferedReader br = new BufferedReader(new FileReader(this.pathToFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] splittedText = line.split(" ");
for(String word: splittedText){
if(!word.equals(" ")){
splittedWords.add(word);
}
}
Iterator itr = splittedWords.iterator();
while(itr.hasNext()){
return (Iterator<String>) itr.next();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String getFilename(String[] args){
return args[2];
}
public static void main(String[] args){
FileContent fileContent = new FileContent("/home/hubert/IdeaProjects/TextAnalysis/src/main/test.txt");
CharIterator charIterator = new CharIterator(fileContent);
System.out.println(charIterator.next());
}
}
klasa CharIterator - iteruje po pojedyńczych literach tekstu i implementuje next () i hasNext () z interfejsu Iterator.
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class CharIterator {
public FileContent fileContent = new FileContent("/home/hubert/IdeaProjects/TextAnalysis/src/main/test.txt");
public int currentIndex = 0;
//String[] arrayLetters = fileContent.splittedLetters.toArray(new String[fileContent.splittedLetters.size()]);
Iterator<String> itr_string = fileContent.charIterator();
public List<String> ListLetters = StreamSupport.stream(Spliterators.spliteratorUnknownSize(itr_string, Spliterator.ORDERED), false).collect(Collectors.<String> toList());
public String[] arrayLetters = ListLetters.toArray(new String[ListLetters.size()]);
public int currentSize = arrayLetters.length;
public CharIterator(FileContent fileContent){
this.fileContent = new FileContent(fileContent.pathToFile);
}
public boolean hasNext(){
return currentIndex < currentSize && arrayLetters[currentIndex] != null;
}
public String next(){
return arrayLetters[currentIndex++];
}
}
klasa WordIterator - iteruje po słowach tekstu. Implementuje next () i hasNext () z interfejsu Iterator.
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class WordIterator {
public FileContent fileContent = new FileContent("/home/hubert/IdeaProjects/TextAnalysis/src/main/test.txt");
public int currentIndex = 0;
//String[] arrayWords = fileContent.splittedWords.toArray(new String[fileContent.splittedWords.size()]);
Iterator<String> itr_string = fileContent.charIterator();
public List<String> ListWords = StreamSupport.stream(Spliterators.spliteratorUnknownSize(itr_string, Spliterator.ORDERED), false).collect(Collectors.<String> toList());
public String[] arrayWords = ListWords.toArray(new String[ListWords.size()]);
public int currentSize = arrayWords.length;
public WordIterator(FileContent fileContent){
this.fileContent = new FileContent(fileContent.pathToFile);
}
public boolean hasNext(){
return currentIndex < currentSize && arrayWords[currentIndex] != null;
}
public String next(){
return arrayWords[currentIndex++];
}
}
I jeszcze jedno pytanie -> czy ten kod poniżej jest prawidłowo napisany ??
public List<String> ListWords = StreamSupport.stream(Spliterators.spliteratorUnknownSize(itr_string, Spliterator.ORDERED), false).collect(Collectors.<String> toList());
public String[] arrayWords = ListWords.toArray(new String[ListWords.size()]);
albundy