0% found this document useful (0 votes)
25 views6 pages

Program 9,10,11

The document contains Java programs organized into packages that demonstrate string manipulation and exception handling. It includes classes for concatenating and extracting strings, validating user input based on age, and various string methods such as converting to lowercase, uppercase, extracting substrings, and replacing characters. Each program is structured with user input and output functionality, showcasing basic Java programming concepts.

Uploaded by

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

Program 9,10,11

The document contains Java programs organized into packages that demonstrate string manipulation and exception handling. It includes classes for concatenating and extracting strings, validating user input based on age, and various string methods such as converting to lowercase, uppercase, extracting substrings, and replacing characters. Each program is structured with user input and output functionality, showcasing basic Java programming concepts.

Uploaded by

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

Program 9

Create a package called mypack

Add three classes inside that 1. Concatenate 2. Extractstr 3.String1

1. Concatenate
package mypack;
import java.io.*;
public class Concatenate
{
public void concat() throws IOException
{
System.out.println("enter a string:");
String1 ob=new String1();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
ob.str1=in.readLine();
System.out.println("Enter another string:");
ob.str2=in.readLine();
System.out.println("\nConcatenated string is: "+ob.str1.concat(""+ob.str2));
}
}
2. Extractstr
package mypack;
import java.io.*;
public class Extractstr
{
public void Extract() throws IOException
{

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


String1 ob=new String1();
System.out.println("\nEnter a string:");
ob.str1=in.readLine();
System.out.println("Enter starting and ending position:");
ob.start=Integer.parseInt(in.readLine());
ob.end=Integer.parseInt(in.readLine());
System.out.println("Extracted string is: "+ob.str1.substring(ob.start,ob.end));
}
}
3. String1
package mypack;
public class String1
{
public String str1,str2;
protected int start,end;
}

Create a class name DisplayString

package mypack1;
import java.io.*;
import mypack.Concatenate;
import mypack.Extractstr;
public class DisplayString
{
public static void main(String[] args)throws IOException
{
Concatenate ob=new Concatenate();
ob.concat();
Extractstr ob1=new Extractstr();
ob1.Extract();
}
}

Program 10
Exception Handling

package program10;

import java.io.*;
class SelectionException extends Exception
{
SelectionException()
{
super("you are not allowed");
}
}
class Details
{
String name;
int age;
Details(String sname,int age)
{
this.name=sname;
this.age=age;
}
public void Validate(int age)
{
try
{
if((age<18)||(age>55))
{
throw new SelectionException();
}
else
System.out.println("Registered Successfully");
}
catch(SelectionException e)
{
System.out.println(e);
}
}
public void display()
{
System.out.println("Name= "+name);
System.out.println("Age= "+age);
}
}
public class Validation
{
public static void main(String[] args)throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name:");
String name=in.readLine();
System.out.println("Enter age:");
int age=Integer.parseInt(in.readLine());
Details obj=new Details(name,age);
obj.Validate(age);
obj.display();
}
}

Program 11

string methods

package lab;
import java.io.*;

public class strmethods

public static void main(String[] args)throws IOException

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

String str,str1,str2;

int ch,pos,n;

char ch1,ch2;

String wish,w="y";

do

System.out.println("1.Lowercase");

System.out.println("2.Uppercase");

System.out.println("3.Extract");

System.out.println("4.Replace");

System.out.println("5.Exit");

System.out.println("Enter your choice:");

ch=Integer.parseInt(in.readLine());

System.out.flush();

switch(ch)

case 1:System.out.println("Enter a String:");

str=in.readLine();

System.out.println("Enter String= "+str+"\t"+"Converted String=

"+str.toLowerCase());

break;

case 2:System.out.println("Enter a String:");

str=in.readLine();
System.out.println("Enter String= "+str+"\t"+"Converted String=

"+str.toUpperCase());

break;

case 3:System.out.println("Enter a String:");

str=in.readLine();

System.out.println("Enter the position of the string to extract:");

pos=Integer.parseInt(in.readLine());

System.out.println("Enter the position of characters till it has to extract:");

n=Integer.parseInt(in.readLine());

if(pos<str.length() && n<str.length())

System.out.println("The Extracted substring is= "+str.substring(pos,n));

else

System.out.println("Not possible to extract:");

break;

case 4:System.out.println("Enter a String:");

str=in.readLine();

System.out.println("Enter the character to be replaced:");

ch1=(char)in.readLine().charAt(0);

System.out.println("Replace with what?");

ch2=(char)in.readLine().charAt(0);

System.out.flush();

str1=str.replace(ch1, ch2);

System.out.println("The original String= "+str+"\t"+"and replaced string= "+str1);

break;

case 5:System.out.println("Program Exiting...");

return;

default:System.out.println("Invalid");

System.out.println("Do you want to continue (y/n)");


wish=in.readLine();

while(wish.equalsIgnoreCase("y"));

You might also like