Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java While Loop and If statement

  1. Apr 13, 2014 #1
    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 (Text):

    while(x == ' ' || x== '\n')
    {
        if(x== ' ')
        {
            //...
        }
           
        if(x== '\n')
        {
            //...
        }
    }  
     
     
    Last edited by a moderator: Apr 13, 2014
  2. jcsd
  3. Apr 13, 2014 #2

    Mark44

    Staff: Mentor

    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.
     
  4. Apr 13, 2014 #3

    AlephZero

    User Avatar
    Science Advisor
    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".
     
  5. Apr 13, 2014 #4
    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.

    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. =/
     
  6. Apr 13, 2014 #5

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Code (Text):
    if(x== ' ')
    {
        // ...
    } else {
        // we have \n
    }
    If the while works as intended, this is a workaround.
     
  7. Apr 13, 2014 #6
    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.
     
  8. Apr 13, 2014 #7

    AlephZero

    User Avatar
    Science Advisor
    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."
     
  9. Apr 13, 2014 #8
    It's not clear where the problem lies from that small amount of code.
    You should post a complete runnable example.
    e.g.
    Code (Text):
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;

    public class Read {
        public static void main(String[] args) throws IOException {
            BufferedReader in = new BufferedReader(new FileReader("text.txt"));
            int emptyLines = 0;
            String line = in.readLine();
            while(line != null && line.matches("\\s*")) {
                emptyLines++;
                line = in.readLine();
            }
            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);
                line = in.readLine();
                n++;
            }
            in.close();
        }
    }
    if text.txt looks like this
    Code (Text):





    abc
    def
    ghi
    The output will be
    Code (Text):
    There are 4 empty lines at the start of the file.
    5. line = abc
    6. line = def
    7. line = ghi
     
  10. Apr 13, 2014 #9

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java While Loop and If statement
  1. C while loop issue (Replies: 26)

  2. Do while loop (Replies: 6)

Loading...