How to accept user logins in eclipse?

Hi, how to accept user inputs using eclipse. Like the command line, we do

C: / java javaApp (arguments here). How to force eclipse to accept user input.?

+5
source share
4 answers

Run → Run Configurations → Arguments (this is the second tab on the right) → Program Arguments

+9
source

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

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

and then add in.readLine () next to any variable you want to accept as input from Runtime. Suppose you want to initialize the variable count with a value of 1, then it should be written as

int count = in.readLine(); The value 1 must be entered in the console after starting the program

+2

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 an integer or double value as input here, how do you do it. I will just give an example of int input, which you just replace 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);}
0
source

This will print the number entered by the 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
source

Source: https://habr.com/ru/post/1790375/


All Articles