Java While Loop and If statement

• Java
I am reading an input file that has blank lines. x reads character by character. x is equal to a new line so it goes into the while loop but skips the if x == '\n'. It's suppose to go inside the if statement but it doesn't. I tried debugging the code and printed out to double check x is holding a new line. What am I doing wrong?

Code:
while(x == ' ' || x== '\n')
{
if(x== ' ')
{
//...
}

if(x== '\n')
{
//...
}
}

Last edited by a moderator:

Related Programming and Computer Science News on Phys.org
Mark44
Mentor
I am reading an input file that has blank lines. x reads character by character. x is equal to a new line so it goes into the while loop but skips the if x == '\n'. It's suppose to go inside the if statement but it doesn't. I tried debugging the code and printed out to double check x is holding a new line. What am I doing wrong?

Code:
while(x == ' ' || x== '\n')
{
if(x== ' ')
{
//...
}

if(x== '\n')
{
//...
}
}
The newline character gets translated by the OS into two characters in Windows (ASCII 13 and ASCII 10) and one character in Linux, Unix, and related OSes (ASCII 13, I believe). If you change your second if condition to x == '\0xD', which is 13 in hex, I think you'll find the character you're looking for.

BTW, you're adding too many TAB characters in your code. The braces should be indented at the same level as the while, if, for, etc they're associated with. If you indent the braces and indent the statements inside the braces, parts of your code soon extend way to the right, making it necessary for someone reading it to have to scroll to see all of the line.

Indenting code where appropriate is good to do, but there can be too much of a good thing.

AlephZero
Homework Helper
Jave should treat end-of-lines the same way as C and C++. The operating system specific details are suppressed, and your code always see a single '\n' character, unless you open the file as raw binary data (which you shouldn't, if it contains text split into "lines").

I suspect the problem is in some code the OP hasn't shown us. There must be some way(s) to exit from the while loop, otherwise any blank or newline character would loop for ever. Or, depending how the file is read, x might never be set to a newline character.

As a detail, if x can only have two values inside the while loop (i..e its value doesn't change in the code we can't see), if would be neater to replace "if (x == '\n')" with "else".

The newline character gets translated by the OS into two characters in Windows (ASCII 13 and ASCII 10) and one character in Linux, Unix, and related OSes (ASCII 13, I believe). If you change your second if condition to x == '\0xD', which is 13 in hex, I think you'll find the character you're looking for.

BTW, you're adding too many TAB characters in your code. The braces should be indented at the same level as the while, if, for, etc they're associated with. If you indent the braces and indent the statements inside the braces, parts of your code soon extend way to the right, making it necessary for someone reading it to have to scroll to see all of the line.

Indenting code where appropriate is good to do, but there can be too much of a good thing.
Oh the tabs were due to copy and paste but in my code it is indented correctly. I printed out print statements and it does read x as 13. It won't let me change the if to '\0xD' because it is not a character constant.

Jave should treat end-of-lines the same way as C and C++. The operating system specific details are suppressed, and your code always see a single '\n' character, unless you open the file as raw binary data (which you shouldn't, if it contains text split into "lines").

I suspect the problem is in some code the OP hasn't shown us. There must be some way(s) to exit from the while loop, otherwise any blank or newline character would loop for ever. Or, depending how the file is read, x might never be set to a newline character.

As a detail, if x can only have two values inside the while loop (i..e its value doesn't change in the code we can't see), if would be neater to replace "if (x == '\n')" with "else".
I created a blank text file in eclipse and pressed enter three times (three new lines). The above is the whole while loop everything that is inside the if/else statement is to assign x with a new value (the next character). I switched it to else but it's still the same. =/

mfb
Mentor
Code:
if(x== ' ')
{
// ...
} else {
// we have \n
}
If the while works as intended, this is a workaround.

Code:
if(x== ' ')
{
// ...
} else {
// we have \n
}
If the while works as intended, this is a workaround.
I debugged it again and it doesn't go into the while loop anymore.

In the first line x = 13.
Second line x = 10.
Third line x = 10.

AlephZero
Homework Helper
There are probably least 57 different ways to read characters from a file in Java. If you don't show us EXACTLY what ALL the relevant code says, this is just a guessing game IMO.

A good principle for debugging code is, "if there is nothing wrong with the code you are looking at, you should be looking somewhere else."

It's not clear where the problem lies from that small amount of code.
You should post a complete runnable example.
e.g.
Code:
import java.io.BufferedReader;
import java.io.IOException;

public static void main(String[] args) throws IOException {
int emptyLines = 0;
while(line != null && line.matches("\\s*")) {
emptyLines++;
}
System.out.println("There are " + emptyLines + " empty lines at the start of the file.");
int n = emptyLines + 1;
while(line != null) {
System.out.println(n + ". line = " + line);
n++;
}
in.close();
}
}
if text.txt looks like this
Code:
abc
def
ghi
The output will be
Code:
There are 4 empty lines at the start of the file.
5. line = abc
6. line = def
7. line = ghi

Mark44
Mentor
Oh the tabs were due to copy and paste but in my code it is indented correctly. I printed out print statements and it does read x as 13. It won't let me change the if to '\0xD' because it is not a character constant.
This would be a character constant in C, C++, C# (I think), and a few other languages. Java can use Unicode character constants, such as '\u000D'. I believe that would work.