Hallo,
Ich bin gerade etwas am verzweifeln weil ich den Fehler einfach nicht finden und langsam das Gefühl habe ich sehe den Wald vor lauter Bäumen nicht mehr.
Ich versuche einen txt nach einem bestimmten Wort zu durchsuchen und dann in einer neuen Txt das Ergebniss auszugeben. Soweit klappt auch eigentlich alles einlesen klappt, genauso wie erstellen der neuen Datei. Doch die equals Methode funktioniert einfach nicht und ich weiß nicht warum. Meiner Meinung nach ist es so korrekt aber es kommt immer false raus. Was nicht sein kann da ich weiß das Beide Wörter enthalten sind im Text.
Ich hoffe mir kann hier jemand helfen, vielen Dank schon einmal.
Liebe Grüße
Ich bin gerade etwas am verzweifeln weil ich den Fehler einfach nicht finden und langsam das Gefühl habe ich sehe den Wald vor lauter Bäumen nicht mehr.
Ich versuche einen txt nach einem bestimmten Wort zu durchsuchen und dann in einer neuen Txt das Ergebniss auszugeben. Soweit klappt auch eigentlich alles einlesen klappt, genauso wie erstellen der neuen Datei. Doch die equals Methode funktioniert einfach nicht und ich weiß nicht warum. Meiner Meinung nach ist es so korrekt aber es kommt immer false raus. Was nicht sein kann da ich weiß das Beide Wörter enthalten sind im Text.
Ich hoffe mir kann hier jemand helfen, vielen Dank schon einmal.
Liebe Grüße
Java:
class WordCounter
{
public static void main(String[] args) throws IOException
{
File f1=new File("c:\\Users\\...\\input.txt"); //Creation of File Descriptor for input file
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr); //Creation of BufferedReader object
PrintWriter out = new PrintWriter(new File("test.txt")); // Erstellen der Ausgabe Txt Datei
String[] words=null; //Intialize the word Array and null is a placeholder = no selected value
String s;
String[] input = {"Grün", "Gelb"}; // Input words to be searched
int count = 0; //Intialize the count to zero
for (int i=0; i < input.length; i++)
{
f1=new File("c:\\Users\\...\\input.txt"); //Creation of File Descriptor for input file
fr = new FileReader(f1); //new file defined = reset file to beginning
br = new BufferedReader(fr); //buffered
s = "MARKER";
while((s = br.readLine()) !=null) //Reading Content from the file
{
words= s.split(" ");//Split the word using space
for (String word : words) //for each (after every run in "word" is the value from "words" saved)
{
if ( word.equals(input[i])) //Search for the given words in input
{
count ++; //If Present increase the count by one
out.println(s);
}
}
}
if(count!=0) //Check for count not equal to zero
{
System.out.println("The word " +input[i]+ " is present for "+count+ " Times in the file");
count = 0;
}
else
{
System.out.println("The word " +input[i]+ " is not present in the file");
}
}
fr.close(); //close File
out.close(); //close new made File
}
}
Zuletzt bearbeitet von einem Moderator: