I have a basic read from a .txt file but I can't figure it out why the string is different ?!
my text file has 10 lines of "zzz" on each line. when I try to compare it with the string "zzz" is says they are not the same. why ? Is it because the reader is trying to read until the next separator ? So, now the string has my "zzz" + separator ? How can I delete that from my string ? Can I make something like print(myString.length()-1) ?
This is the code. I looked over something similar but I got nothing.
try {
Scanner rf = new Scanner(new BufferedReader(new FileReader("Data Saved.txt")));
String fileLine;
while ( rf.hasNext()) {
fileLine = rf.next();
if(fileLine == "zzz") System.out.print("yes ");
else System.out.print("no ");
System.out.println(fileLine);
}
rf.close();
} catch (IOException e) {
System.out.println("Error IOException is: " + e);
}
The output is no zzz , 10 times.
You should use readLine
method to avoid the line breaks, see
http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#readLine--