Tech Support Guy banner
Status
Not open for further replies.
1 - 6 of 6 Posts

· Registered
Joined
·
145 Posts
Discussion Starter · #1 ·
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.
 

· Registered
Joined
·
6,693 Posts
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]
 

· Registered
Joined
·
145 Posts
Discussion Starter · #3 ·
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?
 

· Registered
Joined
·
6,693 Posts
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';
 

· Registered
Joined
·
145 Posts
Discussion Starter · #5 ·
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);
 

· Registered
Joined
·
6,693 Posts
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);
 
1 - 6 of 6 Posts
Status
Not open for further replies.
Top