• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with my code

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
srry but i just got started so im still confused
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
scott Ha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to loop the prgm to repeat the word hi, until some1 presses q or Q
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find the I/O Trail from the Java Tutorial helpful in figuring out how to fix your problem.

Good luck!

Layne
 
reply
    Bookmark Topic Watch Topic
  • New Topic