Reading lines in a file, but not terminating

Hello, I did a code to iterate through a text file. I am doing it with Iterator<String>. The problem is when I run the code it doesnt terminate and shows mw new line each time. Normally also the lines that should be shown are shown, but I dont know where this new lines come, if my file has only a few.
47 Replies
JavaBot
JavaBot8mo ago
This post has been reserved for your question.
Hey @john! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
john
johnOP8mo ago
I can post the code also, help appreciated
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
public class FileIterable implements Iterable<String> { //field representing the name of the file private final String fileName; /** * Constructor for creating a new FileIterable object * * @param fileName name of the text file */ public FileIterable(String fileName) { this.fileName = fileName; } /** * Iterates through text file * * @return Iterator */ @Override public Iterator<String> iterator() { return new Iterator<String>() { //field representing a line private String line; { In.open(fileName);//opening the file if (!In.done()) { //checking if file is successfully open throw new UnsupportedOperationException("File could not be opened."); } line = In.readLine();//reading the line in the file } /** * Checking if there is an another line in the file * @return true if there is a next line */ @Override public boolean hasNext() { return this.line != null; } /** * Returning the next line * @return String next line in the file */ @Override public String next(){ if(!hasNext()) { //throwing a exception if there is no next in the file throw new NoSuchElementException("There is no next line."); } String currentLine = line; line = In.readLine(); //reading the next line return currentLine.replace("\r", ""); } }; } /** * Closes a text file */ public void close() { In.close(); } }​
JavaBot
JavaBot8mo ago
Please format your code to make it more readable. For java, it should look like this:
​`​`​`​java
public void foo() {

}
​`​`​`​
​`​`​`​java
public void foo() {

}
​`​`​`​
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
yes I dont see anything wrong. If I debug it shows my empty strings
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
yes output: ​This is a file with multiple lines. It has five lines in total. The previous was an empty line.​ <newline> <newline> this lines and than infinitely many new lines even the file has only these
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
Its in the project included, pre programmed.
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
Idk I not seeing where the error is.
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
​public static String readLine() { StringBuffer b = new StringBuffer(); char c = read(); while (done && !LSSTRING.contains(String.valueOf(c))) { b.append(c); c = read(); } int i = 0; while (c == LS[i]) { ++i; if (i >= LS.length) { break; } c = read(); } if (b.length() > 0) { done = true; } latestValue = b.toString(); latestMethod = "readLine()"; return b.toString(); }​​ this is the method of readLine But I am not supposed to change anything there this is in In Class
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
empty string
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
I know somthing in javadoc but I think the only problem is at my part of the code if I do a "return this.line != null && !line.isEmpty(); " at method hasNext than it works. But the problem is that the file has a empty line which should be displayed
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
ok i get it
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
"Depending on the platform/operating system, problems with carriage return (\r) may occur when reading in the file for the FileIterable. To prevent this, you can simply call a line.replace("\r", "") on the line directly when reading in a line, which filters out the carriage return." this is what they said thats why I am using it
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
public static boolean "Check if the previous operation was successful. This method returns true if the previous read operation was able to read a token of the requested structure. It can also be called after open() and close() to check if these operations were successful. If done() is called before any other operation it yields true"
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
I dont know that. was just so given to me
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
hmm. if the string doesnt contains the value of c then... so it checks if the previous call was successful and if c was contained or smth like this so each time should be a different value or I am seeing it wrong
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
if done returns false
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
john8mo ago
@Override
public boolean hasNext() {
return In.done();
}
public boolean hasNext() {
return In.done();
}
I think i corrected it
This message has been formatted automatically. You can disable this using /preferences.
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
It seems to be working
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
thank you very much for leading me to the solution
JavaBot
JavaBot8mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
john
johnOP8mo ago
and not that you just said me whats wrong
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
"if done returns false" somehow
JavaBot
JavaBot8mo ago
Before your post will be closed, would you like to express your gratitude to any of the people who helped you? When you're done, click I'm done here. Close this post!.
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
"public class FileIterable implements Iterable<String>, Autoclosable {". Ok thanks
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
john
johnOP8mo ago
Ok, thanks!
JavaBot
JavaBot8mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server