[Go to site: main page, start]

0% found this document useful (0 votes)
15 views16 pages

Basic Java Programs Overview

The document contains code snippets for various Java programs including: 1. Splitting a string into words using regular expressions and counting the number of words. 2. Reading a file, splitting lines into words using regular expressions, and counting the number of lines and words. 3. Finding prime numbers within a range using nested for loops. 4. Printing star patterns of increasing number of stars using for loops. 5. Connecting to a database, getting metadata, and querying for table column names and data types.

Uploaded by

ramuthefriend
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views16 pages

Basic Java Programs Overview

The document contains code snippets for various Java programs including: 1. Splitting a string into words using regular expressions and counting the number of words. 2. Reading a file, splitting lines into words using regular expressions, and counting the number of lines and words. 3. Finding prime numbers within a range using nested for loops. 4. Printing star patterns of increasing number of stars using for loops. 5. Connecting to a database, getting metadata, and querying for table column names and data types.

Uploaded by

ramuthefriend
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

/*TO SPLIT THE WORDS*\

Package [Link]
Import [Link] . Pattern;
Public Class RegexDemo{
Public static void main(String args[ ]){
Int count=0;
String s1=hi ram how are you;
Pattern p = [Link](||s);
String [ ] ss = [Link](s1);
For(string s2:ss){
Count++;
[Link](s2);
}
[Link]( the num of words are +count);
}
}
===========================================

/* To find no. of lines and no. of words in the file *\


Import [Link];
Import [Link];
Import [Link];
Import [Link];
Import [Link];
public Class Sample{
Public static void main(String args[ ]) throws FileNotFoundException, IOException{

String s1=null;
Int lines=0;
Int words=0;
Pattern p = [Link](||s);
BufferedReader bf=new BufferedReader(new FileReader(sample text));
While((s1=[Link]())!=null){
String ss[ ]= [Link](s1);
For (String s3:ss0{
Words++;
[Link]( s3);
}
[Link](s1);
Lines++;
}
[Link]( the num of lines are +lines);
[Link]( the num of words are +words);
}
}
===================================================

/*prime number*\
Public Class PrimeNum{
Public static void main(String args[]){
Int num=10;
Int count=0;
For(int i-0;i<=10;i++){

For (int j=2;j<i;j++){


Int n=i%j;
If(n==0)break;
}
If(i==j){
[Link]( i);
Count++;
}
[Link]( the no. of prime numbers are +count);
}
}
===========================================
*
* *
* * *
Public Class Star{
Public static void main(String args[]){
Int num=10;
For(int i=1;i<num;i++0{
[Link]( * );
For(int j=0;j<i;j++){
[Link]( * );
}
}
}

}
==========================================

/* To find lines , letters , words , lines*\


Import [Link];
Import [Link];
Import [Link];
Import [Link];
Import [Link],Pattern;
Import [Link],Matches;
Public Class LinesWords{
Public static void main(String args[ ]) throws FileNotFoundException, IOException{
String s1=null;
Int lines=0;
Int words=0;
Pattern p = [Link](||s);
Pattern p1= Pattern .compile([a-z A-Z 0-9]);
BufferedReader bf=new bufferedReader(new FileReader(sample text);
While((s1=[Link]())!=null){
Matches m =[Link](s1);
While([Link]()){
Letters++;
}
String sarray[ ] = [Link](s1);
For(String s3:sarray){
Words++;

[Link]( s3);
}
Lines++;
}
[Link]( the num of lines are +words);
[Link]( the num of lines are +lines);
[Link]( the num of lines are +letters);
}
}
=============================================
*
**
***
****
*****

Public Class SampleStar{


Public static void main(String args[ ]){
Int num=10;
For(int i=0;i<num/10;i++){
[Link]( *);
For(int j=0;j<i;j++){
[Link]( * );
}
}

}
}
=============================================
*****
****
***
**
*

Public Class Sample{


Public static void main(String args[ ]){
Int num=5;
For(int i=1;i<=num;i++){
[Link]( * );
If(i==num){
[Link]( );
If(num==0)
Break;
I=0;
num--;
}
}
}
}
=========================================================

/*DB Connection*\
Import [Link];
Import [Link];
Import [Link];
Import [Link];
Public Class DBConn{
Public static void main(string args[ ])throws SQLException , ClassNotFoundException{
Class forName([Link]);
Connection connection =
[Link](jdbc:pracle:thin@localhost:1521:xe,system,system);
DatabaseMetaData dmd=[Link]();
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
==========================================

/* To get the colour of particular table*\


Import [Link];
Import [Link];
Import [Link];
Import [Link];
Import [Link];
Import [Link];
Public Class DBconn{

Public static void main(String args [ ] ) throws SQLException , ClassNotFoundException{


Class forName([Link]);
Connection con =
[Link](jdbc:pracle:thin@localhost:1521:xe,system,system);
Statement st=[Link]();
ResultSet rs=[Link](select * from emp);
ResultSetMetaData rsmd=[Link]();
For(int i=0;i<([Link]());++i){
[Link]([Link](i)+---+[Link](i));
}
}
=================================================

/*DB properties*\
Public Class PropertiesDBExample{
Public static void main(String args [ ] ) throws SQLException , FileNotFoundException ,
IOException{
FileInputStream fis=new FileInputStream([Link]);
Properties p=new Properties();
[Link](fis);
[Link]([Link](dcname));
Connection
con=[Link]([Link](dburl),[Link](dbname),[Link](dbpwd
));
Statement st=[Link]();
ResultSet rs=[Link]([Link]( );
While------------

---------}
}
=============================================

/*Fibonacci series*\
Public class Fibonacci{
Public static void main(String args [ ] ){
Int var1=1;int var2=1; int var3=1;
[Link](var1+ + var2+ );
For(int i=0;i<18;i++){
Var3=var2+var1;
[Link](var3+ );
Var1=var2;
Var2=var3;
[Link],pritnln()
}
}
=======================================================

/*String reverse without API*\


Public Class SampleString{
Public static void main(String args [ ] ){
[Link](enter a string);
Scanner sc=new Scanner([Link])
String s1=[Link]();
[Link](stringReverse(s1));

}
Public static String stringReverse(string masg){
Char[ ] ch=[Link]();
Stringbuffer sb=new StringBuffer();
For(int i=[Link];i<=[Link];i--){
If(i==0)
Break;
[Link](ch[i-1]);
}
Return [Link]();
}
}
=========================================
123
456
789
Public class SampleNumber{
Public static void main(String args [ ] ){
For(int i=1;i<10;i++){
[Link](i);
If(i%3==0)
[Link]();
}
}
=============================================

/*Factorial*\
Import [Link];
Class Factorial{
Public static void main(String args [ ]){
Int n , c ,fact=1;
[Link](enter the limit);
Scanner scanner=new Scanner([Link])
N=[Link]();
For(c=1;c<=n;c++){
Fact=fact*c;
}
[Link](the factorial is +fact);
}
}
==========================================

/*Matrix Program*\
Public class MatrixExample{
Public static void main(String args [ ] ){
Int array[ ] [ ] ={{1,2,3},{4,5,6}};
[Link](row size+[Link]);
[Link](columnsize+columnsize= +array[1].length
Output(array);
}
Public static void outputarray(int[ ] [ ] array){
Int rowsize=[Link]();

Int columnsize=array[0].length;
For(int i=0;i<=1;i++){
[Link]( [ );
For(int j=0;j<=2;j++){
[Link]( +array[i][j]);
}
[Link]( ] );
}
}
==========================================================

/*How many files in the Directory*\

Import [Link];
Public class FileDemo{
Static int count;
Public static void main(String args [ ] )throws Exception{
File file=new File=(file path/ex:e:\\ram\\mohan);
For(String var1:array){
Count++;
[Link](var1);
[Link]( the no. of files are +count);
}
}
===========================================================

/*In given two arrays 1,2,3,4,5 and 2,3,1,0,5 findwhich number is not present
in second array*\
Import [Link].*;
Public class ArrayEx{
Public static void main(String args [ ] ){
Integer[ ] arr1={1,2,3,4,5}
Integer [ ] arr2={2,3,1,0,5}
List l1=[Link](arr1);
List l2=[Link](arr2);
Iterator it=[Link]( );
While([Link]( )){
Integer i=(Integer)[Link]();
boolean b=[Link](i);
if(b==false){
[Link](i);
}
}
}
}
========================================================

/*In an array find how many numbers are duplicates*\


Import [Link].*;
Public Class SingleArrayDuplicate{
Static Set s = new HashSet( );
Public stati void main(String args [ ] ){

Integer [ ] i1 = { 10, 20 ,30,10,40,50,30,60,70};


For(int i=0;i<[Link];i++){
Int some=o;
For(int j=0;j<[Link];j++){
Boolean b=( i1[i]==i1[j]);
If(b){
Some++;
}
If(some>=2){
[Link](i1[i]);
}
}
}
}
}

==========================================================

You might also like