0% found this document useful (0 votes)
16 views11 pages

Electromagnetic

The document discusses input and output in Java programs. It explains how to use println(), print(), and printf() to output text and accept user input using the Scanner class. Examples are provided to demonstrate how to take various types of input and display output to the console.

Uploaded by

mamoabera012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Electromagnetic

The document discusses input and output in Java programs. It explains how to use println(), print(), and printf() to output text and accept user input using the Scanner class. Examples are provided to demonstrate how to take various types of input and display output to the console.

Uploaded by

mamoabera012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

ECEg3142

Object Oriented Programming (OOP)

Unit 1: Introduction to the OOP


Paradigm

Ambo University Institute of Technology


Department of ECE
Input/output

In this, you will learn simple ways to display


output to users and take input from users in
Java.
 Java Output
In Java, you can simply use: System.out.println(); or
System.out.print(); or
System.out.printf();
 to send output to standard output (screen).
 Here, System is a class
out is a public, static field: it accepts output data.
Difference between println(), print() and printf()

 print() - It prints string inside the quotes.


 println() - It prints string inside the quotes similar
like print() method. Then the cursor moves to the beginning
of the next line.
 printf() - It provides string formatting (similar to printf in
C/C++ programming).
Example

class AssignmentOperator {
public static void main(String[] args) {

System.out.println("Java programming is interesting.");


}
}
 Output: Java programming is interesting.
 Here, we have used the println() method to display
the string.
example
class Output {
public static void main(String[] args) {
System.out.println("1. println ");
System.out.println("2. println ");
System.out.print("1. print ");
System.out.print("2. print");
}
}
 Output:
1.println
2. println
1. print 2. print
Java Input
 Java provides different ways to get input from
the user. However, in this, you will learn to get
input from user using the object
of Scanner class.
 In order to use the object of Scanner, we
need to import java.util.Scanner package.
 Then, we need to create an object of
the Scanner class. We can use the object to
take input from the user.
example
Cont….
 In the above example, notice the line
 Scanner input = new
Scanner(System.in);Here, we have created an
object of Scanner named input.
 The System.in parameter is used to take
input from the standard input. It works just
like taking inputs from the keyboard.
 We have then used the nextLine() method of
the Scanner class to read a line of text from
the user.
Import Scanner Class
 As we can see from the above example,
we need to import
the java.util.Scanner package before we
can use the Scanner class.
 import java.util.Scanner;
 Create a Scanner Object in Java
 Once we import the package, here is how
we can create Scanner objects.
Examples
// read input from the input stream
Scanner sc1 = new Scanner(InputStream input);
// read input from files
Scanner sc2 = new Scanner(File file);
// read input from a string
Scanner sc3 = new Scanner(String str);
Here, we have created objects of
the Scanner class that will read input
from InputStream, File, and Stringrespectively.
Java Scanner Methods to Take Input
 The Scanner class provides various methods that
allow us to read inputs of different types.
Method Description
reads an int value from
nextInt()
the user
reads a float value form
nextFloat()
the user
reads a boolean value
nextBoolean()
from the user
reads a line of text from
nextLine()
the user
reads a word from the
next()
user
reads a byte value from
nextByte()
the user
reads a double value from
nextDouble()
the user

You might also like