Alright, let's elaborate with some simplified explanation about the Scanner class.
It is a standard Oracle class which you can use by calling the import java.util.Scanner.
So let's make a basic example of the class:
class Scanner {
InputStream source;
Scanner(InputStream src) {
this.source = src;
}
int nextInt() {
int nextInteger;
//Scans the next token of the input as an int from the source.
return nextInteger;
}
}
Now when you call Scanner input = new Scanner(System.in); you make a new object of the Scanner class (so you make a new "Scanner") and you store it in the variable input. At the same time you are calling the (so called) constructor of the class, with the parameter System.in. That means it is going to read from the standard input stream of the program.
Now when you are calling input.nextInt(); you execute the method from the object you just created (also documented). But as we see, this method returns a integer, so if we want to use that integer, we have to assign the call to a variable like you do:
int i = input.nextInt();
Answer from moffeltje on Stack OverflowVideos
Alright, let's elaborate with some simplified explanation about the Scanner class.
It is a standard Oracle class which you can use by calling the import java.util.Scanner.
So let's make a basic example of the class:
class Scanner {
InputStream source;
Scanner(InputStream src) {
this.source = src;
}
int nextInt() {
int nextInteger;
//Scans the next token of the input as an int from the source.
return nextInteger;
}
}
Now when you call Scanner input = new Scanner(System.in); you make a new object of the Scanner class (so you make a new "Scanner") and you store it in the variable input. At the same time you are calling the (so called) constructor of the class, with the parameter System.in. That means it is going to read from the standard input stream of the program.
Now when you are calling input.nextInt(); you execute the method from the object you just created (also documented). But as we see, this method returns a integer, so if we want to use that integer, we have to assign the call to a variable like you do:
int i = input.nextInt();
Scanner input = new Scanner(System.in); creates a new Scanner instance which points to the input stream passed as argument. In your case the steam is Standard input stream.
So, once your scanner instance is pointing to it, you can scan the stream and get integers, strings and do other stuff .
I have to learn how to code in JAVA for school, I have been trying for many hours now but I fail to understand how to to use 'Scanner'
Could anybody help me out with this code to at least teach me how I can use it right the next time? Because the answers to my mistakes is nowhere to be found even after watching countless of Youtube videos.
I have to change the code so that the number will get printed.
Sample input1: 12 > The sample output for this has to be also: 12
Sample input 2: 42 > The sample output for this also has to be: 42
Sample imput 3: -12 > The sample output for this also has to be: -12
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = New Scanner(System.in);
int getal = scanner.nextInt();
System.out.println(........)
And then I honestly have no clue what to do..
Please help and thank you in advance