2 Basic I_O, Operators 16-07-2024
2 Basic I_O, Operators 16-07-2024
2 Basic I_O, Operators 16-07-2024
Others methods:
1. System.out.println()
2. System.out.print()
3. System.out.printf()
Let us see an example
class AssignmentOperator
{
public static void main(String[] args)
{
System.out.println("Java programming.");
}
}
Difference between print(), println() and printf()
•println() - prints string inside the quotes similar like print() method. Then
the cursor moves to the beginning of the next line.
class PrintVariables
{
public static void main(String[] args)
{
Double number = -10.6;
System.out.println("I am " + "awesome.");
System.out.println("Number = " + number);
}
}
Consider this code snippet
int a = 3;
int b = 4;
System.out.println( a + b );
System.out.println( "3" + "4" );
System.out.println( "" + a + b );
System.out.println( 3 + 4 + a + " " + b + a );
System.out.println( "Result: " + a + b );
System.out.println( "Result: " + ( a + b ) );
Printing characters
You can use + operator to concatenate strings and print it.
char a=65;
char b='A';
System.out.println(a);
System.out.println(b);
READING INPUT
READING INPUT FROM CONSOLE
In Java, there are three different ways for reading input from
the user in the command line environment(console).
• The main purpose of the Scanner class is to parse primitive types and strings
using regular expressions, however it is also can be used to read input from
the user in the command line.
SCANNER CLASS
import java.util.Scanner; int a = in.nextInt();
System.out.println("You entered
class GetInputFromUser integer "+a);
{
public static void main(String args[]) float b = in.nextFloat();
{ System.out.println("You entered
// Using Scanner for Getting Input float "+b);
from User }
Scanner in = new }
Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered
string "+s);
CONSOLE CLASS
• It has been becoming a preferred way for reading user’s input from the
command line.
System.out.println(name);
}
}
COMMAND LINE ARGUMENTS
COMMAND LINE ARGUMENTS
The java command-line argument is an argument i.e. passed at
run time.
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}
Program 1 : Adding two integers using command line arguments
class A
Predict the output .
{
public static void main(String args[]) 9
{
System.out.println(args[0]+args[1]);
} 18
}
Program 1 : Adding two integers using command line arguments
class A{
public static void main(String args[]) Predict the output .
{
9
System.out.println(Integer.parseInt(args[0])
+Integer.parseInt(args[1]));
}
}
Program 2 : Concatenating two strings
class A
{ Predict the output .
public static void main(String args[])
Hai Hello
{
System.out.println(args[0]+args[1]);
}
}
Program 3 : Find average of your marks (5 subjects)
class A
{
public static void main(String args[]) Predict the output .
{ Input : java A 67 98 91 78 98
float avg;
avg = (args[0]+args[1]+args[2]+args[3]+args[4])/5; 86.4
System.out.println(avg);
}
}
Program 3 : Find average of your marks (5 subjects)
class A
{
public static void main(String args[]) Predict the output .
{ Input : java A 67 98 91 78 98
float avg;
avg= 86.4
(Float.valueOf(args[0])+Float.valueOf(args[1])
+Float.valueOf(args[2])+Float.valueOf(args[3])
+Float.valueOf(args[4]))/5;
System.out.println(avg);
}
}
THANK YOU