• 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

Import and keylistener?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have recently bought a book called "Java 2 For Dummies", this book told me about Import, but when i copied code in it It wouldnt work, and I did make sure the names of class matched, and my assignment for my summer work was to prompt a name until they pressed Q or q for Quit, but I used a Do while loop for the name, but cant get the while condition to work, I said Do
system.out.println("George")
While(System.in != 'Q')
But I dont know whats wrong please help a beginner.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi George

Java is case-sensitive, so your do-while should look something like


Could you please provide some information with regards to the error messsage that you are getting along with some of the code you think is causing the problem.
[ August 17, 2005: Message edited by: Abdulla Mamuwala ]
 
George Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my real code
import java.lang.String;
class Displayer {
public static void main(char args[]) {
String reply;
do {
System.out.println("HI");
reply = System.in;
} while (reply != "q" && reply != "Q");
}
}

and the error is this

--------------------Configuration: prgm 1 - 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.

I thinks that Im using the System.in the wrong way
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
George,

Your error says it all. The field System.in returns an InputStream whereas reply is a String. Check out the following System api.
 
George Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umm. Yea I still dont get it cause I started java so I have no idea what you meant and I read it and still dont, could you give me a sample code of how you would do it, because I took a class in Visual Basic and if I see an example I can see how to do it much easier.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well console input in java is a bit tricky,
try the following...



// Lee
 
Lee Diego
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Little comment on the above code...

The System.in is a InputStream object but
if we want to read from it we have to get
a Reader object.
We use the InputStreamReader as our Reader but this
object does not give us a method that read String's
(which is what we want) so we send this object to
BufferedReader that has the function readLine() which
returns a String and since that is what we want we are done.

Since the readLine() method throws an exception we
have to add the try-catch.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this.


System.in.read() reads one character at a time. That is why the data type on the reply is char.
Also notice how the q and Q and are enclosed in single qoutes. Single qoutes make then a char instead of a string that the double qoutes would do.

Make sense?

hth, Steve
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lee's example is much better than mine as it deals with Strings which may have been what you were looking for.

I hope I didn't confuse you.

Steve
 
George Han
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx to both of you, I understand the whole process of manipulating System.in , but you had "throws java.io.IOException", Which I do not know why you put it in there
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by George Han:
Thx to both of you, I understand the whole process of manipulating System.in , but you had "throws java.io.IOException", Which I do not know why you put it in there



This is because the read() method is also declared to throw an IOException. Alternatively, you can add a try...catch block to catch the exception and print an error message.

Layne

p.s. Go ahead and remove it and try to compile to see what it says. It's a good idea to get a feel for what different compiler errors mean and this is a good example of one that you will encounter often.
[ August 18, 2005: Message edited by: Layne Lund ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic