1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

I/O handling

Discussion in 'Software Development' started by Caliraj, Jan 17, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    Hi,
    This is a program to ask the user to type any text and incase he has to exit out of the program, he simply has to type 'q' or 'Q'.
    import java.io.*;
    class readline
    {
    public static void main(String args[]) throws IOException
    {
    char c;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter any text and type \"q\" or \"Q\" to exit") ;
    do
    {
    c=(char)br.read();
    }while((c!='Q') || (c!='q'));
    }
    }

    The output is such that the program never exits unless I force a Control + C. Im not able to find the error in the logic.
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Code:
    [SIZE=2]while((c!='Q') || (c!='q'));[/SIZE]
    The above statement is just like my sig : it's an infinite loop.
    The correct statement :

    Code:
    [SIZE=2]while((c!='Q') && (c!='q'));[/SIZE]

    In boolean algebra :

    Code:
    [SIZE=2]
    !(A && B) = !A || !B
    
    !(A || B) = !A && !B[/SIZE]
     
  3. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    Thanks chicon for the answer. I have a quaery regarding the same program...Now instead of a do..while loop, I tried to use the while loop as below :
    import java.io.*;
    class readline
    {
    public static void main(String args[]) throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter any text and type \"q\" or \"Q\" to exit") ;
    char a;
    while((a!='q') && (a!='Q'))
    {
    a=(char)br.read();
    }
    }
    }
    Now i always get an error as follows:
    readline.java:9: variable a might not have been initialized
    while((a!='q') && (a!='Q'))
    I have already declared the character variable 'a'.Why do I get this error?
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Just before the while loop, the character a must be initialized with a value because Java doesn't allow to compare a null object with other values than null.
    Therefore, you may complete the declaration of a with a value different from those used in the comparaison : char a = ' '; or char a = '1';
     
  5. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    Thanks once again chicon....guess im coming back to you too often:confused: .....This is another code thats giving me errors :
    import java.io.*;
    class filenames
    {
    public static void main(String args[]) throws IOException
    {
    BufferedWriter obj=new BufferedWriter(new FileWriter("abcd.txt"));
    String str="Good Morning";
    obj.write(str);
    obj.close();
    BufferedReader ob=new BufferedReader(new FileReader("abcd.txt"));
    String st;
    while((st==ob.readLine())!=null) System.out.println(st);
    ob.close();
    }
    }
    Here is the error:
    filenames.java:12: incomparable types: boolean and int
    while((st==ob.readLine())!=-1) System.out.println(st);
     
  6. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    The == is invalid in your expression : you're comparing st with ob.readLine() and it returns you a boolean.

    You have to use a single = to assign the value of ob.readLine() to the string st, the whole expression gives an integer.

    Therefore, the correct syntax is :

    while((st=ob.readLine())!=-1) System.out.println(st);
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/536197

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice