Tech Support Guy banner
  • Please post in our Community Feedback thread for help with the new forum software! If you are having trouble logging in, please Contact Us for assistance.
Status
Not open for further replies.
1 - 3 of 3 Posts

·
Registered
Joined
·
145 Posts
Discussion Starter · #1 ·
Hi,
Im trying to ask the user to type text.The object of the program is that if teh user wants to exit the program, he has to type the letter 'q'. The code is below:
import java.io.*;
class quits
{
public static void main(String args[]) throws exception
{
System.out.println("Type anything you wish");
System.out.println("Type \"q\" to exit out of the program");
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
String st[]=new String[200];
for(j=0;j<200;j++)
{
String st[j]=obj.readLine();
if (st[j] == 'q') break;
}
System.out.println("Text entered so far is as below");
for(j=0;j<200;j++)
{
System.out.println(st[j]);
}
}
}
But I get errors that say as below:
quits.java:12: ']' expected
String st[j]=obj.readLine[];
^

1 error

COuld anyone explain the mistake in the program?
 

·
Registered
Joined
·
6,649 Posts
The syntax of the string comparaison is not valid.
Here is the correct syntax in your example :

Code:
[SIZE=2][B]if (st[j].equals("q")) break;[/B][/SIZE]
There's an exception : when you have to compare a string with null, then you must use the == operator.

Also, the String st[j]=obj.readLine(); line must be replaced by st[j]=obj.readLine(); .
 
1 - 3 of 3 Posts
Status
Not open for further replies.
Top