aspose file tools*
The moose likes Beginning Java and the fly likes Help with my code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with my code" Watch "Help with my code" New topic
Author

Help with my code

scott Ha
Greenhorn

Joined: Aug 12, 2005
Posts: 3
class Displayer {
public static void main(char args[]) {
String reply;
do {
System.out.println("HI");
reply = System.in;
} while (reply != "q" && reply != "Q");
}
}
I am getting this error
--------------------Configuration: 1st prgm - j2sdk1.4.2_08 <Default> - <Default>--------------------
C:\Program Files\Xinox Software\JCreatorV3LE\MyProjects\1st prgm\displayer.java:7: incompatible types
found : java.io.InputStream
required: java.lang.String
reply = System.in;
^
1 error

Process completed.

Im new but i need my program to stop looping when someone presses Q or q, but im lost. PLZ HELP
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
Hi Scott,
maybe you should give better title to your Question...
Anyways you are defining a String reply and try to assign the console in object into it...

as i can see you are trying to read the input, you will need to read the input from the console in object.

maybe you want to have a look at the sun api for System:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html
scott Ha
Greenhorn

Joined: Aug 12, 2005
Posts: 3
srry but i just got started so im still confused
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You have the following declaration:

This says that the reference variable named "reply" refers to a String object. Now look at the link that Jean-Sebastion gave above. This is the javadocs for the System class from the standard Java API. Scroll down until you see a box with a blue border and the title "Field Summary". Here you can see that the reference variable System.in is declared to refer to an InputStream object. Since String and InputStream are not related to each other by inheritence, you cannot assign System.in to reply.

So what is your intent here? What do you think this should do? Can you please explain further and we can give you some more information to help you along the way. I strongly suggest that you look at the Javadocs as I described above. You will notice that the word "InputStream" next to the description of the "in" variable is a link. This will lead you to a description of the InputStream class. In particular, you will find a list of the methods that are available. (Hint: you may find the read() method helpful if you are trying to read some input.)

I hope this helps clear some of the confusion. If not, please come back with some answers to my questions above and I will be glad to help you further.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
scott Ha
Greenhorn

Joined: Aug 12, 2005
Posts: 3
I am trying to loop the prgm to repeat the word hi, until some1 presses q or Q
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
Look at the error message you got:

incompatible types
found : java.io.InputStream
required: java.lang.String
reply = System.in;

The compiler is saying the it found an InputStream (System.in)
and it required a String. Look up the type or definition of System.in.
What is it? It is NOT a String, its an InputStream object that can be used to read data. So you need to figure out how to use that object to read the input from the console. Read the doc the InputStream class and see what methods it has available for reading. You'll need to use these somehow to get what the user has entered.

Some definitions to consider:
System is a class.
in is a public variable in that class.
System.in accesses that class.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You may find the I/O Trail from the Java Tutorial helpful in figuring out how to fix your problem.

Good luck!

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with my code