How to find if a string starts with a particular word – JAVA ?

Hi,

In order to find whether a given string starts with a particular string or not in JAVA, use the following code.


public class Main {

  public static void main(String[] args) {

    String urStr = "Coderz Heaven";

    if (urStr.startsWith("Coderz")) {

      System.out.println("Yes! Your string starts with Coderz");

    }

    else {

      System.out.println("No!");

    }

  }

}

Fancy SrollBar in ANDROID…

Hello all..
ScrollBars are our basic needs in a UI if we have to show a content that do not fit fully into our layout.
A normal scrollBar doesn’t look so beautiful, but don’t worry ANDROID is so customizable that you can customize your scrollBar also.
Here is a simple demo to make a custom beautiful scrollBar.
Let’s start

package pack.coderzheaven;

import android.app.Activity;
import android.os.Bundle;

public class FancyScrollBarDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fancyscrollbar);
    }
}

Now the layout for the scrollbar “fancyscrollbar.xml”.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track"
    android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"
    android:scrollbarSize="12dip">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CoderzHeaven Fancy ScrollBar Demo"/>
    </LinearLayout>
</ScrollView>

Now create an xml inside the drawable folder and name it “scrollbar_vertical_thumb.xml” and copyt his code into it.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#3333FF" android:endColor="#8080FF"
            android:angle="0"/>
    <corners android:radius="6dp" />
</shape>

Now create another xml inside the drawable folder and name it “scrollbar_vertical_track.xml” and copyt his code into it.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#505050" android:endColor="#C0C0C0"
            android:angle="0"/>
 </shape>

You are done, Now go on and run it.
Enjoy.
Please leave your valuable comments.

Fancy ScrollBar Demo

Fancy ScrollBar Demo

Merge two layout xml in android

Merging layout is an excellent feature in android. Separate xml can be included in a single xml.

For this first i create a main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

 <TextView
	android:text="Account"
	android:id="@+id/account"
	android:layout_below="@+id/title"
	 android:padding="10dip"
	android:layout_alignParentLeft="true"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	>
</TextView>
<EditText
	android:id="@+id/youtube_feed_TF"
	android:hint="Username"
	android:layout_marginLeft="10dip"
	android:inputType="textEmailAddress"
	android:layout_below="@+id/account"
	android:layout_centerHorizontal="true"
	android:layout_width="fill_parent"
	android:padding="10dip"
	android:layout_height="wrap_content"
></EditText>
<include layout="@layout/footer2button"/>

</RelativeLayout>

In the bottom you can see that i have included a xml file named footer2button.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
	android:layout_width="fill_parent"
	android:gravity="bottom"
	android:id="@+id/bottomlayout"
	android:layout_height="wrap_content"
	android:layout_alignParentBottom="true">

			<Button android:layout_height="wrap_content"
				 android:id="@+id/backbutton"
				 android:text="Back"
				 android:layout_width="wrap_content"
				 android:layout_weight="1"
				 android:layout_alignParentBottom="true">
			</Button>

			<Button android:layout_height="wrap_content"
				android:id="@+id/nextbutton"
				 android:text="Next"
				 android:layout_width="wrap_content"
				 android:layout_weight="1"
				 android:layout_alignParentBottom="true">
		   </Button>

</LinearLayout>
</merge>

The Merged layout will be like this

How to Open camera in ANDROID?

Hello everyone..
In today’s tutorial I will show you how to use camera in ANDROID in your program.
In this tutorial we will be having a button which will open the camera and after taking the photo it will show it in an imageView.

Note: This program will work only in the real device not in the emulator. So make sure to test it in the device itself. Also make sure to add the permission while using camera as shown below in your AndroidManifest file.

 <uses-feature android:name="android.hardware.camera"></uses-feature>  

Here is the main java file code

package com.coderzheaven;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class OpenCameraDemo extends Activity {

	private static final int CAMERA_PIC_REQUEST = 2500;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				 startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
			}
		});
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
        	  Bitmap image = (Bitmap) data.getExtras().get("data");
              ImageView imageview = (ImageView) findViewById(R.id.ImageView01);
              imageview.setImageBitmap(image);
        }
    }
}

Now the main.xml file which contains the button and the imageview.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<ImageView
	android:id="@+id/ImageView01"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</ImageView>
<Button
	android:text="Open Camera"
	android:id="@+id/Button01"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>
</LinearLayout>

The strings.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, OpenCameraDemo!</string>
    <string name="app_name">OpenCamera</string>
</resources>

And the AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">

      <uses-feature android:name="android.hardware.camera"></uses-feature>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".OpenCameraDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Please leave your valuable comments on this post.

How to create a new file using JAVA ?

Hi,

For creating a new file using JAVA, use the following code.

import java.io.File;
public class MainClass {

  public static void main(String[] a)throws Exception {

    File myFile = new File("c:\temp\newText.txt");

    myFile.createNewFile();

  }

}

Exception Handling – Divide by Zero – Java Example

Hi,

Given below is a code snippet which will demonstrate an exception handling in Java with Divide by zero error.

public class MainClass {
  public static void main(String args[]) {

    int urAns, urDiv;

    try {

      urDiv = 0;
      urAns = 25 / urDiv;

      System.out.println("Do you really think this will print out? No! It won't!");

    }

    catch (ArithmeticException e) {

      System.out.println("Division by zero not Possible!");

    }

    System.out.println("This will print out after Exception Handling");
  }

}

:)

Simple String Concatenation Example in JAVA

Hi,

Given below is a simple concatenation example in JAVA where two strings are joined together to form a single string.

public class MainClass {

  public static void main(String[] arg) {

    String yourString = "Coderz" + "Heaven";

    System.out.println(yourString);

  }

}

And…the output will be.. “CoderzHeaven”
:)

How to search for substring from a string in JAVA ?

Hi,

For searching for a particular substring from a starting index of a string and get the starting index of it in JAVA, use the following sample of code.


public class MainClass{

  public static void main(String[] arg){

    String myStr = "coderzco";

    int begIndex= 2;

    int myIndex= 0;

    myIndex = str.indexOf("co", begIndex);

    System.out.println(myIndex);
  }

}

It will print out the starting index of the substring ‘co’ (the second ‘co’), that is 6.

:)

How to search for characters from strings in JAVA ?

Hi,

For searching for a particular character from a string and getting the index of that particular character, use the following code.


public class MainClass{

  public static void main(String[] arg){

    String myStr = "Coderz";

    int strIndex = 0;
    strIndex = myStr.indexOf('o');

    System.out.println(strIndex);
  }

}

It will print out the index of ‘o’, that’s 1.
:)

How to get the UUID of an iPhone/iPod/iPad ?

Hi,

In order to get the UUID of an iPhone, iPod or iPad, do the following in your code.

NSString *myUUIDStr= [UIDevice currentDevice].uniqueIdentifier;

Here ‘myUUIDStr’ will contain the UUID of your specified iOS device.
:)

How to make UITextField a Password Field in iPhone ?

Hi,

In order to make a UITextField a Password Field / Password Mode do the following.

In your .h class file,

IBOutlet UITextField *urPassword;

In your .m class file,

urPassword.secureTextEntry = YES;

This will make your UITextField in a Password entry mode or a Password Field.
:)

How to set a UITextView font type and size in iPhone ?

Hi,

In order to set the font type of UITextView, do the following.

In your .h class file,

IBOutlet UITextView *urTextView;

In your .m class file,

[urTextViewsetFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:18]];

This will set ‘urTextView’ with font name ‘Arial Rounded MT Bold’ with font size 18.
:)

How to make a UITextField without border in iPhone ?

Hi,

In order to make a UITextField with out or no border, do like this.

In your .h class file,

IBOutlet UITextField *urTextField;

In your .m class file,

urTextField.borderStyle = UITextBorderStyleNone;

Thus you will get a UITextField named urTextField without border.
:)

Sort elements in a String Array – C Sharp / C#

Hi,

In order to sort the elements in a string array in C Sharp/C#, we use the sort() method. Given below is a simple example using sort() method to sort the elements in a string array.

using System;

class MainClass
{

  public static void Main()
  {

    string[] urStringArray = {"xylo", "apple", "zeebra", "coderz", "heaven123", "heaven777"};
    Array.Sort(urStringArray );
    Console.WriteLine("Sorted string Array:");
    for (int i = 0; i < urStringArray.Length; i++)
    {
      Console.WriteLine("urStringArray[" + i + "] = " + urStringArray[i]);
    }
  }

}

It will print out,
Sorted string Array:
urStringArray[0] = apple
urStringArray[1] = coderz
urStringArray[2] = heaven123
urStringArray[3] = heaven777
urStringArray[4] = xylo
urStringArray[5] = zeebra
:)

How to use SeekBar in ANDROID?

Here is a simple example to show how to use seek Bar in android.

Create a new project and place this code in it.

package com.coderzheaven;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class SeekBarDemo extends Activity  implements SeekBar.OnSeekBarChangeListener {
    SeekBar mSeekBar;
    TextView mProgressText;
    TextView mTrackingText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSeekBar = (SeekBar)findViewById(R.id.seek);
        mSeekBar.setOnSeekBarChangeListener(this);
        mProgressText = (TextView)findViewById(R.id.progress);
        mTrackingText = (TextView)findViewById(R.id.tracking);
    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        mProgressText.setText(progress + " " +
                getString(R.string.seekbar_from_touch) + "=" + fromTouch);
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText(getString(R.string.seekbar_tracking_on));
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText(getString(R.string.seekbar_tracking_off));
    }
}

The main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <SeekBar android:id="@+id/seek"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />

    <TextView android:id="@+id/progress"
       	android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/tracking"
       	android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

The strings.xml file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">SeekBarDemo</string>
    <string name="seekbar_tracking_on">Tracking on</string>
    <string name="seekbar_tracking_off">Tracking off</string>
    <string name="seekbar_from_touch">from touch</string>
</resources>
SeekBar Demo in ANDROID

SeekBar Demo in ANDROID

Here is another example to show How to get the current progress on your SeekBar in android?

Please leave your valuable cmments

Styling text in ANDROID through XML.

Hello….

In today’s tutorial I will show you how to style your text in ANDROID.
This is a simple example to show how styling is done using XML in android.
This will also show you how to get a string from the resources in java code.

First create a new project and name it “StyleTextDemo” and copy this code into it.

package pack.coderzheaven;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;

public class StyleTextDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.resources);

        TextView tv;
        CharSequence cs;
        String str;

        // Using the getString() conevenience method, retrieve a string
        // resource that happens to have style information.  Note the use of
        // CharSequence instead of String so we don't lose the style info.
        cs = getText(R.string.styled_text);
        tv = (TextView)findViewById(R.id.styled_text);
        tv.setText(cs);

        // Use the same resource, but convert it to a string, which causes it
        // to lose the style information.
        str = getString(R.string.styled_text);
        tv = (TextView)findViewById(R.id.plain_text);
        tv.setText(str);

        Context context = this;

        // Get the Resources object from our context
        Resources res = context.getResources();

        // Get the string resource, like above.
        cs = res.getText(R.string.styled_text);
        tv = (TextView)findViewById(R.id.res1);
        tv.setText(cs);
    }
}

Now in the “main.xml” file copy the following code. This is simply for the layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

   <TextView
        android:id="@+id/coderz"
        android:text="@string/coderz"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        android:padding="10dip"
        />
    <TextView
        android:id="@+id/styled_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        />

    <TextView
        android:id="@+id/plain_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        />

    <TextView
        android:id="@+id/res1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        />
</LinearLayout>

Now the third step in the strings.xml file, place this code.

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name="coderz"><b><i>Style Text Demo from CoderzHeaven, Enjoy</i></b></string>
     <string name="styled_text">Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string>
</resources>

You are done, Now go on and run it.
Enjoy
Please leave your comments on this post and encourage us.

Style Text Demo in android

Style Text Demo in android

A simple Exception Handling example – C Sharp/C#

Hi,

As you know an exception is an error that occurs during the runtime.
Here is a simple exception handling example using C Sharp/C#.

using System;

class MainClass{

    public static void Main(){
        int value= 0;
        try {
            int i = 7/value;
        }
        catch (DivideByZeroException e) // catching divide by zero exception
        {
            Console.WriteLine("DivideByZero {0}", e);
        }
        catch (Exception e) // catching any other exceptions
        {
            Console.WriteLine("Exception {0}", e);
        }
    }
}

:)

Rename Namespace in C Sharp/C#

Hi,

This is how you can simply rename a namespace in C Sharp/C#,

using urNamedClass = System.Console;

class MainClass
{
    public static void Main()
    {
        urNamedClass.WriteLine("Hello");
    }
}

This will print out ‘Hello’! :)

C++ program to add two complex nos.

The program contains a class complex with two member variables x and y and a string class with one member variable. Two objects are created for complex class and string class and accepted values through read () function and their values are added using the operator overloaded function and assigned to the third object for each class. Values of the added object are displayed using the display () function. The + operator is overloaded here.

#include<iostream.h>
#include<string.h>
#include<conio.h>
class number
{
	private:
		int num1,num2;

	public:
		void input()
		{
			cout<<"nntEnter the real part:";
			cin>>num1;
			cout<<"ntEnter the imaginary part:";
			cin>>num2;
			cout<<"n";
		}
		number operator + (number x)      /*Operator overloading*/
		{
			number temp;
			temp.num1 = num1 + x.num1;
			temp.num2 = num2 + x.num2;
			return(temp);
		}

		void display()
		{
			cout<<"nntSum of two numbers is: "<<num1<<" + i "<<num2;

		}
};

Output

First Number
Enter the real part: 5
Enter the imaginary part: 4

Second Number
Enter the real part: 1
Enter the imaginary part: 2

Sum of 2 no is : 6 + i 6

C++ program append the two text files

Three file objects are created using the fstream class one for first, second for second file and third for the appended file. The first two files are opened in the input mode and third in the append mode. The first file is opened and checked for error condition if the file exist its contents are written to the third file else the program is aborted with a message on the screen. This process is repeated for the second file also .If the two files exist ,the first file contents are written first and second file contents are written to the third file by appending to its present contents.

#include<iostream.h>
            #include<conio.h>
#include<fstream.h>
#include<process.h>
void main()
{
		fstream file1, file2,file3;
		char ch;
		file1.open("file1.txt",ios :: out);
		file2.open("file2.txt",ios :: out);
		cout << "n Enter data in file 1 n";
		cin.get(ch);
		while(ch != 'n')
		{
			  file1.put(ch);
	 		 cin.get(ch);
		}
		cout << "n Enter data in file 2 n";
		cin.get(ch);
		while(ch != 'n')
		{
			  file2.put(ch);
			  cin.get(ch);
		}
		file1.close();	file2.close();

		file1.open("file1.txt",ios :: in);
		file2.open("file2.txt",ios :: in);
		file3.open("file3.txt",ios :: out | ios :: trunc | ios :: app);
		if(!file1)
		{
			 cout << "n First file not found ";
			 getch();
			 exit(0);
		}
		while(file1)
		{
			 file1.get(ch);
			 file3.put(ch);
		}
		if(!file2)
		{
			 cout << "n Second file not found ";
	 		getch();
			 exit(0);
		}
		while(file2)
		{
			 file2.get(ch);
			 file3.put(ch);
		}
		file1.close();
		file2.close();
		file3.close();
		cout << "nt Appended file contents n";
		file3.open("file3.txt",ios :: in);
		while(file3)
		{
			 file3.get(ch);
			 cout.put(ch);
		}
		getch();
}

Output :

Enter data in file 1
I am coder
Enter data in file 2
I am an student.
Appended file contents
I am coder. I am an student.

C++ program to copy the contents of a text file to another

Two file objects are created using the fstream class for the two files, one for source file and another destination file. The first file is opened and its contents are copied to the second file using get ( ) and put ( ) function. Each time the source file is opened error checking is done to ensure that the file is existing else the program is aborted with a message.

If the contents are successfully copied, the destination file contents are displayed on the screen.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<process.h>

void main()
{
		fstream file1, file2;
		char ch;
		clrscr();
		file1.open("file1.txt",ios :: out);
		cout << "nt Press 'enter key' to quit nn";
		cout << "n Enter the data n";
		cin.get(ch);
		while(ch != 'n')
		{
	   		 file1.put(ch);
	    		cin.get(ch);
		}
		file1.close();
		file1.open("file1.txt",ios :: in);
		if(!file1)
		{
			 cout<< "n File1 not found !! n";
			 getch();
			 exit(0);		// aborting…
		}
		cout << "n First file contents n";
		while(file1)
		{
			 file1.get(ch);
			 cout.put(ch);
		}
		file1.seekg(0);		      	    /* copying process..... */
		file2.open("file2.txt",ios :: out );
		cout << "nn First file contents copied to second file... n";
		while(file1)
		{
			 file1.get(ch);
			 file2.put(ch);
		}
		file1.seekg(0);
		file2.close();
		file2.open("file1.txt",ios :: in);
		if(!file1)
		{
			 cout<< "n File1 not found !! n";
			 getch();
			 exit(0); 		// aborting…
		}
		cout << "n Second file contents n";
		while(file2)
		{
			 file2.get(ch);
			 cout.put(ch);
		}
		file2.close();
		getch();
}

The output is like this

Press ‘enter key’ to quit
Enter the data
I am an Indian
First file contents
I am an Indian
First file contents copied to second file…
Second file contents
I am an Indian

How to make Non Colliding bodies in Box2D ?

Hi,

For making non colliding bodies in Box2D, you may need to give SAME, NEGATIVE, group index to the b2FixtureDef of bodies you created. See the following code.

fixtureDefA.filter.groupIndex = -7;
fixtureDefB.filter.groupIndex = -7;

The bodies with the above two fixture definitions won’t collide.
:)

How to Pause and Resume schedulers and Actions in cocos2D ?

Hi,

For Pausing all schedulers and actions in Cocos2D, use this line.

[self pauseSchedulerAndActions];

For resuming the paused schedulers and actions, use this.

[self resumeSchedulerAndActions];

:)

How to open URL from iPhone application ?

Hi,

For opening a Web Link/URL from an iPhone application, follow the two simple steps.
Step 1 :
Create an NSURL object and initialize it with your URL string

 NSURL *myURL = [NSURL URLWithString:@"http://www.coderzheaven.com/"];

;

Step 2 :
Use the openURL method by passing your NSURL object

[[UIApplication sharedApplication] openURL: myURL];

Done. :)

How to know whether your b2Body is moving or not in Box2D ?

Hi,

Some times while game building via Box2D Physics Engine, you may need to find out whether your bodies (from b2Body) are moving or not. Use the following sample of code to find out whether the given b2Body is moving or not.


b2Body *urBody;

if(urBody->IsAwake()
{
        //urBbody is moving! It's not in rest!
}

else
{
        //urBody is at rest!
}

:)