Java class 5

35
Input And Output
  • date post

    18-Oct-2014
  • Category

    Education

  • view

    539
  • download

    1

description

 

Transcript of Java class 5

Page 1: Java class 5

Input And Output

Page 2: Java class 5

Input and Output

• Input is the data what we give to the program.• Output is the data what we receive from the

program in the form of result.• Stream represents flow of data i.e. sequence of

data.• To give input we use InputStream and to

receive output we use OutputStream.

Page 3: Java class 5

How input is read from Keyboard?

System.in InputStreamReader BufferedReader

It represents keyboard. To read data from keyboard it should be connected to InputStreamReader

It reads data from keyboard and send that data to BufferedReader.

It reads data from InputStreamReader and stores data in buffer. It has got methods so that data can be easily accessed.

connected to send data to

Page 4: Java class 5

Reading Input from console

• Input can be given either from file or keyword.

• Input can be read from console in 3 ways.BufferedReaderStringTokenizerScanner

Page 5: Java class 5

BufferedReader

BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));

int age = bufferedreader.read();String name = bufferedreader.readLine(); Methods

int read()String readLine()

Page 6: Java class 5

StringTokenizer

•It can be used to accept multiple inputs from console in a single line where as BufferedReader accepts only one input from a line.•It uses delimiter(space, comma) to make the input into tokens.

BufferedReader bufferedreader = new BufferedReader(newInputStreamReader(System.in));

String input = bufferedreader.readLine();

StringTokenizer tokenizer = new StringTokenizer(input, ”,”);String name = tokenizer.nextToken();int age=tokenizer.nextToken(); delimiter

Page 7: Java class 5

Scanner

• It accepts multiple inputs from file or keyboard and divides into tokens.

• It has methods to different types of input( int, float, string, long, double, byte) where tokenizer does not have.

Scanner scanner = new Scanner(System.in); int rollno = scanner.nextInt();` String name = scanner.next();

Page 8: Java class 5

Writing output to console

• The output can be written to console in 2 ways: print(String)-

System.out.print(“hello”); write(int)-

int input=‘i’;System.out.write(input);System.out.write(‘/n’);

Page 9: Java class 5

I/O StreamsI/O Streams

Byte Oriented Streams Unicode Character Oriented Streams

InputStream OutputStream Reader Writer

FileInputStreamDataInputStream

FileOutputStreamDataOutputStream

InputStreamReader

OutputStreamWriter

FileWriterFileReaderMay be buffered or unbuffered

Page 10: Java class 5

Array List

Page 11: Java class 5

ArrayList class

• The ArrayList class is a concrete implementation of the List interface.

• Allows duplicate elements.• A list can grow or shrink dynamically • On the other hand array is fixed once it is created.

– If your application does not require insertion or deletion of elements, the most efficient data structure is the array

Page 12: Java class 5

ArrayList class

Java.util.ArrayList size: 5

0 1 2 3 4 … …

Ravi Rajiv Megha Sunny Atif

elementData

Page 13: Java class 5

Java Programming: OOP 13

Methods in ArrayList

• boolean add(Object e)• void add(int index, Object

element)• boolean addAll(Collection c)

• Object get(int index)• Object set(int index,Object

element)

• Object remove(int index)

• Iterator iterator()• ListIterator listIterator()

• int indexOf()• int lastIndexOf()

• int index(Object element)• int size()• void clear()

Page 14: Java class 5

ArrayList - Insertion

// Create an arraylistArrayList arraylist = new ArrayList();

// Adding elementsarraylist.add("Rose");arraylist.add("Lilly");arraylist.add("Jasmine");arraylist.add("Rose");

//removes element at index 2arraylist.remove(2);

Page 15: Java class 5

Java Programming: OOP 15

How to trace the elements of ArrayList?

• For-each loop• Iterator• ListIterator• Enumeration

Page 16: Java class 5

Java Programming: OOP 16

For-each loop

• It’s action similar to for loop. It traces through all the elements of array or arraylist.

• No need to mention size of Arraylist.• for ( String s : arraylist_name)

Keyword type of data name of arraylist stored in arraylist

Page 17: Java class 5

Java Programming: OOP 17

Iterator

• Iterator is an interface that is used to traverse through the elements of collection.

• It traverses only in forward direction with the help of methods.

• boolean hasNext()• element next()• void remove ()

Iterator Methods

Page 18: Java class 5

Displaying Items using Iterator

Iterator iterator = arraylist.iterator();

while (iterator.hasNext()) {Object object = iterator.next();System.out.print(object + " ");

}

Java Programming: OOP

Page 19: Java class 5

Java Programming: OOP 19

ListIterator

• ListIterator is an interface that traverses through the elements of the collection.

• It traverses in both forward and reverse direction.

• boolean hasNext()• element next()• void remove ()• boolean

hasPrevious()• element previous()

ListIterator Methods

Page 20: Java class 5

Displaying Items using ListIterator

// To modify objects we use ListIteratorListIterator listiterator = arraylist.listIterator();

while (listiterator.hasNext()) {Object object = listiterator.next();listiterator.set("(" + object + ")");

}

Java Programming: OOP

Page 21: Java class 5

Java Programming: OOP 21

Enumeration

• Enumeration is an interface whose action is similar to iterator.

• But the difference is that it have no method for deleting an element of arraylist.

• boolean hasMoreElement()

• element nextElement()

Enumeration Methods

Page 22: Java class 5

Displaying Items using Enumeration

Enumeration enumeration = Collections.enumeration(arraylist);

while (enumeration.hasMoreElements()) {Object object =

enumeration.nextElement();System.out.print(object + " ");

}Java Programming: OOP

Page 23: Java class 5

HashMaps

Page 24: Java class 5

HashMap Class• The HashMap is a class which is used to perform operations such as

inserting, deleting, and locating elements in a Map . • The Map is an interface maps keys to the elements.• Maps are unsorted and unordered.• Map allows one null key and multiple null values• HashMap < K, V >

key value associated with key• key act as indexes and can be any objects.

Page 25: Java class 5

Java Programming: OOP 25

Methods in HashMap

• Object put(Object key, Object value)

• Enumeration keys()• Enumeration elements()• Object get(Object keys)

• boolean containsKey(Object key)• boolean containsValue(Object key)

• Object remove(Object key)• int size()• String toString()

Page 26: Java class 5

HashMap

01234...…

100

Ravi

Megha

Atif

Rajiv

Sunny

…..

………..

……….…….

Key Value

HashMap Class

Page 27: Java class 5

HashMap - Insertion

// Create a hash mapHashMap hashmap = new HashMap();

// Putting elementshashmap.put("Ankita", 9634.58);hashmap.put("Vishal", 1283.48);hashmap.put("Gurinder", 1478.10);hashmap.put("Krishna", 199.11);

Page 28: Java class 5

HashMap - Display

// Get an iteratorIterator iterator = hashmap.entrySet().iterator();

// Display elementswhile (iterator.hasNext()) {

Map.Entry entry = (Map.Entry) iterator.next();System.out.print(entry.getKey() + ": ");System.out.println(entry.getValue());

}

Page 29: Java class 5

Hashtable

Page 30: Java class 5

Hashtable Class

• Hashtable is a class which is used to perform operations such as inserting, deleting, and locating elements similar to HashMap .

• Similar to HashMap it also have key and value.• It does not allow null keys and null values.• The only difference between them is Hashtable

is synchronized where as HashMap is not by default.

Page 31: Java class 5

Java Programming: OOP 31

Methods in Hashtable

• Object put(Object key, Object value)

• Enumeration keys()• Enumeration elements()• Object get(Object keys)

• boolean containsKey(Object key)• boolean containsValue(Object key)

• Object remove(Object key)• int size()• String toString()

Page 32: Java class 5

Hashtable - Insertion

// Create a hash mapHashtable hashtable = new Hashtable();

// Putting elementshashtable.put("Ankita", 9634.58);hashtable.put("Vishal", 1283.48);hashtable.put("Gurinder", 1478.10);hashtable.put("Krishna", 199.11);

Page 33: Java class 5

Hashtable - Display

// Using EnumerationEnumeration enumeration = hashtable.keys();

// Display elementswhile (enumeration.hasMoreElements()) {

String key = enumeration.nextElement().toString();

String value = hashtable.get(key).toString();

System.out.println(key + ":"+value);}

Page 34: Java class 5

•Q& A..?

Page 35: Java class 5

Thanks..!