Session17 19
Session17 19
----------------
Slide 3):-
==============================
import java.util.*;
import java.io.*;
public class TestStack
{
public static void main(String[] args){
stack.push("one");
stack.push("two");
stack.push("three");
int size = stack.size()-1;
while (size >= 0 ) {
System.out.println(stack.pop());
size--;
Slide 7):-
====================================
import java.io.*;
import java.util.*;
class ComparableStudent
implements Comparable<ComparableStudent> {
}
public String getName(){ return this.name; }
else { return 0; }
/*
public int compareTo(ComparableStudent s){
else { return 0; }
} */
}
for(ComparableStudent student:studentList){
System.out.println(student);
}
}
}
Slide 13 ):-
=================================
{
System.out.println("args[" + i + "] is '" + args[i] + "'");
o/p:
-------------------------
Slide 15):-
Step 1):
============
CREATE A FILE ( ServerInfo.properties )
hostName=172.23.172.132
userName=scott
password=tiger
Step 2):-
==============
import java.io.*;
import java.util.*;
try
myProps.load(fis);
} catch (IOException e) {
// Print Values
Slide 16):'-
==================================
import java.io.*;
compile :-
d:\vijay\javac prt1.java
execute :-
d:\vijay\java -DuserName=hr -Dpassword=hr prt1
Slide 17):-
==================================
import java.io.PrintWriter;
// PrintWriter will flush the output buffer to the underlying device ( here
console).
Slide 19):-
================================
int quantity = 2;
String out = String.format("We have %03d %s Polo shirts that cost $%3.2f.",
quantity, color,
price);
System.out.println(out);
pw.printf("We have %03d %s Polo shirts that cost $%3.2f.\n", quantity, color,
price);
}
Slide 23 ):-
====================================
- Thread safe means the the methods are synchronized that is at a given point
of time only one thread can work on the object.
- Not Thread safe in this multiple threads can work on an object at a given
of time.
System.out.println("First Time"+sb);
//sb.append("Hello");
//sb.append("-Hai");
sb.append("Hello").append("-Hai");
Slide 24):-
====================================
String
============
String str="Gagan";
//length() Method
System.out.println("String Length is:"+str.length());
System.out.println("String Length is:"+"Suresh".length());
//toUpperCase() method
String uppercase;
uppercase=str.toUpperCase();
System.out.println("Upper Case :"+uppercase);
//substring() method
String substr;
substr=str.substring(1);
System.out.println("Substring :"+substr);
String substr_beg_end;
substr_beg_end=str.substring(2,9);
System.out.println("SubString :"+substr_beg_end);
//replace() method
//split() Method
String record="S1001,Vijay,Trainer,Cloud Campus";
String tokens[]=record.split(",");
for(int i=0;i<tokens.length;i++)
{
System.out.println(tokens[i]);
}
String str1=new String("Hello");
String str2=new String("Hello");
if(str1==str2)
System.out.println("Str1 and Str2 is equal");
else
System.out.println("Str1 and Str2 is not equal");
System.out.println("String1 HashCode:"+str1.hashCode());
System.out.println("String2 HashCode:"+str2.hashCode());
String str3="Sunita";
String str4="Sunita";
if(str3==str4)
System.out.println("Str3 and Str4 is equal");
else
System.out.println("Str3 and Str4 is not equal");
System.out.println("String3 HashCode:"+str3.hashCode());
System.out.println("String4 HashCode:"+str4.hashCode());
//equals() method
if(str1.equals(str2))
System.out.println("str1 and str2 are equal");
else
System.out.println("str1 and str2 are not equal");
Session 19)
----------------------------------------
Slide 2):-
===============================================
String shirts = "Blue Shirt, Red Shirt, Black Shirt, Maroon Shirt";
for(String shirtStr:results){
System.out.println(shirtStr);
}
}
}
Slide 3):-
==============================================
import java.util.StringTokenizer;
public class StringTokenizerDemo
{
public static void main(String arg[])
{
while(token.hasMoreTokens())
{
String str=token.nextToken();
System.out.println("Token ::"+str);
}
Slide 4):-
===============================================
eg1):-
-------------
import java.io.*;
import java.util.*;
int m,p,c,total;
String name;
System.out.println(" name = " + name +" m = "+ m + " p = "+ p + " c = "+ c );
eg 2):-
---------------------------
String Builder :-
import java.util.Scanner;
import java.io.*;
public class ScannerDemo {
public static void main(String[] args) {
Scanner s = null;
try {
while (s.hasNextFloat()) {
float f = s.nextFloat();
fsum += f;
sb.append(f).append(" ");
System.out.println("Valuesfound:"+sb.toString());
System.out.println(e.getMessage());
}
}
}
Slide 6):-
===========================
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternExample
{
public static void main(String[] args)
{
String t = "It was the best of times";
Pattern pattern = Pattern.compile("the");
Matcher matcher = pattern.matcher(t);
if ( matcher.find())
{
System.out.println("Found match!");
}
}
}
Slide 9):-
==============================
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Matcher m=pattern.matcher(t);
Slide 12):-
==============================
Example1.java
================
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example1
{
public static void main(String arg[])
{
while(m.find())
{
System.out.println(m.group());
Slide 15):-
======================================
Quantifier Example
=======================
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Matcher m=pattern.matcher(T1);
while(m.find())
{
System.out.println(m.group());
Matcher m1=pattern1.matcher(T2);
if(m1.find())
{
System.out.println(m1.group(0));
}
}
}
Slide 17):-
========================================
// Greediness Example
// =====================
import java.util.regex.*;
public class Example3
{
Matcher m=pattern.matcher(T1);
if(m.find())
{
System.out.println(m.group());
}
}
Slide 19):-
===================================
//Boundary Matches
//====================
import java.util.regex.*;
public class Example4
{
public static void main(String arg[])
{
String T1 = "it was the best of times or it was the worst of times";
Matcher m=pattern.matcher(T1);
if(m.find())
{
System.out.println(m.group());
}
}
Slide 23):-
=================================
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Pattern p1 = Pattern.compile("h1");
Matcher m1 = p1.matcher(header);
if (m1.find())
{
header = m1.replaceAll("p");