You are on page 1of 47

Unit 2

How to get input from user in java


•One really useful class that handles input from a user is
called the Scanner class. The Scanner class can be found
in the java.util library. To use the Scanner class, you need
to reference it in your code

import java.util.Scanner;

•The next thing you need to do is to create an object from


the Scanner class. (A class is just a bunch of code. It
doesn't do anything until you create a new object from it.)
•To create a new Scanner object the code is this:

•Scanner user_input = new Scanner( System.in);


• To get the user input, you can call into action one of
the many methods available to your new Scanner
object. One of these methods is called next. This gets
the next string of text that a user types on the
keyboard:
String first_name;
first_name = user_input.next( );
• Following methods of Scanner class are used in the program below :-
1) nextInt to input an integer
2) nextFloat to input a float
3) nextLine to input a string
Java If-else Statement
• The Java if statement is used to test the condition. It checks boolean
condition: true or false. There are various types of if statement in java.
• if statement
• if-else statement
• nested if statement
• if-else-if ladder
Java IF Statement
• The Java if statement tests the condition. It executes
the if block if condition is true.
• The Java if statement tests the condition. It executes
the if block if condition is true.
Java IF-else Statement
• The Java if-else statement also tests the condition. It
executes the if block if condition is true otherwise else
block is executed.
nested if statement in java
Java IF-else-if ladder Statement
Java Switch Statement
While loop in Java
do while loop in java
• A do...while loop is similar to a while loop, except that
a do...while loop is guaranteed to execute at least one
time.
Java For Loop
• The Java for loop is used to iterate a part of the program several
times. If the number of iteration is fixed, it is recommended to use for
loop
Java Break Statement
• The Java break is used to break loop or switch statement. It breaks
the current flow of the program at specified condition. In case of
inner loop, it breaks only inner loop.
Java Continue Statement

• The continue keyword can be used in any of the loop control


structures. It causes the loop to immediately jump to the next
iteration of the loop.
Java Labeled For Loop

• We can have name of each for loop. To do so, we use label before the for loop. It
is useful if we have nested for loop so that we can break/continue specific for
loop.
• Normally, break and continue keywords breaks/continues the inner most for loop
only
Methods in Java

• A method is a collection of statements that perform some specific


task and return result to the caller.
• A method can perform some specific task without returning anything.
Methods allow us to reuse the code without retyping the code.
• In Java, every method must be part of some class which is different
from languages like C, C++ and Python
Method Declaration
• Modifier-: Defines access type of the method i.e. from where it can
be accessed in your application. In Java, there 4 type of the access
specifiers. public: accessible in all class in your application.
• protected: accessible within the class in which it is defined and in its
subclass(es)
• private: accessible only within the class in which it is defined.
• default (declared/defined without using any modifier) : accessible
within same class and package within which its class is define
Method Calling

• For using a method, it should be called. There are two ways in which a
method is called i.e., method returns a value or returning nothing (no
return value).
• The process of method calling is simple. When a program invokes a
method, the program control gets transferred to the called method.
This called method then returns control to the caller in two
conditions, when −
• the return statement is executed.
• it reaches the method ending closing brace

You might also like