Print Customer Details: Import Import Public Class Public Static Void New
Print Customer Details: Import Import Public Class Public Static Void New
import java.util.*;
2 import java.io.*;
3 public class Customer{
4 public static void main(String args[]){
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter your name:");
7 String name=sc.nextLine();
8 System.out.println("Enter age:");
9 int age=sc.nextInt();
10 System.out.println("Enter gender:");
11 String gender=sc.next();
12 sc.nextLine();
13 System.out.println("Hailing from:");
14 String from=sc.nextLine();
15 System.out.println("Welcome," +name+"!");
16 System.out.println("Age:"+age);
17 System.out.println("Gender:"+gender);
18 System.out.println("City:"+from);
19 }
20 }
Road Signaling
import java.util.*;
2
3 public class Main{
4
5 public static void main(String[] args){
6
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter the color");
9 String color=sc.nextLine();
10 if(color.equals("green")){
11 System.out.println("go");
12 }
13 else if(color.equals("red")){
14 System.out.println("stop");
15 }
16 else if(color.equals("yellow")){
17 System.out.println("proceed with caution");
18 }
19 else{
20 System.out.println("prepare to go");
21 }
22 }
23 }
Registration Details
import java.util.Scanner;
2
3 public class RegistrationDetails{
4
5 public static void main(String[] args){
6
7 Scanner sc=new Scanner(System.in);
8
9 System.out.println("Enter your name:");
10 String name=sc.nextLine();
11 System.out.println("Enter your age:");
12 int age=sc.nextInt();
13 System.out.println("Enter your phoneno:");
14 String pno=sc.nextLine();
15 System.out.println("Enter your qualification:");
16 String qualification=sc.nextLine();
17 System.out.println("Enter your email id[Please provide valid id, after registering your registration id will be
mailed]:");
18 String email=sc.nextLine();
19 System.out.println("Enter your noofexperience[if any]:");
20 String noe=sc.nextLine();
21 System.out.println("Dear "+name+", Thanks for registering in our portal, registration id will be mailed to
"+noe+" within 2 working days ");
22 }
23
24 }
25
Display Characters
import java.util.Scanner;
2
3 public class AsciValue{
4
5 public static void main(String args[]){
6
7 Scanner in=new Scanner(System.in);
8
9 System.out.println("Enter the digits: ");
10
11 int a=in.nextInt();
12
13 int b=in.nextInt();
14
15 int c=in.nextInt();
16
17 int d=in.nextInt();
18
19
20
21 char q=(char)a;
22
23 char w=(char)b;
24
25 char e=(char)c;
26
27 char r=(char)d;
28
29 System.out.println();
30
31 System.out.print(a);
32
33 System.out.println("-"+q);
34
35 System.out.print(b);
36
37 System.out.println("-"+w);
38
39 System.out.print(c);
40
41 System.out.println("-"+e);
42
43 System.out.print(d);
44
45 System.out.println("-"+r);
46
47 }
48
49 }
Bill Generation
1 import java.util.Scanner;
2 public class SnacksDetails
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter the no of pizzas bought:");
7 int piz=sc.nextInt();
8 System.out.println("Enter the no of puffs bought:");
9 int puf=sc.nextInt();
10 System.out.println("Enter the no of cool drinks bought:");
11 int cdk=sc.nextInt();
12
13 System.out.println("Bill Details");
14 System.out.println("No of pizzas:"+piz);
15 System.out.println("No of puffs:"+puf);
16 System.out.println("No of cooldrinks:"+cdk);
17 System.out.println("Total price="+(piz*100+puf*20+cdk*10));
18 System.out.println("ENJOY THE SHOW!!!");
19 }
20 }
Find Season
import java.util.Scanner;
2 public class Season
3 {
4 public static void main (String[] args) {
5 Scanner sc = new Scanner(System.in);
6 System.out.println("Enter the month:");
7 int mon=sc.nextInt();
8 if(mon>12||mon<1)
9 {
10 System.out.println("Invalid month");
11 }
12 else if(mon>=3&&mon<=5)
13 {
14 System.out.println("Season:Spring");
15 }
16 else if(mon>=6&&mon<=8)
17 {
18 System.out.println("Season:Summer");
19 }
20 else if(mon>=9&&mon<=11)
21 {
22 System.out.println("Season:Autumn");
23 }
24 else if(mon==12||mon==1||mon==2)
25 {
26 System.out.println("Season:Winter");
27 }
28 }
29 }
Highest Placement
import java.util.Scanner;
2 public class Placement
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter the no of students placed in CSE:");
7 int CSE= sc.nextInt();
8 System.out.println("Enter the no of students placed in ECE:");
9 int ECE=sc.nextInt();
10 System.out.println("Enter the no of students placed in MECH:");
11 int MECH=sc.nextInt();
12
13 if(CSE<0||ECE<0||MECH<0)
14 {
15 System.out.println("Input is Invalid");
16 }
17
18 else if(CSE==ECE && ECE==MECH)
19 {
20 System.out.println("None of the department has got the highest placement");
21 }
22
23 else if(CSE==ECE && MECH<CSE)
24 {
25 System.out.println("Highest placement");
26 System.out.println("CSE");
27 System.out.println("ECE");
28 }
29
30 else if(CSE==MECH && ECE<MECH)
31 {
32 System.out.println("Highest placement");
33 System.out.println("CSE");
34 System.out.println("MECH");
35 }
36
37 else if(ECE==MECH && CSE<MECH)
38 {
39 System.out.println("Highest placement");
40 System.out.println("ECE");
41 System.out.println("MECH");
42 }
43
44 else if(CSE>ECE&&CSE>MECH)
45 {
46 System.out.println("Highest placement");
47 System.out.println("CSE");
48 }
49
50 else if(ECE>CSE && ECE>MECH)
51 {
52 System.out.println("Highest placement");
53 System.out.println("ECE");
54 }
55
56 else if(MECH>CSE&&MECH>ECE)
57 {
58 System.out.println("Highest placement");
59 System.out.println("MECH");
60 }
61 }
62 }
IncrementCalculation
import java.util.*;
2
3 class IncrementCalculation{
4
5 public static void main (String[] args) {
6
7
8
9 Scanner sc = new Scanner (System.in);
10
11 System.out.println("Enter the salary");
12
13 int salary = sc.nextInt();
14
15 System.out.println("Enter the Performance appraisal rating");
16
17 float rating = sc.nextFloat();
18
19 if(salary<1||rating<1.0||rating>5.0){
20
21 System.out.println("Invalid Input");
22
23 System.exit(0);
24
25 }
26
27 else if(rating>=1&&rating<=3){
28
29 salary=salary+(int)(0.1*salary);
30
31 System.out.println(salary);
32
33 }
34
35 else if(rating>3&&rating<=4){
36
37 salary=salary+(int)(0.25*salary);
38
39 System.out.println(salary);
40
41 }
42
43 else /*if(rating>4&&rating<=5)*/{
44
45 salary=salary+(int)(0.3*salary);
46
47 System.out.println(salary);
48
49 }
50
51 }
52
53 }
Display String
import java.util.Scanner;
3
4 public class Main {
5
6 public static void main(String[] args){
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter the String");
9 String st=sc.nextLine();
10 System.out.println("Enter the number");
11 int nb=sc.nextInt();
12 if(nb<1){
13 System.out.println(nb+ "is not a valid input");
14 }
15 else{
16 for(int i=0;i<=nb;i++){
17 System.out.println(st);
18 }
19 }
20 }
21
22 }
import java.util.Scanner;
3
4 public class Main {
5
6 public static void main(String[] args){
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter the String");
9 String st=sc.nextLine();
10 System.out.println("Enter the number");
11 int nb=sc.nextInt();
12 if(nb<1){
13 System.out.println(nb+ "is not a valid input");
14 }
15 else{
16 for(int i=0;i<=nb;i++){
17 System.out.println(st);
18 }
19 }
20 }
21
22 }
Prime Factors
1 import java.util.*;
2 class Main {
3 public static void main(String[] args)
4 {
5 Scanner sc=new Scanner(System.in);
6 Scanner scan = new Scanner(System.in);
7 System.out.print("Input a number: ");
8 int n = scan.nextInt();
9 if (n>0)
10 {
11 while (n%2==0)
12 {
13 System.out.print(2 + " ");
14 n /= 2;
15 }
16
17 for (int i = 3; i <= Math.sqrt(n); i+= 2)
18 {
19 while (n%i == 0)
20 {
21 System.out.print(i + " ");
22 n /= i;
23 }
24 }
25 if (n > 2)
26 System.out.print(n);
27 }
28 }
29 }
30
Display Month/Day
import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 int n=0;
8 for(;n!=3;){
9 System.out.println("1.Display the Months");
10 System.out.println("2.Display the Days");
11 System.out.println("3.Exit");
12 n=sc.nextInt();
13 switch(n)
14 {
15 case 1:{
16 System.out.println("1.January");
17 System.out.println("2.February");
18 System.out.println("3.March");
19 System.out.println("4.April");
20 System.out.println("5.May");
21 System.out.println("6.June");
22 System.out.println("7.July");
23 System.out.println("8.August");
24 System.out.println("9.September");
25 System.out.println("10.October");
26 System.out.println("11.November");
27 System.out.println("12.December");
28 break;
29 }
30 case 2:{
31 System.out.println("1.Monday");
32 System.out.println("2.Tuesday");
33 System.out.println("3.Wednesday");
34 System.out.println("4.Thursday");
35 System.out.println("5.Friday");
36 System.out.println("6.Saturday");
37 System.out.println("7.Sunday");
38 break;
39 }
40 case 3: System.exit(0);
41
42 }
43
44 }
45 }
46 }
47
Power of a Number
import java.util.*;
2 public class Power
3 {
4 public static void main(String args[]){
5 Scanner scan = new Scanner (System.in);
6 long n, p;
7 System.out.println("Enter the number");
8 n = scan.nextLong();
9 System.out.println("Enter the digit");
10 p = scan.nextLong();
11 if(n < 0 || p < 0) {
12 System.out.println("Invalid input.");
13 return;
14 }
15 if(n == 0 && p == 0) {
16 System.out.println("Invalid input");
17 return;
18 }
19 if (n > 0 && p == 0) {
20 System.out.println("1");
21 return;
22 }
23 if(n == 0 && p > 0) {
24 System.out.println("0");
25 return;
26 }
27 else
28 System.out.println(power(n, p));
29 }
30 private static long power(long n, long p) {
31 long result = n;
32 for(int i=1; i<p; i++)
33 result *= n;
34 return result;
35 }
36 }
37
Factors of a Number
1 import java.util.*;
2 import java.lang.*;
3 public class FindFactor
4{
5 public static void main (String[] args) {
6 Scanner sc = new Scanner(System.in);
7 int num=Math.abs(sc.nextInt());
8 if(num==0)
9 {
10 System.out.println("No Factors");
11 }
12 else
13 {System.out.print("1");
14 for(int i=2;i<=num;i++)
15 {
16 if(num%i==0)
17 {
18 System.out.print(", "+i);
19 }
20 }
21 }
22 }
23 }
Lucky Number
import java.util.Scanner;
2 public class LuckyNum
3{
4 public static void main (String[] args) {
5 Scanner sc= new Scanner(System.in);
6 System.out.println("Enter the car no:");
7 int num=sc.nextInt();
8 if(num<1000 || num>9999)
9 {
10 System.out.println(num+" is not a valid car number");
11 }
12 else
13 {int sum=0;
14 while(num>0)
15 {
16 sum+=num%10;
17 num/=10;
18 }
19 if(sum%3==0 || sum%5==0 || sum%7==0)
20 {
21 System.out.println("Lucky Number");
22 }
23 else
24 {
25 System.out.println("Sorry its not my lucky number");
26 }
27 }
28 }
29 }
Number Palindrome
import java.util.Scanner;
2
3 public class Palindrome {
4
5 public static void main(String[] args) {
6
7 int lastDigit, sum = 0, a;
8 Scanner sc = new Scanner(System.in);
9 int num = sc.nextInt();
10 if (num < 0) {
11 System.out.println("Invalid Input");
12 } else {
13 a = num;
14
15
16 while (a > 0) {
17
18 lastDigit = a % 10;
19 sum = (sum * 10) + lastDigit;
20 a = a / 10;
21
22 }
23
24
25 if (sum == num)
26 System.out.println("Palindrome");
27 else
28 System.out.println("Not a Palindrome");
29
30 }
31
32 }
33 }
34
Book Detail
TestBook.java
import java.util.Scanner;
2 public class TestBook
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6
7 System.out.println("Enter the Book name:");
8 String bookname=sc.nextLine();
9
10 System.out.println("Enter the price:");
11 int price=sc.nextInt();
12 sc.nextLine();
13
14 System.out.println("Enter the Author name:");
15 String authorname=sc.nextLine();
16
17 Book obj=new Book();
18 obj.setBookName(bookname);
19 obj.setBookPrice(price);
20 obj.setAuthorName(authorname);
21 System.out.println("Book Details");
22 System.out.println("Book Name :"+obj.getBookName());
23 System.out.println("Book Price :"+obj.getBookPrice());
24 System.out.println("Author Name :"+obj.getAuthorName());
25 }
26 }
Book.java
1 public class Book
2{
3 private String bookName;
4 private int bookPrice;
5 private String authorName;
6
7 public void setBookName(String bookName)
8 {
9 this.bookName=bookName;
10 }
11
12 public String getBookName()
13 {
14 return this.bookName;
15 }
16
17 public void setBookPrice(int bookPrice)
18 {
19 this.bookPrice=bookPrice;
20 }
21
22 public int getBookPrice()
23 {
24 return this.bookPrice;
25 }
26
27 public void setAuthorName(String authorName)
28 {
29 this.authorName=authorName;
30 }
31
32 public String getAuthorName()
33 {
34 return this.authorName;
35 }
36 }
import java.util.Scanner;
2 public class Main
3{
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 Ticket obj=new Ticket();
7
8 System.out.println("Enter no of bookings:");
9 int no_bookings=sc.nextInt();
10
11 System.out.println("Enter the available tickets:");
12 obj.setAvailableTickets(sc.nextInt());
13
14 while(no_bookings>0)
15 {
16 System.out.println("Enter the ticketid:");
17 obj.setTicketid(sc.nextInt());
18
19 System.out.println("Enter the price:");
20 obj.setPrice(sc.nextInt());
21
22 System.out.println("Enter the no of tickets:");
23 int no_tickets=sc.nextInt();
24 if(obj.calculateTicketCost(no_tickets)==-1)
25 {
26 continue;
27 }
28
29 System.out.println("Available tickets: "+obj.getAvailableTickets());
30
31 System.out.println("Total amount:"+obj.calculateTicketCost(no_tickets));
32
33 System.out.println("Available ticket after booking:"+obj.getAvailableTickets());
34
35 no_bookings--;
36 }
37 }
38 }
Ticket.java
public class Ticket
2{
3 private int ticketid;
4 private int price;
5 private static int availableTickets;
6
7 public void setTicketid(int ticketid)
8 {
9 this.ticketid=ticketid;
10 }
11
12 public int getTicketid()
13 {
14 return this.ticketid;
15 }
16
17 public void setPrice(int price)
18 {
19 this.price=price;
20 }
21
22 public int getPrice()
23 {
24 return this.price;
25 }
26
27 public static void setAvailableTickets(int availableTickets1)
28 { if(availableTickets1>=0)
29 {
30 availableTickets=availableTickets1;
31 }
32 }
33
34 public static int getAvailableTickets()
35 {
36 return availableTickets;
37 }
38
39 public int calculateTicketCost(int nooftickets)
40 {
41 if(availableTickets>=nooftickets)
42 {
43 availableTickets-=nooftickets;
44 return nooftickets*this.price;
45 }
46 else
47 {
48 return -1;
49 }
50 }
51 }
Travel Details
TestMain.java
1
2 import java.util.*;
3 public class TestMain
4 {
5 public static BusTicket getTicketDetails()
6 {
7 BusTicket bt= new BusTicket();
8 Scanner sc= new Scanner(System.in);
9 System.out.println("Enter the ticket no:");
10 bt.setTicketNo(sc.nextInt());
11 System.out.println("Enter the ticket price:");
12 bt.setTicketPrice(sc.nextFloat());
13 return bt;
14 }
15 public static void main (String[] args) {
16 Scanner sc= new Scanner(System.in);
17 Person p= new Person();
18 System.out.println("Enter the passenger name:");
19 p.setName(sc.nextLine());
20 System.out.println("Enter the gender(M or F / m or f):");
21 p.setGender(sc.next().charAt(0));
22 System.out.println("Enter the age:");
23 p.setAge(sc.nextInt());
24 BusTicket bt= getTicketDetails();
25 bt.setPerson(p);
26 p= bt.getPerson();
27 System.out.println("Ticket no:"+bt.getTicketNo());
28 System.out.println("Passenger Name:"+p.getName());
29 System.out.println("Price of a ticket : "+bt.getTicketPrice());
30 bt.calculateTotal();
31 System.out.println("Total Amount : "+bt.getTotalAmount());
32 }
33 }
34
BusTicket.java
1
2 public class BusTicket
3 {
4 private int ticketNo;
5 private float ticketPrice;
6 private float totalAmount;
7 private Person person;
8 public void setPerson(Person person)
9 {
10 this.person=person;
11 }
12 public Person getPerson()
13 {
14 return person;
15 }
16 public void setTicketNo(int ticketNo)
17 {
18 this.ticketNo=ticketNo;
19 }
20 public void setTicketPrice(float ticketPrice)
21 {
22 this.ticketPrice=ticketPrice;
23 }
24 public void setTotalAmount(float totalAmount)
25 {
26 this.totalAmount= totalAmount;
27 }
28 public int getTicketNo()
29 {
30 return ticketNo;
31 }
32 public float getTicketPrice()
33 {
34 return ticketPrice;
35 }
36 public float getTotalAmount()
37 {
38 return totalAmount;
39 }
40 public void calculateTotal()
41 {
42 if(person.getAge()<=15)
43 {
44 setTotalAmount(ticketPrice/2);
45 }
46 else if(person.getAge()>=60)
47 {
48 setTotalAmount(ticketPrice*75/100);
49 }
50 else if(person.getGender()=='f'||person.getGender()=='F')
51 {
52 setTotalAmount(ticketPrice*90/100);
53 }
54 else
55 {
56 setTotalAmount(ticketPrice);
57 }
58 }
59 }
BankAccountDetails
import java.util.*;
2 public class AccountDetails
3{
4 public static Account getAccountDetails()
5 {
6 Account acc=new Account();
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter account id: ");
9 acc.setAccountId(sc.nextInt());
10 sc.nextLine();
11 System.out.println("Enter account type: ");
12 acc.setAccountType(sc.nextLine());
13 int b;
14 do
15 {
16 System.out.println("Enter Balance");
17 acc.setBalance(sc.nextInt());
18 b=acc.getBalance();
19 if(b<=0)
20 System.out.println("Balance should be positive");
21 }
22 while(b<=0);
23 return acc;
24 }
25 public static int getWithdrawAmount()
26 {
27 Scanner sc=new Scanner(System.in);
28 int w;
29 do
30 {
31 System.out.println("Enter amount to be withdrawn:");
32 w=sc.nextInt();
33 if(w<=0)
34 System.out.println("Amount should be positive");
35 }
36 while(w<=0);
37 return w;
38 }
39 public static void main(String[] args)
40 {
41 Account accObj=new Account();
42 accObj=getAccountDetails();
43 int c=getWithdrawAmount();
44 accObj.withdraw(c);
45 }
46 }
Account.java
1 public class Account
2{
3 private int accountId;
4 private String accountType;
5 private int balance;
6 public int getAccountId()
7 {
8 return accountId;
9
10 }
11 public String getAccountType()
12 {
13 return accountType;
14 }
15 public int getBalance()
16 {
17 return balance;
18
19 }
20 public void setAccountId(int id)
21 {
22 accountId=id;
23 }
24 public void setAccountType(String s)
25 {
26 accountType=s;
27 }
28 public void setBalance(int b)
29 {
30 balance=b;
31 }
32 public boolean withdraw(int w)
33 {
34 if(getBalance()<w)
35 {
36 System.out.println("Sorry!!! No enough balance");
37 return false;
38 }
39 else
40 {
41 System.out.println("Balance amount after withdraw: "+(getBalance()-w));
42 return true;
43 }
44 }
45 }
Department.java
1 public class Department {
2 private int did;
3 private String dname;
4 public void setDid(int did){
5 this.did=did;
6 }
7 public int getDid(){
8 return did;
9 }
10 public void setDname(String dname){
11 this.dname=dname;
12 }
13 public String getDname() {
14 return dname;
15 }
16 }
Display array values
import java.util.Scanner;
2
3 public class Main
4 {
5 public static void main(String args[]) {
6 Scanner in = new Scanner(System.in);
7 System.out.println("Enter the array size");
8 int n = in.nextInt();
9
10 int arr[] = new int[n];
11 System.out.println("Enter the values");
12 for (int i = 0; i < n; i++) {
13 arr[i] = in.nextInt();
14 }
15
16
17 for (int i = 0; i < n; i++) {
18 System.out.println( arr[i]);
19
20 }
21
22
23 }
24 }
Main.java
1 import java.io.*;
2 import java.util.*;
3 public class Main{
4 public static void main(String args[]){
5 Player[] pObj=new Player[4];
6 pObj[0]=new Player(146,"Athul",16,9876543210l);
7 pObj[1]=new Player(135,"Rakshana",16,9956231045l);
8 pObj[2]=new Player(168,"Christy",16,9856471230l);
9 pObj[3]=new Player(194,"Athulya",16,9768541230l);
10
11 Scanner sc=new Scanner(System.in);
12 System.out.println("Enter the id to be searched");
13 int x=sc.nextInt();
14 PlayerUtility obj=new PlayerUtility();
15 Player finded=obj.findPlayerDetailsById(pObj,x);
16 if(finded==null)
17 {
18 System.out.println("No player found");
19 }
20 else
21 { System.out.println("Name:"+finded.getPlayerName());
22 System.out.print("Phone number:"+finded.getPhoneNumber());
23 }
24
25 }
26 }
Array Compatiblilty
CompatibleArrays.java
1 import java.util.*;
2 public class CompatibleArrays
3 {
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.println("Enter the size for First array:");
7 int size1=sc.nextInt();
8 if(size1<=0)
9 System.out.println("Invalid array size");
10 else
11 {
12 int arr1[]=new int[size1];
13 System.out.println("Enter the elements for First array:");
14 for(int i=0;i<size1;i++)
15 arr1[i]=sc.nextInt();
16 System.out.println("Enter the size for Second array:");
17 int size2=sc.nextInt();
18 if(size2<=0)
19 System.out.println("Invalid array size");
20 else
21 {
22 int count=0;
23 int arr2[]=new int[size2];
24 System.out.println("Enter the elements for Second array:");
25 for(int i=0;i<size2;i++)
26 {
27 arr2[i]=sc.nextInt();
28 }
29 if(size1==size2)
30 {
31 for(int i=0;i<size1;i++)
32 {
33 if(arr1[i]>=arr2[i])
34 count++;
35 }
36 if(count==(size1))
37 System.out.println("Arrays are Compatible");
38 else
39 {
40 System.out.println("Arrays are Not Compatible");
41 }
42 }
43 else
44 System.out.println("Arrays are not Compatible");
45 }
46 }
47 }
48 }
String length
Main.java
1 import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 String str=sc.nextLine();
8 int len=str.length();
9 if(len%2 ==0){
10 System.out.println(len+" even");
11 }
12 else{
13 System.out.println(len+" odd");
14 }
15 }
16 }
17
Count of alphabets
import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 String str=sc.nextLine();
8 int count=0;
9 for(int i=0;i<str.length();i++){
10 if(Character.isLetter(str.charAt(i)))
11 {
12 count++;
13 }
14 System.out.println(count);
15 }
16 }
17
18 }
19
20
21
SubString
2 import java.util.Scanner;
3
4 public class Main {
5
6 public static void main(String[] args){
7 Scanner sc=new Scanner(System.in);
8 String str=sc.nextLine();
9 int start=sc.nextInt();
10 int end=sc.nextInt();
11 System.out.println(str.substring(start,end));
12 }
13
14 }
15
String Contains
Main.java
1 import java.util.Scanner;
2
3 public class Main {
4
5 public static void main(String[] args){
6 Scanner sc=new Scanner(System.in);
7 String str=sc.nextLine();
8 String sub_str=sc.nextLine();
9 if(str.indexOf(sub_str)!=-1){
10 System.out.println(sub_str+" is contained in a sentence");
11 }
12 else{
13 System.out.println(sub_str+" is not contained in a sentence");
14 }
15 }
16
17 }
18
String Concatenation
Authority.java
1 import java.util.*;
2 class Authority
3 {
4 public static void main (String[] args) {
5 Scanner sc=new Scanner(System.in);
6 System.out.print("Inmate's name:");
7 String name=sc.nextLine();
8 System.out.print("Inmate's father's name:");
9 String fname=sc.nextLine();
10 name=name.concat(" ").concat(fname);
11 char []chars=name.toCharArray();
12 for(char c:chars)
13 {
14 if(Character.isDigit(c)||c=='+'||c=='!'||c=='@'||c=='#'||c=='%'||c=='$'||c=='^'||c=='&'||c=='*'||c=='('
15 ||c==')'||c=='-'||c=='='||c=='/'||c=='<'||c=='>'||c=='?'||c=='{'||c=='}'||c=='['||c==']')
16 {
17 System.out.print("Invalid name");
18 System.exit(0);
19 }
20 }
21 System.out.println(name.toUpperCase());
22 }w
23 }
Length of String
import java.util.*;
2
3 class FindLength {
4
5 public static void main(String[] args) {
6
7 String testString;
8
9 Scanner in = new Scanner(System.in);
10
11 System.out.println("Enter String : ");
12
13 testString = in .nextLine();
14
15 for (int i = 0; i < testString.length(); i++) {
16
17 if ((testString.charAt(i) >= 'A' && testString.charAt(i) <= 'Z') || (testString.charAt(i) >= 'a' &&
testString.charAt(i) <= 'z') || testString.charAt(i) == ' ')
18
19 {
20
21 continue;
22
23 } else
24
25 {
26
27 System.out.println("Invalid String.");
28
29 System.exit(0);
30
31 }
32
33 }
34
35 System.out.println("No. of characters is : " + testString.length());
36
37 }
38
39 }
40
41
InitCap
InitCap.java
1 import java.util.*;
2 public class InitCap
3 {
4 static void convert(String str)
5 {
6 int count=0;
7 char []ch=str.toCharArray();
8 for(int i=0;i<str.length();i++)
9 {
10 if(i==0&&ch[i]!=' '||ch[i]!=' '&&ch[i-1]==' ')
11 {
12 if(ch[i]>='a'&& ch[i]<='z')
13 {
14 ch[i]=(char)(ch[i]-'a'+'A');
15 count++;
16 }
17 }
18 else if(ch[i]>='A'&&ch[i]<='Z')
19 ch[i]=(char)(ch[i]+'a'-'A');
20 }
21 if(count==0)
22 System.out.println("First character of each word is already in uppercase");
23 else
24 {
25 String st=new String(ch);
26 System.out.println(st);
27 }
28 }
29 public static void main (String[] args) {
30 Scanner sc=new Scanner(System.in);
31 System.out.println("Enter the String:");
32 String str=sc.nextLine();
33 convert(str);
34 }
35 }
Palindrome
Palindrome.java
1 import java.util.*;
2 import java.util.regex.Matcher;
3 import java.util.regex.Pattern;
4 public class Palindrome
5{
6 public static void main (String[] args) {
7 Scanner sc= new Scanner(System.in);
8
9 System.out.println("Enter the word :");
10 String str=sc.nextLine();
11 int len=str.length();
12
13 Pattern p=Pattern.compile("[^a-z0-9]", Pattern.CASE_INSENSITIVE);
14 Matcher m=p.matcher(str);
15 boolean b=m.find();
16
17 if(b)
18 {
19 System.out.println("Invalid Input");
20 }
21 else
22 {
23 String rvs=new String();
24
25 for(int i=(len-1); i>=0; i--)
26 {
27 rvs=rvs+str.charAt(i);
28 }
29
30 if(str.equalsIgnoreCase(rvs))
31 {
32 System.out.println(str+" is a Palindrome");
33 }
34 else
35 {
36 System.out.println(str+" is not a Palindrome");
37 }
38 }
39 }
40 }
Find and Replace
ReplaceWord.java
1 import java.util.*;
2 public class ReplaceWord{
3 public static void main (String[] args) {
4 Scanner in=new Scanner(System.in);
5 System.out.println("Enter the String:");
6 String a=in.nextLine();
7 Character dot=a.charAt(a.length()-1);
8 System.out.println("Enter the word to be searched:");
9 String b=in.nextLine();
10 System.out.println("Enter the word to be replaced:");
11 String n=in.nextLine();
12 String res="";
13 int count=0;
14 String[] words=a.split("\\W");
15
16 for(String word:words){
17 if(word.equals(b)){
18 res=res+n+" ";
19 count=count+1;
20 }
21 else{
22 res=res+word+" ";
23 }
24 }
25 if(count>=1){
26 System.out.print(res.trim());
27 if(dot=='.')
28 System.out.print(".");
29 }
30 else{
31 System.out.print("The word "+b+" not found");
32 }
33 }
34 }