Pages

Friday, August 31, 2012

how to print a word after a word in java

public class SearchString{
   public static void main(String[] args) {
      String strOrig = "how to print a word after a word in java.";
      String word=null;
      int intIndex = strOrig.indexOf("print");
      if(intIndex == - 1){
         System.out.println("the word not found");
      }else{
      String substring1 = strOrig.substring(intIndex );
      String substring2 = substring1.substring(substring1.indexOf(" "));
      substring2 = substring2.trim();
      word = substring2.substring(0, substring2.indexOf(" "));
        
         System.out.println(word);
      }
   }
}

Output
 it will print 'a' as it is the word after print...


Explanation of code
int intIndex = strOrig.indexOf("print");
this code segment used to  find the word is occurred or not

String substring1 = strOrig.substring(intIndex );
this code segment create a sub-string from the word 'print' to the end of the original string.

String substring2 = substring1.substring(substring1.indexOf(" "));
this will create sub-string from the next space till end (will remvoe the word 'print form the sub string)

substring2 = substring2.trim();
trim the string (remove the first and last spaces...)

word = substring2.substring(0, substring2.indexOf(" "));

taking our required sub string 

Sunday, November 20, 2011

Tips of java2:-Menu shortcut

Here is a way to add short for adding shortcut like image shown below.

You can add this by simply adding a code to your java program.
mnuItemQuit.setAccelerator(KeyStroke.getKeyStroke("control Q"));
mnuItemQuit is the id of the menu item "Quit"


tips Of java 1:Changing The Icon of Java frame

By default java frame have the icon in the picture below
. Here i am going to explain a way to change the icon of java frame.
Create jframe using JFrame f=new JFrame("sample");
Set static java.net.URL imgURL = Window.class.getResource("images/icon.jpg");
Call f.setIconImage(getFDImage());
function getFDImage()
protected static Image getFDImage() {
        if (imgURL != null) {
            return new ImageIcon(imgURL).getImage();
        } else {
            return null;
           
        }
 }
This will change the icon to icon.jpg put this file into images folder.
Changed Icon



Monday, November 14, 2011

Two Way Chat

In two way chat we have two programs one is a server and the other is a client
Here in server program we create a server socket with a port of 6789(can be changed) by usingser=new ServerSocket(6789); and socket is connected to it by using socket=ser.accept(); Also we create inputstream reader and outputstream reader in the server.java file

In client we create a socket and connect it to the port number 6789 by using  socket=new Socket(InetAddress.getLocalHost(),6789); Also we create inputstream reader and outputstream reader in the client.java file
 For running the Chat we have to run server using a one command prompt and client using another command prompt.

Sunday, November 13, 2011

Analogue Clock

 This is a program for creating Analogue Clock using applet. An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When you use a Java technology-enabled browser to view a page that contains an applet, the applet's code is transferred to your system and executed by the browser's Java Virtual Machine (JVM). For information and examples on how to include an applet in an HTML page.
For this we need two files one java file and one html file. In the java file the below program should be pasted and should save as "Clock.java" and html file you have to include the program given bellow

Java Hello World

Basic Java Program
This is the first step to the java world. Here i am going to explain how can a simple string "Hello World" print or display in the command prompt. For this all we need is the JDK(java development kit).
You can download it from the link given below. If you had it, then you are ready now.
  Just install it and find the path where the jdk is located and the find the bin folder (for 64bit os it is "C:\Program Files\Java\jdk1.7.0_01\bin" , for 32 bit os it isC:\Program Files (x86)\Java\jdk1.7.0_01\bin) 
If you have any other version of java then you can replace the version no with yours..

Simple Calculator

SIMPLE CALCULATOR PROGRAM USING J FRAME


Given Bellow Is a java Program To create a simple calculator......
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener{
    // Variables