Saturday, 24 January 2015

Check ia string is hexadecimal number or not


Check if a String is Hexadecimal number or not in java


  public boolean isHexNumber(String){
       boolean flag;
       try {
           int t = Integer.parseInt(value, 16);
            flag = true;
       } catch (NumberFormatException e) {
            flag = false;
       }
       return flag;
}
   



 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).

Friday, 23 January 2015

Read File And Count Occurrence Of Word In Java

Read a file and count the occurrence of word in java

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.TreeMap;
import java.util.StringTokenizer;

public class fileRead {

    public static void main(String [] args){

        try{
           
            String textFile ="D:\\workspace\\datastructure\\src\\javaprogram\\test.txt";
            BufferedReader input = new BufferedReader(new FileReader(textFile));
           
            //Creating the Map to store the words and their occurrences
            TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>();
            String currentLine = null;
           
            //Reading line by line from the text file
            while((currentLine = input.readLine()) != null){
               
                //Parsing the words from each line
                StringTokenizer parser = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'\"");
                while(parser.hasMoreTokens()){
                    String currentWord = parser.nextToken();
                   
                    Integer frequency = frequencyMap.get(currentWord);
                    if(frequency == null){
                        frequency = 0;                       
                    }
                    //Putting each word and its occurrence into Map
                    frequencyMap.put(currentWord, frequency + 1);
                }
               
            }
           
            //Displaying the Result
            System.out.println(frequencyMap);
                  
        }catch(IOException ie){
            ie.printStackTrace();
            System.err.println("Your entered path is wrong");
        }       
       
    }
   
}

Wednesday, 21 January 2015

Tic Tac Toe game



Tic Tac Toe Game In Java


Tic Tac Toe game program in java

TicTacToe.java


public class TicTacTOe {
  int _row=3;
char _board[][]=new char[_row][_row];
public void makeBoard(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j < _row; j++) {
_board[i][j]='o';
}

}
}
boolean isValid(int r,int c){
return((r>=0&&r<_row)&&(c>=0&&c<_row));
}
boolean isFill(int r,int c){
return(_board[r][c]!='o');
}
public boolean gamePlay(int r ,int c,int playerNo){
if (!(isValid(r,c))) {
System.out.println("Enter Correct index");
return true ;
}
if (isFill(r,c)) {
System.out.println("This index already fill");
return true;
}
if (playerNo==1) {
_board[r][c]='*';
} else {
        _board[r][c]='+';
}
return false;
}

boolean horizontalAndVerticalMatch(char ch,int n){
// for row and col math
    boolean flag=true;
for (int i = 0; i < _row; i++) {
   flag=true;
  for (int j = 0; j < _row; j++) {
  System.out.println("i="+i+"j="+j);
  char ch1=(n==1)?_board[i][j]:_board[j][i];
         if (ch1!=ch) {
         System.out.println("y");
flag=false;
break;
}
 }
  if (flag) {
  return flag;
}
}
return flag;
}
boolean majorDiagonal(char ch){
return((_board[0][0]==ch)&&(_board[1][1]==ch)&&(_board[2][2]==ch));
}
boolean minorDiagonal(char ch){
return((_board[2][0]==ch)&&(_board[1][1]==ch)&&(_board[0][2]==ch));
}
public boolean isGameOver(int PlayerNo){
char ch;
if (PlayerNo==1) {
ch='*';
} else {
ch='+';
}
return(horizontalAndVerticalMatch(ch,1)||horizontalAndVerticalMatch(ch,2)||majorDiagonal(ch)||minorDiagonal(ch));
}
public void display(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j < _row; j++) {
System.out.print("     "+_board[i][j]);
}
System.out.println("");
}
}

}

MainTicTacToe.java

import java.util.Scanner;

public class MainTicTacToe extends TicTacTOe {
 public static void main(String[] args) {
TicTacTOe obj=new TicTacTOe();
obj.makeBoard();
obj.display();
int r,c;
Scanner input=new Scanner(System.in);
while(true){
boolean flag=true;
while(flag){
System.out.println("Player 1");
System.out.println("enter row");
r=input.nextInt();
System.out.println("enter col");
c=input.nextInt();
flag=obj.gamePlay(r,c,1);
}
if (obj.isGameOver(1)) {
obj.display();
System.out.println("Player 1 Win Game");
break;
}
System.out.println("============");
obj.display();
flag=true;
while(flag){
System.out.println("Player 2");
System.out.println("enter row");
r=input.nextInt();
System.out.println("enter col");
c=input.nextInt();
flag=obj.gamePlay(r,c,2);
}
if (obj.isGameOver(2)) {
obj.display();
System.out.println("Player 2 Win Game");
break;
}
System.out.println("============");
obj.display();
}
}
}






Minesweeper game in java


minesweeper Program in java

Minesweeper.java

import java.util.Random;
public class Minesweeper {
  int _row=5,_col=5,_countGameOver=0;
  int [][] _board=new int[_row][_col];
  boolean [][] _flagMatrix=new boolean[_row][_col];
 public void plotBoard(){
 int noOfBomb=_col;
while(noOfBomb>0) {
 int i=getRandomNumber();
 int j=getRandomNumber();
 if (_board[i][j]!=-1) {
_board[i][j]=-1;
noOfBomb--;
  }
 }
  }
public int getRandomNumber(){
Random t = new Random();
// random integers in [0, 100]
int num;
{
num=t.nextInt(_col);
}while(num>_col);
return num;
    }
boolean isValid(int i,int j){
return((i>=0 && i<_row)&&(j>=0 && j<_col));
}
int getAdjBomb(int i,int j){
int num=0;
//System.out.println("i="+i+"j="+j);
for (int k = i-1; k <=(i+1); k++) {
for (int l= j-1; l<=(j+1); l++) {
    if (isValid(k,l)&&(_board[k][l]==-1)) {
num++;
   }
    }
}
return num;
}
public void calculateCounts(){
for (int i = 0; i <_row; i++) {
    for (int j = 0; j <_col; j++) {
    if(_board[i][j]!=-1)
       _board[i][j]=getAdjBomb(i,j);
    }
}
}
public void print(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j <_col; j++) {
System.out.print(" "+_board[i][j]);
}
System.out.println("");
}
}
public void displayFlagMatrix(){
for (int i = 0; i <_row; i++) {
   for (int j = 0; j <_col; j++) {
    if (_flagMatrix[i][j]==true) {
    System.out.print(" "+"*");
}
    else{
    System.out.print(" "+"+");
    }
   }
System.out.println(" ");
}
}
public boolean play(int r,int c){
if (!(isValid(r,c))) {
System.out.println("Please Enter row and col in correct range");
return false;
}
if (_flagMatrix[r][c]==true) {
System.out.println("This is a open position please enter other col and row");
return false;
}
if (_board[r][c]==-1)
return true;
if (_board[r][c]!=0) {
_countGameOver++;
_flagMatrix[r][c]=true;
}
else{
_flagMatrix[r][c]=true;
_countGameOver++;
openSpace(r, c);
}
return false;
}
void openSpace(int r,int c){
for (int k = r-1; k <=(r+1); k++) {
   for (int l= c-1; l<=(c+1); l++) {
            if (isValid(k,l)&&(_board[k][l]!=-1)&&(_flagMatrix[k][l]==false)) {
             _flagMatrix[k][l]=true;
              _countGameOver++;
          if (_board[k][l]==0) {
             openSpace(k,l);
          }
         }  
    }
}
}
public boolean checkGameOver(){
return(_countGameOver==(_row*_row)-(_row));
}
}

++++++++++++++++++

MainMinessweeper.java


import java.util.Scanner;
import com.jan21.*;
public class MainMinesweeper extends Minesweeper {
public static void main(String[] args) {
MainMinesweeper obj=new MainMinesweeper();
    obj.plotBoard();
    obj.calculateCounts();
    System.out.println("afate count");
    obj.print();
    int r,c;
    System.out.println("+++++++++++++++");
    obj.displayFlagMatrix();
    while(true){
  System.out.println("===============");
    Scanner input=new Scanner(System.in);
    System.out.println("enter row");
    r=input.nextInt();
    System.out.println("enter col");
    c=input.nextInt();
    if (obj.play(r,c)) {
System.out.println("You Lose");
break;
  }
System.out.println("ok");
if (obj.checkGameOver()) {
obj.displayFlagMatrix();
System.out.println("You Win Game");
break;
}
obj.displayFlagMatrix();
System.out.println("+++++++++++");
obj.print();

  }
}

}



Top array Programs In Java

arrays program in java with solution

 expand a given array (Solution)

Java Tutorial

 

Java Tutorial


OOP Concept:




      Programs :



Java Collections Framework Tutorials For Beginners:





Data Structure in Java



Top String Programs In Java

Top String Program In Java


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).



Other Useful Posts:


  Top 50 core java coding interview question and answer

  Guava Tutorial