I/O handling

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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.
 
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]
 

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?
 
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';
 

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);
 
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);
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top