Tokenizing Strings - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Tokenizing Strings

Description:

... the access attributes in java: No access attribute specified. Public ... x is accessible from methods in any class in the same package. Access Attribute. public ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 37
Provided by: bas44
Category:

less

Transcript and Presenter's Notes

Title: Tokenizing Strings


1
Tokenizing Strings
2
Tokenizing Strings
  • Tokens are substrings separated by a delimiter
    character (string)
  • StringTokenizer is used to tokenize strings
  • It belongs to package java.util

3
Tokenization Example
  • String s "String to be broken"
  • StringTokenizer st
  • new StringTokenizer(s)
  • String token
  • while ( st.hasMoreElements() )
  • token st.nextToken()
  • System.out.println(token)

4
Tokenization Example Output
  • String
  • to
  • be
  • broken

5
Another Tokenization Example
  • String s String,Separated,By,Commas"
  • StringTokenizer st
  • new StringTokenizer(s,,)
  • String token
  • while ( st.hasMoreElements() )
  • token st.nextToken()
  • System.out.println(token)

6
Tokenization Example Output
  • String
  • separated
  • by
  • commas

7
Using JOptionPane for input and output
8
JOptionPane
  • Resides in javax.swing package
  • Used for showing different kind of dialog boxes

9
Displaying Messageboxes
  • Following line of code displays a simple
    messagebox
  • JOptionPane.showMessageDialog( null,
  • Message Title,
  • Message text,
  • JOptionPane.PLAIN_MESSAGE)

10
JOptionPane.showMessageDialog( null, Message
Title, Message text, JOptionPane.PLAIN_ME
SSAGE)
  • Argument 1 Parent Component
  • Argument 2
  • Argument 3

11
Argument 4
  • JOptionPane.PLAIN_MESSAGE

12
Argument 4
  • JOptionPane.QUESTION_MESSAGE

13
Argument 4
  • JOptionPane.WARNING_MESSAGE

14
Argument 4
  • JOptionPane.INFORMATION_MESSAGE

15
Argument 4
  • JOptionPane.ERROR_MESSAGE

16
Taking Input
  • String s
  • s JOptionPane.showInputDialog( Enter a
    number)

17
  • Always returns a string
  • Blocks for input
  • Convert the String s to an integer by
  • int i Integer.parseInt( s )

18
(No Transcript)
19
Lets get Object Oriented
  • Introduction to classes
  • class Name
  • Name() // Constructor

20
Lets get Object Oriented
  • Every class has a method named exactly same as
    the class, called Constructor
  • If you dont define one, a default constructor is
    automatically created, for example for class
    named Name, default constructor looks like
  • Name()

21
Lets get Object Oriented
  • For every class, you can have multiple
    constructors with different arguments (but
    obviously the same name)
  • class Name
  • Name()
  • Name(String s)

22
Class member functions
  • class Name
  • Name() // Constructor
  • public void memberFunc(int x)
  • // Body here

23
Copy Constructor
  • class Name
  • int a, b, c
  • Name(final Name n)
  • a n.a
  • b n.b
  • c n.c

24
Using Copy Constructor
  • class Test
  • public static void main(String a)
  • Name n new Name()
  • Name m new Name(n)

25
Member functions
  • All member functions along with their bodies must
    be typed inside the class code.
  • There is no concept of inline functions in java
  • There is no concept of prototypes either

26
Method Overloading
  • Member function overloading in Java is similar to
    that of C
  • Declare two functions with same name but
    different arguments to make them overloaded

27
Method Overloading Example
  • class Name
  • public myMethod()
  • public myMethod(String s)

28
Recursion
  • Calling a function from within itself is called
    recursion
  • Recursion is allowed in Java as it is in C

29
Recursion Example
  • class Recursive
  • public int function(int i)
  • if(i1 i0)
  • return 1
  • else
  • return i function(i-1)

30
Creating Packages
  • package something
  • class ABC
  • Associates ABC in package something
  • Put ABC.java file in a folder named something
  • Compile normally

31
Creating sub-packages
  • package something.anything
  • class XYZ
  • Associates XYZ in package something.anything
  • Put XYZ.java file in a folder named anything
    placed inside another folder something
  • Compile normally

32
Access Attributes in java
  • Ever data member and every member function in a
    class must be assign an access attribute in java.
  • Following are the access attributes in java
  • No access attribute specified
  • Public
  • Private
  • Protected

33
Access Attribute
  • No access specified
  • class Test
  • int x
  • x is accessible from methods in any class in the
    same package

34
Access Attribute
  • public
  • class Test
  • public int x
  • x is accessible from methods in any class anywhere

35
Access Attribute
  • private
  • class Test
  • private int x
  • x is accessible only from methods inside the
    class. No access from outside

36
Access Attribute
  • protected
  • class Test
  • protected int x
  • x is accessible from methods in any class in the
    same package and from any sub-classes anywhere
Write a Comment
User Comments (0)
About PowerShow.com