Eclipse user input

Eclipse user input

Eclipse does user input according to the given java packages on the oracle website, which is where you download eclipse.The specific java package is java.util.Scanner.  So, the first thing you need to do is import the package if the version of eclipse doesn’t import it automatically.

Which is done by doing:

import java.util.Scanner;

In the space before the class header along with any other imports.

The second thing you need to do is make a scanner object

Which is typically done in the public method and is done by a piece of code along the lines of:

Scanner varname= new Scanner(System.in);

The next step is to typically make the prompt. Which is normally done with:

System.out.print(“prompt”);

The final step is to turn the answer to the prompt into user input so doing something along the lines of:

int k = varname.nextInt();

So all together it is something along the lines of:

import java.util.Scanner;

public class Name

{

public static void main(String[] args)

{

Scanner varname= new Scanner(System.in);

System.out.print(“prompt”);

int k = varname.nextInt();

varname.close();

}

}

varname.close (); is just closing the scanner so you can’t put in anymore input, but I found it isn’t normally completely necessary.

The scanner class can also be used from what I can tell, since I have never used it this way personally, to get things from files on your computer or to take things out of a string by using the delimiter part of the class.

A link to the scanner package is below:

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

One thought on “Eclipse user input

Comments are closed.