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
⌛
This post has been reserved for your question.
Hey @john! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
I can post the code also, help appreciated
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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();
}
}
Please format your code to make it more readable. For java, it should look like this:
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
yes
I dont see anything wrong. If I debug it shows my empty strings
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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 theseUnknown User•8mo ago
Message Not Public
Sign In & Join Server To View
Its in the project included, pre programmed.
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
Idk I not seeing where the error is.
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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 ClassUnknown User•8mo ago
Message Not Public
Sign In & Join Server To View
empty string
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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•8mo ago
Message Not Public
Sign In & Join Server To View
ok i get it
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
"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•8mo ago
Message Not Public
Sign In & Join Server To View
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•8mo ago
Message Not Public
Sign In & Join Server To View
I dont know that.
was just so given to me
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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•8mo ago
Message Not Public
Sign In & Join Server To View
if done returns false
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
@Override
I think i corrected it
This message has been formatted automatically. You can disable this using
/preferences
.Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
It seems to be working
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
thank you very much for leading me to the solution
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.
and not that you just said me whats wrong
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
"if done returns false" somehow
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•8mo ago
Message Not Public
Sign In & Join Server To View
"public class FileIterable implements Iterable<String>, Autoclosable {". Ok thanks
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
Ok, thanks!
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.