Showing posts with label Java AppCodes. Show all posts
Showing posts with label Java AppCodes. Show all posts

How to read XML file in java



books.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<library>
 <book>
   <name>Head First Java, 2nd Edition</name>
   <author>Kathy Sierra and Bert Bates</author>
   <publication-date>09-Feb-2005</publication-date>
 </book>
 <book>
   <name>Effective Java</name>
   <author>Joshua Bloch</author>
   <publication-date>28-May-2008</publication-date>
 </book>
 <book>
   <name>Java How to Program, 7th Edition</name>
   <author>Harvey M. Deitel and Paul J. Deitel</author>
   <publication-date>6-Jan-2007</publication-date>
 </book>
</library>

------------------------------------------------------------
readDOMXML.jsp

<%@ page language="java" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="javax.xml.parsers.DocumentBuilder" %>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory" %>
<%
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db =dbf.newDocumentBuilder();
    Document doc=db.parse("c:\\books.xml");
   
   
    NodeList nl = doc.getElementsByTagName("book");
   
%>
<html>
<head>
<title>How to read XML file in JAVA</title>
</head>
<body>
<%

for(int i=0;i<nl.getLength();i++)
{
  NodeList nameNlc=    doc.getElementsByTagName("name");
  Element nameElements=(Element)nameNlc.item(i);
  String nameTagValue=nameElements.getChildNodes().item(0).getNodeValue();
 
 
  NodeList authorNlc=    doc.getElementsByTagName("author");
  Element authorElements=(Element)authorNlc.item(i);
  String authorTagValue=authorElements.getChildNodes().item(0).getNodeValue();
 
  NodeList dateNlc=    doc.getElementsByTagName("publication-date");
  Element dateElements=(Element)dateNlc.item(i);
  String dateTagValue=dateElements.getChildNodes().item(0).getNodeValue();
 
  out.println("name :"+nameTagValue+"<br>");  
  out.println("author :"+authorTagValue+"<br>");  
  out.println("publication-date :"+dateTagValue+"<br><br>");  
}

%>

</body>
</html>
READ MORE - How to read XML file in java

print/draw Diamond Shape using Asterisk in java


public class Diamond
{
    public String Diamond_Asterisk(int num)
    {
        if(num>0)
        {
            return "*-" + Diamond_Asterisk(num-1);
        }
        else
        {
            return "-";
        }
    }
     public String Diamond_Asterisk2(int num)
    {
        if(num>0)
        {
            return "-*-" + Diamond_Asterisk(num-1);
        }
        else
        {
            return "-";
        }
    }

    public String Space(int num)
    {
        if(num>0)
        {
            return "-" + Space(num-1);
        }
        else
        {
            return "-";
        }
    }
    public void DiamondResult(int num)
    {
        for(int i=1; i<num; i++)
        {
            System.out.print(Space(num-i));
             System.out.println(Diamond_Asterisk(i));
            
        }
         for(int i=0; i<num; i++)
        {
            System.out.println(Diamond_Asterisk2(num-i));
            System.out.print(Space(i));

        }
     
    }
    

}


//main class


import java.util.Scanner;

public class Main {

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = input.nextInt();

        Diamond access = new Diamond();
        System.out.println("The shape for this is: ");
        access.DiamondResult(num);
        
    }

}
===================================
Sample Output 1: Enter a number: 3 The shape for this is: ---*-- --*-*-- -*-*-*-- --*-*-- ---*-- Sample Output 2: Enter a number: 4 The shape for this is: ----*-- ---*-*-- --*-*-*-- -*-*-*-*-- --*-*-*-- ---*-*-- ----*--
READ MORE - print/draw Diamond Shape using Asterisk in java

Print Different Asterisk Shapes in java


//main method
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
Asterisk1 access = new Asterisk1();
access.asterisk2(n);
}
}
//java class
public class Asterisk1 {
private int count = 0;
public void asterisk1(int n) {
if(n==0) {
System.out.println();
}
else {
System.out.print("*");
asterisk1(n-1); } }
public void asterisk2( int n) {
if(n==0){
return; }
else {
asterisk1(n);
asterisk2(n-1);
}
}
}
This program will output the asterisk in this form. If the number 4 is entered the form will be like this.
****
***
**
*
2. A Java source code for another Asterisk Form
/*The following code will have the same method as the first but different in implementation*/ //main method
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
Asterisk1 access = new Asterisk1();
access.asterisk2(n,1);
}
}
// java class
public class Asterisk1 {
private int count = 0;
public void asterisk1(int n) {
if(n==0) {
System.out.println();
}
else {
System.out.print("*");
asterisk1(n-1);
}
}
public void asterisk3(int count,int m) {
if( count == 0) {
System.out.println();
} else {
asterisk1(m);
asterisk3(n-1,m+1);
}
}
}
The output of this code if number 4 is entered will be like this.
*
**
***
****

To get a form that would look like this,
****
***
**
*
*
**
***
****
3. Here is the other code.

//main method
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
Asterisk1 access = new Asterisk1();
access.asterisk2(n); access.asterisk3(n,1);
}
}
// java class
public class Asterisk1 {
private int count = 0;
public void asterisk1(int n) {
if(n==0) { System.out.println();
} else {
System.out.print("*");
asterisk1(n-1);
}
}
public void asterisk2( int n) {
if(n==0) {
return;
} else {
asterisk1(n);
asterisk2(n-1);
}
}
public void asterisk3( int n,int m)
{ if( n == 0) {
System.out.println();
} else {
asterisk1(m);
asterisk3(n-1,m+1);
}
}
}
4. Lastly, to acquire a shape like this,
*
**
***
****
****
***
**
*
here is the code:
//main method
package prog_proj2;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
Asterisk1 access = new Asterisk1();
access.asterisk3(n); access.asterisk2(n);
}
}
//java class
public class Asterisk1 {
private int count = 0;
public static void asterisk1(int n) {
if(n==0) { System.out.println();
} else {
System.out.print("*");
asterisk1(n-1);
}
}
public void asterisk2( int n) {
if(n==0) { return;
} else {
asterisk1(n);
asterisk2(n-1);
}
}
public void asterisk3( int n) {
count++;
if( n == 0) {
return;
} else {
asterisk1(count);
asterisk3(n-1);
}
}
}
READ MORE - Print Different Asterisk Shapes in java

Java program to Read text File Line by Line


import java.io.*;
class FileRead
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (strLine);
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}
READ MORE - Java program to Read text File Line by Line

Simple console print statement demonstration


public class ConsolePrintApplet1 extends java.applet.Applet
{
  public void init ()
  {

    // Put code between this line
    //------------------------------------------
    double x = 5.0;
    double y = 3.0;
    System.out.println ("x * y = " + (x*y));
    System.out.println ("x / y = " + (x/y));

    // and this line.
    //------------------------------------------
  }

  // Paint message in Applet window.
  public void paint (java.awt.Graphics g) {
     g.drawString ("ConsolePrintApplet1",10,20);
  }
}
public class ConsolePrintApp1 extends java.applet.Applet
{
  public static void main (String [] args) {
    // Put code between this line
    //------------------------------------------

    double x = 5.0;
    double y = 3.0;
    System.out.println ("x * y = " + (x*y));
    System.out.println ("x / y = " + (x/y));

    //------------------------------------------
    // and this line.
  }

}
READ MORE - Simple console print statement demonstration

HTTPS URL Connection program in Java

import java.io.*; import java.net.*; import java.security.Security.*; import com.sun.net.ssl.*; import com.sun.*; public class sslpost { public static void main(String[] args){ String cuki=new String(); try { System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol"); java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); URL url = new URL("https://www.sunpage.com.sg/sso/login.asp"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setFollowRedirects(true); String query = "UserID=" + URLEncoder.encode("williamalex@hotmail.com"); query += "&"; query += "password=" + URLEncoder.encode("password"); query += "&"; query += "UserChk=" + URLEncoder.encode("Bidder"); // This particular website I was working with, required that the referrel URL should be from this URL // as specified the previousURL. If you do not have such requirement you may omit it. query += "&"; query += "PreviousURL=" + URLEncoder.encode("https://www.sunpage.com.sg/sso/login.asp"); //connection.setRequestProperty("Accept-Language","it"); //connection.setRequestProperty("Accept", "application/cfm, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, ///"); //connection.setRequestProperty("Accept-Encoding","gzip"); connection.setRequestProperty("Content-length",String.valueOf (query.length())); connection.setRequestProperty("Content-Type","application/x-www- form-urlencoded"); connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"); // open up the output stream of the connection DataOutputStream output = new DataOutputStream( connection.getOutputStream() ); // write out the data int queryLength = query.length(); output.writeBytes( query ); //output.close(); System.out.println("Resp Code:"+connection.getResponseCode()); System.out.println("Resp Message:"+ connection.getResponseMessage()); // get ready to read the response from the cgi script DataInputStream input = new DataInputStream( connection.getInputStream() ); // read in each character until end-of-stream is detected for( int c = input.read(); c != -1; c = input.read() ) System.out.print( (char)c ); input.close(); } catch(Exception e) { System.out.println( "Something bad just happened." ); System.out.println( e ); e.printStackTrace(); } } }
READ MORE - HTTPS URL Connection program in Java

Convert numbers to word in java


import java.util.*;
public class NumtoWord 
{ 
public static void main(String[] args) 
{ 
String a;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Number : ");
a = s.next();
int b = a.length();
String str[] = {"1","One","2","Two","3","Three","4","Four","5","Five","6","Six","7","Seven","8","Eight","9","Nine","10","Ten","11","Eleven","12","Twelve","13","Thirteen","14","Forteen","15","Fifteen","16","Sixteen","17","Seventeen","18","Eighteen","19","Nineteen","20","Twenty","30","Thirty","40","Fourty","50","Fifty","60","Sixty","70","Seventy","80","Eighty","90","Ninty","100","Hundred"};
System.out.println("");
if (b==9) 
{ 
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s6= a.substring(5,6);
String s7= a.substring(6,7);
String s8= a.substring(7,8);
String s9= a.substring(8,9);
String s10= a.substring(0,2);
String s11= a.substring(2,4);
String s12= a.substring(4,6);
String s14= a.substring(7,9);
{
if (s10.equals("00"))
System.out.print("");
else if (s1.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10)) 
System.out.print("\n" + str[r+1] + " Crore ");
}
else
{
{
for (int i=0;i<=40;i++)
if (str[i].equals(s1))
System.out.print("\n" + str[i+37] + " ");
}
{
if(s2.equals("0"))
{
System.out.print("Crore ");
}
else 
for (int j=0;j<=40;j++)
{
if (str[j].equals(s2))
System.out.print(str[j+1] + " Crore ");
}
}
}
}
{
if (s11.equals("00"))
System.out.print("");
else if (s3.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11)) 
System.out.print(str[r+1] + " Lacks ");
}
else
{
{
for (int k=0;k<=38;k++)
if (str[k].equals(s3))
System.out.print(str[k+37] + " ");
}
{
if(s4.equals("0"))
{
System.out.print("Lacks ");
}
else 
for (int l=0;l<=38;l++)
{
if (str[l].equals(s4))
System.out.print(str[l+1] + " Lacks ");
}
}
}
}
{
if (s12.equals("00"))
System.out.print("");
else if (s5.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s12)) 
System.out.print(str[r+1] + " Thousand ");
}
else
{ 
{
for (int m=0;m<=38;m++)
if (str[m].equals(s5))
System.out.print(str[m+37] + " ");
}
{
if(s6.equals("0"))
{
System.out.print("Thousand ");
}
else 
for (int n=0;n<=38;n++)
{
if (str[n].equals(s6))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
for (int o=0;o<=40;o++)
if (str[o].equals(s7))
System.out.print(str[o+1] + " Hundred ");
}
{
if (s14.equals("00"))
System.out.print("");
else if (s8.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s14)) 
System.out.print(str[r+1]);
System.out.print("\n"); 
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s8))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s9))
System.out.print(" " + str[q+1]);
}
} 
System.out.print("\n"); 
}
}
else if (b==8) 
{ 
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s6= a.substring(5,6);
String s7= a.substring(6,7);
String s8= a.substring(7,8);
String s10= a.substring(1,3);
String s11= a.substring(3,5);
String s12= a.substring(6,8);
{
if (s1.equals("0"))
System.out.print("");
else
for (int i=0;i<=40;i++)
if (str[i].equals(s1))
System.out.print("\n" + str[i+1] + " Crore ");
} 
{
if (s10.equals("00"))
System.out.print("");
else if (s2.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10)) 
System.out.print(str[r+1] + " Lacks ");
}
else
{
{
for (int k=0;k<=38;k++)
if (str[k].equals(s2))
System.out.print(str[k+37] + " ");
}
{
if(s3.equals("0"))
{
System.out.print("Lacks ");
}
else 
for (int l=0;l<=38;l++)
{
if (str[l].equals(s3))
System.out.print(str[l+1] + " Lacks ");
}
}
}
}
{
if (s11.equals("00"))
System.out.print("");
else if (s4.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11)) 
System.out.print(str[r+1] + " Thousand ");
}
else
{ 
{
for (int m=0;m<=38;m++)
if (str[m].equals(s4))
System.out.print(str[m+37] + " ");
}
{
if(s5.equals("0"))
{
System.out.print("Thousand ");
}
else 
for (int n=0;n<=38;n++)
{
if (str[n].equals(s5))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
for (int o=0;o<=40;o++)
if (str[o].equals(s6))
System.out.print(str[o+1] + " Hundred ");
}
{
if (s12.equals("00"))
System.out.print("");
else if (s7.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s12)) 
System.out.print(str[r+1]);
System.out.print("\n"); 
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s7))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s8))
System.out.print(" " + str[q+1]);
}
} 
System.out.print("\n"); 
}
}
else if (b==7) 
{ 
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s6= a.substring(5,6);
String s7= a.substring(6,7);
String s10= a.substring(0,2);
String s11= a.substring(2,4);
String s12= a.substring(5,7);
{
if (s10.equals("00"))
System.out.print("");
else if (s1.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10)) 
System.out.print(str[r+1] + " Lacks ");
}
else
{
{
for (int k=0;k<=38;k++)
if (str[k].equals(s1))
System.out.print(str[k+37] + " ");
}
{
if(s2.equals("0"))
{
System.out.print("Lacks ");
}
else 
for (int l=0;l<=38;l++)
{
if (str[l].equals(s2))
System.out.print(str[l+1] + " Lacks ");
}
}
}
}
{
if (s11.equals("00"))
System.out.print("");
else if (s3.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11)) 
System.out.print(str[r+1] + " Thousand ");
}
else
{ 
{
for (int m=0;m<=38;m++)
if (str[m].equals(s3))
System.out.print(str[m+37] + " ");
}
{
if(s4.equals("0"))
{
System.out.print("Thousand ");
}
else 
for (int n=0;n<=38;n++)
{
if (str[n].equals(s4))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
for (int o=0;o<=40;o++)
if (str[o].equals(s5))
System.out.print(str[o+1] + " Hundred ");
}
{
if (s12.equals("00"))
System.out.print("");
else if (s6.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s12)) 
System.out.print(str[r+1]);
System.out.print("\n"); 
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s6))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s7))
System.out.print(" " + str[q+1]);
}
} 
System.out.print("\n"); 
}
}
else if (b==6) 
{ 
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s6= a.substring(5,6);
String s10= a.substring(1,3);
String s11= a.substring(4,6);

{
if(s1.equals("0"))
System.out.print("");
else 
{
for (int j=0;j<=40;j++)
if (str[j].equals(s1)) 
System.out.print(str[j+1] + " Lacks ");
}
}
{
if (s10.equals("00"))
System.out.print("");
else if (s2.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10)) 
System.out.print(str[r+1] + " Thousand ");
}
else
{ 
{
for (int m=0;m<=40;m++)
if (str[m].equals(s2))
System.out.print(str[m+37] + " ");
}
{
if(s3.equals("0"))
{
System.out.print("Thousand ");
}
else 
for (int n=0;n<=38;n++)
{
if (str[n].equals(s3))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
if(s4.equals("0"))
System.out.print("");
else 
{
for (int o=0;o<=40;o++)
if (str[o].equals(s4)) 
System.out.print(str[o+1] + " Hundred ");
}
}
{
if (s11.equals("00"))
System.out.print("");
else if (s5.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11)) 
System.out.print(str[r+1]);
System.out.print("\n"); 
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s5))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s6))
System.out.print(" " + str[q+1]);
}
} 
System.out.print("\n"); 
}
}
else if (b==5) 
{ 
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s5= a.substring(4,5);
String s10= a.substring(0,2);
String s11= a.substring(3,5);
{
if (s10.equals("00"))
System.out.print("");
else if (s1.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10)) 
System.out.print(str[r+1] + " Thousand ");
}
else
{ 
{
for (int m=0;m<=38;m++)
if (str[m].equals(s1))
System.out.print(str[m+37] + " ");
}
{
if(s2.equals("0"))
{
System.out.print("Thousand ");
}
else 
for (int n=0;n<=38;n++)
{
if (str[n].equals(s2))
System.out.print(str[n+1] + " Thousand ");
}
}
}
}
{
for (int o=0;o<=40;o++)
if (str[o].equals(s3))
System.out.print(str[o+1] + " Hundred ");
}
{
if (s11.equals("00"))
System.out.print("");
else if (s4.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s11)) 
System.out.print(str[r+1]);
System.out.print("\n"); 
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s4))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s5))
System.out.print(" " + str[q+1]);
}
} 
System.out.print("\n"); 
}
}
else if (b==4) 
{ 
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s4= a.substring(3,4);
String s10= a.substring(2,4);
{
if(s1.equals("0"))
System.out.print("");
else 
{
for (int j=0;j<=40;j++)
if (str[j].equals(s1)) 
System.out.print(str[j+1] + " Thousand ");
}
}
{
if(s2.equals("0"))
System.out.print("");
else 
{
for (int o=0;o<=40;o++)
if (str[o].equals(s2)) 
System.out.print(str[o+1] + " Hundred ");
}
}
{
if (s10.equals("00"))
System.out.print("");
else if (s3.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10)) 
System.out.print(str[r+1]);
System.out.print("\n"); 
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s3))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s4))
System.out.print(" " + str[q+1]);
}
} 
System.out.print("\n"); 
}
}
else if (b==3) 
{ 
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s3= a.substring(2,3);
String s10= a.substring(1,3);
{
if(s1.equals("0"))
System.out.print("");
else 
{
for (int o=0;o<=40;o++)
if (str[o].equals(s1)) 
System.out.print(str[o+1] + " Hundred ");
}
}
{
if (s10.equals("00"))
System.out.print("");
else if (s2.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10)) 
System.out.print(str[r+1]);
System.out.print("\n"); 
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s2))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s3))
System.out.print(" " + str[q+1]);
}
} 
System.out.print("\n"); 
}
}
else if (b==2) 
{ 
String s1= a.substring(0,1);
String s2= a.substring(1,2);
String s10= a.substring(0,2);
{
if (s10.equals("00"))
System.out.print("");
else if (s1.equals("1"))
{
for (int r=0;r<=40;r++)
if (str[r].equals(s10)) 
System.out.print(str[r+1]);
System.out.print("\n"); 
}
else
{
for (int p=0;p<=40;p++)
if (str[p].equals(s1))
System.out.print(str[p+37]);
for (int q=0;q<=40;q++)
{
if (str[q].equals(s2))
System.out.print(" " + str[q+1]);
}
} 
System.out.print("\n"); 
}
}
else if (b==1) 
{ 
String s1= a.substring(0,1);
for (int q=0;q<=40;q++)
if (str[q].equals(s1))
System.out.print(" " + str[q+1]);
} 
System.out.println("\n"); 
}
}
READ MORE - Convert numbers to word in java

Bubble Sort in Java


public class TestBubbleSort {
    public static void main(String[] args) {
        int unsortedArray[] = {10, 97, 6, 23, 0, -45, 697, -1000, 1, 0}; //Random set of numbers for example.
        int i;
        
        bubbleSort(unsortedArray, unsortedArray.length); //Pass the array to be sorted and its length.
        
        System.out.println("After sorting, the list elements are: "); //Just to show you it worked. :)
        
        for(i=0; i<unsortedArray.length; i++) {
            System.out.print(unsortedArray[i] + " ");
        }
    }

    private static void bubbleSort(int[] unsortedArray, int length) {
        int temp, counter, index;
        
        for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array.
            for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter.
                if(unsortedArray[index] > unsortedArray[index+1]) { //Test if need a swap or not.
                    temp = unsortedArray[index]; //These three lines just swap the two elements:
                    unsortedArray[index] = unsortedArray[index+1];
                    unsortedArray[index+1] = temp;
                }
            }
        }
    }
}
READ MORE - Bubble Sort in Java

Java Program to Send SMS


public class SMSClient implements Runnable{

  public final static int SYNCHRONOUS=0;
  public final static int ASYNCHRONOUS=1;
  private Thread myThread=null;

  private int mode=-1;
  private String recipient=null;
  private String message=null;

  public int status=-1;
  public long messageNo=-1;


  public SMSClient(int mode) {
      this.mode=mode;
    }

  public int sendMessage (String recipient, String message){
    this.recipient=recipient;
    this.message=message;
    //System.out.println("recipient: " + recipient + " message: " + message);
    myThread = new Thread(this);
    myThread.start();
//    run();
    return status;
    }
    public void run(){

    Sender aSender = new Sender(recipient,message);

    try{
      //send message
          aSender.send ();

         // System.out.println("sending ... ");

      //in SYNCHRONOUS mode wait for return : 0 for OK,
      //-2 for timeout, -1 for other errors
      if (mode==SYNCHRONOUS) {
          while (aSender.status == -1){
            myThread.sleep (1000);
          }
      }
      if (aSender.status == 0) messageNo=aSender.messageNo ;

    }catch (Exception e){

        e.printStackTrace();

    }

    this.status=aSender.status ;

    aSender=null;


  }
}
READ MORE - Java Program to Send SMS

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|