You are going to want to start off by importing the Scanner object in the util package (i use wildcard to import the whole package, you don't have to if you don't want to).
Code:
import java.util.*;
public class Reca {
public static void main(String[] args) {
}
}
Next we are going to want to create our Scanner object and pass a parameter of System.in to it.
Code:
import java.util.*;
public class Reca {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
}
There are various types of ways to grab input, I'm not going to go into detail on how to use each one, but I am going to show you how to grab a string. (Since it probably would be the most useful data type seeing as you can do anything to a string!)
You want to create a variable and assign it the Scanner reference ("input") and the method we are going to use is "nextLine()" to grab the line of input.
Code:
import java.util.*;
public class Reca {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
}
}
That is pretty much how to use the Scanner object to grab input from the keyboard, in a nutshell!
here is a test code to show you that it works
Code:
import java.util.*;
public class Reca {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("What is your name? ");
String s = input.nextLine();
System.out.println("Your name is " + s);
}
}
If you have any troubles, or questions feel free to ask! :]