开发者

Input values to a normal JAVA SE program

I'm new to JAVA and I'm currently trying out both Netbeans 开发者_开发问答and Eclipse.

I'm confused about inputting values to my JAVA application.I see the output getting displayed on the output window in Eclipse ...but,I don't know how to input values ...??

In a normal console application in .Net,we can input values through the console window (Command Prompt Window) ,but I don't see any command prompt window coming up when I run my JAVA program to Input values or Type Anything.How do I do this?

Thanks.


If I remember correctly, you can actually type in Eclipse's Java console and that will be sent through the standard input stream, System.in (probably depending on the application's run configuration).

However the standard way of doing this is to pass a number of strings on the command line, which will appear as the elements of the String[] parameter to your application's main method.

Alternatively you can get input in via flat files, from a database, through sockets etc. though this is, as you might expect, a lot more complex and should certainly not be considered for a "Hello World" type application. Java web services, for example, do take their input via network sockets rather than via the command line as this latter would restrict them somewhat. ;-)


Using eclipse, you need to update the Run Configuration for a given Class. Provide any arguments you want to pass into the main method.


In netbeans there is an output window that behaves like a console. That is, if your program requests keyboard input you can type directly in the output window.


You can use two ways.

1) The first is from the console.

System.out.println("Please give number");
Scanner s = new Scanner(System.in);
int number = s.nextInt(); // Or s.nextString() to get the string.
System.out.println("You typed " + number);

2) The second is to show a dialog

String input = JOptionPane.showInputDialog("Please give number");

Hope this helps.


In Eclipse, you just type in the console where you get the results (e.g. System.out.println(...)). Your text will be colored in green.


You can type in the same space where you see output (it's lake windows console but withe and embedded into IDE).

Also You can run your APP form console: java ClassName


Use System.console() in Java 6.


To get the no. from user use it.

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

and to set the variable location of that no.

int variablename=in.readLine(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜