Java
break statement
public class
JavaBreakExample
{
public static void
main(String[] args)
{
int intArray[] = new
int[]{1,2,3,4,5};
System.out.println("Elements
less than 3 are : ");
for(int i=0; i < intArray.length
; i ++)
{
if(intArray[i] == 3)
break;
else
System.out.println(intArray[i]);
}
}
}
Java
break statement with label
public class JavaBreakWithLableExample
{
public static void main(String[] args)
{
int[][] intArray = new
int[][]{{1,2,3,4,5},{10,20,30,40,50}};
boolean blnFound = false;
System.out.println("Searching 30 in two
dimensional int array..");
for(int intOuter=0; intOuter <
intArray.length ; intOuter++)
{
for(int intInner=0; intInner <
intArray[intOuter].length; intInner++)
{
if(intArray[intOuter][intInner] == 30)
{
blnFound = true;
break Outer;
}
}
}
if(blnFound == true)
System.out.println("30 found in the
array");
else
System.out.println("30 not found in the
array");
}
}
Switch Statement
public class SwitchStatementExample
{
public static void main(String[] args)
{
switch(expression)
{
break;
case
value2:
default:
break;
}
for(int i=0; i <= 3 ; i++)
{
switch(i)
{
case 0:
System.out.println("i is 0");
break;
case 1:
System.out.println("i is 1");
break;
case 2:
System.out.println("i is 2");
break;
default:
System.out.println("i is greater than
2");
}
}
}
}
Free Flowing Switch Statement
public class FreeFlowingSwitchExample
{
public static void main(String[] args)
{
int i=0;
switch(i)
{
case 0:
System.out.println("i is 0");
case 1:
System.out.println("i is 1");
case 2:
System.out.println("i is 2");
default:
System.out.println("Free flowing switch
example!");
}
}
}
Nested Switch Statements
public class NestedSwitchExample
{
public static void main(String[] args)
{
int i = 0;
int j = 1;
switch(i)
{
case 0:
switch(j)
{
case 0:
System.out.println("i is 0, j is
0");
break;
case 1:
System.out.println("i is 0, j is
1");
break;
default:
System.out.println("nested default
case!!");
}
break;
default:
System.out.println("No matching case
found!!");
}
}
}
Free
Flowing Switch Statement
public class FreeFlowingSwitchExample
{
public static void main(String[] args)
{
int i=0;
switch(i)
{
case 0:
System.out.println("i is 0");
case 1:
System.out.println("i is 1");
case 2:
System.out.println("i is 2");
default:
System.out.println("Free flowing switch
example!");
}
}
}
Get Horizontal Alignment of JLabel
import javax.swing.JApplet;
import javax.swing.JLabel;
public class GetHorizontalAlignmentExample
extends JApplet
{
public void init()
{
JLabel label1 = new JLabel("JLabel Get
Horizontal Alignment Example."); add(label1);
int alignment =
label1.getHorizontalAlignment();
switch(alignment)
{
case JLabel.LEFT:
label1.setText("Left");
break;
case JLabel.CENTER:
label1.setText("Center");
break;
case JLabel.RIGHT:
label1.setText("Right");
break;
case JLabel.LEADING:
label1.setText("Leading");
break;
case JLabel.TRAILING:
label1.setText("Trailing");
break;
}
}
}
Set Vertical Alignment of JLabel
import javax.swing.JApplet;
import javax.swing.JLabel;
public class JLabelVerticalAlignmentExample
extends JApplet
{
public void init()
{
JLabel label1 = new JLabel("JLabel Get
Horizontal Alignment Example.");add(label1);
int alignment = label1.getVerticalAlignment();
switch(alignment)
{
case JLabel.LEFT:
label1.setText("Left");
break;
case JLabel.CENTER:
label1.setText("Center");
break;
case JLabel.RIGHT:
label1.setText("Right");
break;
case JLabel.LEADING:
label1.setText("Leading");
break;
case JLabel.TRAILING:
label1.setText("Trailing");
break;
}
}
}
JTextField Horizontal Alignment
import java.awt.FlowLayout;
import javax.swing.JApplet;
import javax.swing.JTextField;
public class
JTextFieldHorizontalAlignmentExample extends JApplet
{
public void init()
{
this.getContentPane().setLayout(new
FlowLayout());
JTextField field = new
JTextField("JTextField Horizontal Alignment Example", 10);
int horizontalAlignment =
field.getHorizontalAlignment();
switch(horizontalAlignment)
{
case JTextField.LEFT:
field.setText("Left");
break;
case JTextField.RIGHT:
field.setText("Right");
break;
case JTextField.CENTER:
field.setText("Center");
break;
case JTextField.LEADING:
field.setText("Leading");
break;
case JTextField.TRAILING:
field.setText("Trailing");
break;
}
field.setHorizontalAlignment(JTextField.CENTER);
add(field);
}
}
Do While loop
public class DoWhileExample
{
public static void main(String[] args)
{
dowhile();
nt i =0;
do
{
System.out.println("i is : " + i);
i++;
}while(i < 5);
}
}
Infinite For loop
public class InfiniteForLoop
{
public static void main(String[] args)
{
for(;;)
System.out.println("Hello");
}
}
Java StringBuffer setLength
public class JavaStringBufferSetLengthExample
{
public static void main(String[] args)
{
StringBuffer sbf = new
StringBuffer("StringBuffer setLength method example");
sbf.setLength(12);
System.out.println("StringBuffer
contents: " + sbf);
sbf.setLength(0);
System.out.println("StringBuffer contents
deleted:" + sbf);
}
}
Java String Buffer append new line
public class
JavaStringBufferAppendNewLineExample
{
public static void
main(String args[])
{
StringBuffer sbf = new
StringBuffer("This is the first line.");
sbf.append(System.getProperty("line.separator"));
sbf.append("This
is second line.");
System.out.println(sbf);
}
}
Java String Buffer to InputStream
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class StringBufferToInputStreamExample
{
public static void main(String args[])
{
StringBuffer sbf = new
StringBuffer("StringBuffer to InputStream Example");
byte[] bytes = sbf.toString().getBytes();
InputStream inputStream = new
ByteArrayInputStream(bytes);
System.out.println("StringBuffer
converted to InputStream");
}
}
String Buffer Reset Java
public class StrinBufferResetExample
{
public static void main(String[] args)
{
StringBuffer sbf = new
StringBuffer("Hello World!");
System.out.println("StringBuffer content:
" + sbf) ;
sbf.delete(0, sbf.length());
System.out.println("StringBuffer content
after reset:" + sbf);
}
}
String Buffer toString
public class JavaStringBufferToStringExample
{
public static void main(String args[])
{
StringBuffer sbf = new
StringBuffer("Hello World!");
String str = sbf.toString();
System.out.println("StringBuffer to
String: " + str);
}
No comments:
Post a Comment