Monday, August 13, 2012

BlueJ Program On String Class Using indexOf And substring Functions


Here is a BlueJ program where user will enter a string like Computer and any character like
if enters 'c', the output will be - computer
if enters 't' , the output will be - ter
if enters 'r' , the output will be – r


import java.io.*;
class St
{
String str;
char ch;
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take()throws Exception
{
System.out.println("Enter the sentence:");
str=br.readLine();
System.out.println("Enter the character from where the sentence is to be extracted:");
ch=(char)br.read();
i=str.indexOf(ch);
if(i< 0)
System.out.println("Character is not present...");
else
{
str=str.substring(i);
System.out.println(str);
}
}
 public static void main(String args[])throws Exception
{
St tr=new St();
tr.take();
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner