Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i do "cin" in Java? - Please Help!!! 4

Status
Not open for further replies.

qb828

Programmer
Sep 27, 2000
98
US
hello! how are you?
i am a beginner in java. i know some C++. i have to write codes for the game called "Mancala".i found some sources codes other people wrote.but i have to write my own.And their codes are too hard to understand for me right now.
i have to write in text version and in graphic version.
i know a little java and this project is over my head.

my question is how do you do "CIN" in java?

i need a user to enter some value and use that value in my algorithm.

what do you use if you do it in java application and if you do it in java applet? (in simplest way) or is it same for both application and applet?

i know a little dialog box(JoptionPane, etc.)
thanks for your help in advance.
 
That would be System.in.

Or better yet, use the Swing JOptionPane.showInputDialog component method.

Brian
 
This should be helpful... can you understand this example?

// BufferedReader resides in the java.io package
import java.io.*;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter A Value: "); // this is a cout
System.out.flush(); // flushing the out buffer
String theValue = in.readLine(); // mocking cin
 
thanks brian.

i have a questiion about JOptionPane.
is that used in applet only, right?

i have to code in java application, too.

so should i just use "System.In.Read()"?
does is take integer too?
(user enters say, integer value '7')

ex) int num=System.In.Read();
thanks again for the help.

Q
 
qb828,

Basically, when you do a 'in.readLine()', you are planning to read in a line of text, you are actually calling the method of the class BufferedReader which you have instantiate earlier when you do this :

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

In case you might not understand the above codes, it is actually the same as :

InputStreamReader isr = new InputStreamReader();
BufferedReader in = new BufferedReader(isr);

So when you do a 'String temp = in.readLine();', you are actually trying to read from commandline instead of from the applet. Also, when you do a 'in.readLine()', the parameter returned is a String. So if you do a 'int temp = in.readLine();', there would be compilation error.

If you want to read in a int from commandline, look at the following codes :

try
{
int temp = (new Integer((String)in.readLine())).intValue();
}
catch (NumberFormatException e)
{
System.out.println("Input is not a number.");
}

For JOptionPane, it could be used in both UI applications and applets. So which you are going to use will depend on whether you are coding a UI or not.

Regards,
Leon
 
hello!
my java programs won't run since yesterday which ran before.

something has to do with my jdk1.2 compiler. it did worked until yesterday. but now none of my java program runs. they all compile but when try to execute them .it gives error message saying"Exception in thread "main"java.lang.NOclassDefFOundError:"myjava file name"
what did i do? now all my java programs won't work which ran before.

even simple helloWorld.java won't run!!!

public class HelloWorld{

public static voic main (String[] args){

System.out.println("Hello World!!");
}}
 
Hi,

When you got the error, java.lang.NOclassDefFoundError, it means that the jvm is unable to find the class file. From your example, it might be typo thou, but the name of the .java file is different from the name you specified in your codes.

Like if you named your class HelloWorld, in your codes, it must be public class HelloWorld. It is case sensitive.

Another thing is that you type public static voic main(..)
Please make sure that it is public static void main(..) and nothing else.

Lastly, your classpath might have been altered by one program or another. So maybe you would like to check that as well.

If it still doesn't work, then I guess you would have to UnInstall JDK and install and reconfigure JDK again.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
The Java piece of QuickTime for Windows sets up its own CLASSPATH variable. If you recently installed or updated QuickTime, or an application (like a game) that uses QuickTime, that might have killed your CLASSPATH.

If you're in Win9x, you'll need to edit your autoexec.bat. In WinNT, adjust the system properties. Win2k and WinME might have different methods to change that variable.

If you want to retain the QuickTime setting and make the JDK work, you'll want to consult the JDK docs. If you don't care about QuickTime, just SET CLASSPATH=

Of course, there may be other forces at work, but I have experienced what I've described a couple of times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top