It’s briefly touched on in Learn Intermediate Java, but generally Codecademy is not going to teach much about it. The good news is it’s pretty straightforward
First, you create a Scanner object (I named it “scanner”, but it could be anything)
/* make sure you have import java.util.*; or import java.util.Scanner;
written at the top, before your class begins */
Scanner scanner = new Scanner(System.in);
Then you call a method on that Scanner object. next()
means “scan the next element and return it as a String” (that is, “the next token”, as scanned elements are called), nextInt()
means “scan the next element and return it as an int”, nextDouble
means “scan the next element and return it as a double”. As soon as your scanner reaches a delimiter, it considers it the end of the token. By default, delimiters are one or more spaces. For example
// suppose a user types in "love 2 code"
String token1 = scanner.next();
int token2 = scanner.nextInt();
System.out.println(token1);
System.out.println(token2);
// "love" and "2" get printed
or, alternatively,
String token1 = scanner.next();
String token2 = scanner.next();
System.out.println(token1);
System.out.println(token2)
// again, "love" and "2" get printed, but "2" is now a String
or, alternatively,
int token1 = scanner.nextInt();
System.out.println(token1);
/* the program returns the InputMismatchException because the first element
in "love 2 code" is not an int */
You can also call the nextLine()
method. It returns the whole line as a String
String token1 = scanner.nextLine();
System.out.println(token1);
// "love 2 code" gets printed
You can also change what your scanner considers a delimiter with the help of the useDelimiter()
method. For example
// suppose the input is now "love 2,code"
scanner.useDelimiter("\\s+|,");
/* symbol | means "or", \\s means a space, + means "any number of" (spaces,
in this case), , means, well, a comma. So overall, the expression means "any
number of spaces or a comma" */
String token1 = scanner.next();
String token2 = scanner.next();
System.out.println(token1);
System.out.println(token2);
/* "love" and "2" get printed: the first token is separated by our scanner with
a space, the second token is separated with a comma */
Remember, when we created out Scanner object we wrote System.in
as a parameter? It means we told our scanner to scan what a user types in during execution of our program. But scanner can scan more than that, for instance txt
files. We need to create a List object and pass it as a parameter. For example
/* make sure you have import java.io.*; or import java.io.File written at the
top, before your class begins. now, suppose we have a file named input.txt on
our computer which only contains this text, "1,2,3" */
File input = new File("input.txt");
Scanner scanner = new Scanner(input);
scanner.useDelimiter(",");
int token1 = scanner.nextInt();
int token2 = scanner.nextInt();
int token3 = scanner.nextInt();
System.out.println(token1);
System.out.println(token2);
System.out.println(token3);
// "1", "2", "3" get printed
You can even scan Strings, in case you may need it!
String love = "love,2,code";
Scanner scanner = new Scanner(love);
/* notice how we passed a String as a parameter instead of System.in or
a File object */
scanner.useDelimiter(",");
String token1 = scanner.next();
String token2 = scanner.next();
String token3 = scanner.next();
System.out.println(token1);
System.out.println(token2);
System.out.println(token3);
// "love", "2", "code" get printed
I hope that cleared things out a little bit!