开发者

how to take user inputs in eclipse?

开发者 https://www.devze.com 2023-02-09 03:29 出处:网络
Hi how to take user inputs using eclipse. Like in command prompt we do C:/ java javaApp (arguments here). How to make eclipse to take inputs from the user开发者_StackOverflow中文版.?Run --> Run Confi

Hi how to take user inputs using eclipse. Like in command prompt we do

C:/ java javaApp (arguments here). How to make eclipse to take inputs from the user开发者_StackOverflow中文版.?


Run --> Run Configurations --> Arguments (it is the 2nd tab on the right) --> Program arguments


Add this line in your program to accept user's input from the console:

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

and then add in.readLine() beside whatever variable you want to accept input from Runtime. Lets say, you want to initialize count variable to the value 1, then, it should be written as

int count = in.readLine(); The value 1 should be inputted in the console after you run the program


Here is one way to take user input in Eclips.

import java.util.Scanner; // enter the before the start of any class 
// declare the class here 
// this is an example of how to use the Scanner in a method
Public static void Username();{
Scanner Input = new scanner (System.in);
String username// declaring the username variable 
Scanner un = new Scanner(System.in); //  un is just a random variable you can choose any other variable name if you want
System.out.println("Enter Your Username");
username = un.next(); 
System.out.println("The username you entered is : " + username);}

But if you want to take a integer or double as a input here how you do it. I am just going to give an example for int input you just have replace the int with double.

import java.util.Scanner; 
// Declare the class here
// this is an example of how to use the Scanner in a method for an integer input by user
Public static void booksRead();{
Scanner Input = new scanner (System.in);
int books // declaring the books variable 
Scanner br = new Scanner(System.in); //  br is just a random variable you can choose any other variable name if you want
System.out.println("Enter how many books have you read: ");
books = br.next(); 
System.out.println("The number of books you have read is : " + books);}


This will print the number entered by user :

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner reader = new Scanner(System.in);  // Reading from System.in
    System.out.println("Enter a number: ");
    int n = reader.nextInt(); // Scans the next token of the input as an int.
    //once finished
    System.out.println(n);
    reader.close(); 
}
0

精彩评论

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

关注公众号