Reputation: 17
I need some help in understanding this code. what do the different parts say and so on especially the part with the try function?
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Upvotes: 0
Views: 102
Reputation: 1990
As indicated by Roddy, that block is a try-with-resources. When the program enters the try
block, it generates the following ressource:
Scanner scanner = new Scanner(System.in);
The only constraint is that the instantiated ressource must implement java.lang.AutoCloseable
, so that it can be closed after the try
block is finished.
Upvotes: 1
Reputation: 17
public static void main(String[] args) {
try(Scanner scanner = new Scanner(System.in))
{
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Scanner scanner = new Scanner(System.in)
- Create an instance of type Scanner, which takes an input stream as argument.
System.in
- returns the standard input stream. (Standard input is a stream from which a program reads its input data).
try() { }
- This is called a try-with-resources statement in java, that declares try with one or more resources, that will need to be closed after the program is terminated to safely release the system resources. The open and closed curly braces makes it a block.
System.out.println();
- This can print a character, a string, a boolean or an array of characters to the console.
In this case System.out.println(num + " is even");
prints out 2 is even if the value of num is 2.
For a list of what the println method can print depending on the type of argument passed to it, please refer this https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println
int num = scanner.nextInt();
- This will scan the number typed on the console as an int and assigns it to the variable num.
nextInt()
method used can be found here - https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
num % 2
- divides the value in the variable num by 2 and gives the reminder. If num is 3, then the reminder returned is 1.
num % 2
== 1 - checks if value returned by num % 2 is equal to 1, returns true if equal and false if not. If the condition satisfies then it enters into if block and prints to the console that the num is even. If the condition does not satisfy it prints to console that the num is odd.
Upvotes: 1
Reputation: 505
// The try-with-resources statement ensures that the System.in stream is closed at the end of the block
try (Scanner scanner = new Scanner(System.in)) {
// print a line to the console
System.out.print("Enter a number: ");
// wait till input is typed into the console and the user pressed enter
// and parse the input as an integer value
int num = scanner.nextInt();
// get the remainder of a division of the input number by 2.
// so 3 % 2 == 1, 2 % 2 == 0
if (num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
Upvotes: 2