Java Codes Basic to Advance
2.03K subscribers
4 photos
2 videos
1 file
48 links
Download Telegram
import java.io.*;
import java.net.*;

public class Client {
Socket socket;
BufferedReader br;
PrintWriter out;

public Client() {

try {
System.out.println("Connecting to the server.");
socket = new Socket("127.0.0.1", 7777);
System.out.println("Connection successfull..");

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());

startReading();
startWritting();

} catch (Exception e) {
System.out.println("Error: Firstly run Server Code");
}
}

public void startReading() {
// thread reading
Runnable r1 = () -> {
System.out.println("Reader started..");
try {
while (true) {

String msg = br.readLine();
if (msg.equals("exit")) {
System.out.println("Server terminated the chat, Please press enter to free port");
socket.close();
break;
}
System.out.println("Server: " + msg);
}
} catch (Exception e) {
System.out.println("Connection Closed");
}
};
new Thread(r1).start();
}

public void startWritting() {
// thread sending data
Runnable r2 = () -> {
System.out.println("Writter started..");
String content = "";
try {
while (!socket.isClosed()) {

BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));

content = br1.readLine();

out.println(content);
out.flush();

if (content.equals("exit")) {
System.out.println("You terminated the chat");
socket.close();
break;
}
}
} catch (Exception e) {
if (content.equals("exit")) {
System.out.println("Connection Closed successfully");
}
else{
System.out.println("Connection terminated unsuccssefully \nTermnate all existing cmd's or check other errors");
}
}
};
new Thread(r2).start();
}

public static void main(String[] args) {
System.out.println("Going to start Client..");
new Client();
}
} // Client Code : @Java_Codes_Pro
👍5
Java Chat Code(2 Program sent Copy both (Server Code and Client Code) run And Practice with them)
// Factotial from recursion in java

import java.util.Scanner;

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

Scanner s = new Scanner(System.in);
System.out.print("Enter Number: " );
int n = s.nextInt();
if(n>26)
System.out.print("Please enter number less than 25\n" );
else{

System.out.println("Showing factorial for "+ n + " Number" );
System.out.print("\n" +Main.f(n));
}
}

// Factorial value calculation + series printing
static long f(long n){
if (n==1){
System.out.print(n);
return n;
}
else{
System.out.print(n + " * ");
return n*f(n-1);
}
}

} // @Java_Codes_Pro
// Factotial from loop in java
// see recursive approach in upper code
import java.util.Scanner;

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

Scanner s = new Scanner(System.in);
System.out.print("Enter Number: " );
int n = s.nextInt();
if(n>26)
System.out.print("Please enter number less than 25\n" );
else{

System.out.println("Showing factorial for "+ n + " Number" );
System.out.print("\n" +Main.f(n));
}
}

// Factorial value calculation + series printing
static long f(int n){
long h = 1;
for(int i = n; 0 < i; i-- ){
if(i == 1){
System.out.print(i);
continue;
}
System.out.print(i +" * " );
h = i*h;
}
return h;
}
}
// Join now @Java_Codes_Pro
👍4
Learn how to create Telegram Bot
Also clear Javascript basics
(Regular video Uploading...)

https://youtube.com/playlist?list=PLjEYzWkdEvxvar65MToPfkA1x4CeZ_bNk
👍3
// java basic threads example

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

        // Creating and starting two threads
        Thread thread1 = new MyThread("Thread 1");
        Thread thread2 = new MyThread("Thread 2");
        thread1.start();
        thread2.start();
    }
}

class MyThread extends Thread {
    private String threadName;

    public MyThread(String name) {
        threadName = name;
    }

    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println(threadName + " - Count: " + i);
            try {
                Thread.sleep(1000); // Pause for 1 second
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
} // @Java_Codes_Pro
👍4🤔1
// #1 Your First Program

class HelloWorld {

public static void main(String[] args){
    System.out.println("Hello, World!");
     }

}
// #2 For Adding 2 numbers

public class MyClass {

public static void main(String args[]) {
int x=10;
int y=25;
int z=x+y;
System.out.println("Sum of x+y = " + z);
}

}
👍3
// Airthmetic Operations in Java

public class MyClass {
  public static void main(String args[]) {
    
   // declaring variables
     int add, sub, div, mul, r;
    
     // Declaring two varables
     int x=4;
     int y=2;
    
    // performing Operations
     add = x+y;    // Addition
     sub = x-y;     // Subtraction
     div = x/y;      // Division
     mul = x+y;     // Multiply
     r= x%y;     // re m inder
    
   // Showing results
     System.out.println("4 + 2 = "+ add);
     System.out.println("4 - 2 = "+ sub);
     System.out.println("4 * 2 = "+ mul);
     System.out.println("4 / 2 = "+ div);
     System.out.println("4 % 2 = "+ r);
  }
}

// in this i introduced operators in java
// code by @Java_Codes_Pro
// buttons by @IOChannel_Bot
// Compiler @Compiler0bot
👍21
Are You tried these patterns ?
public class NumberTrianglePattern {
public static void main(String[] args) {
int rows = 5;

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
} // @Java_Codes_Pro
👍1
public class StarPyramidPattern {
public static void main(String[] args) {
int rows = 5;

for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
// @Java_Codes_Pro
👍2
public class DiamondPattern {
public static void main(String[] args) {
int rows = 5;

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}

for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
// @Java_Codes_pro
👍1
All Codes
@C_Code5
@CPP_Coding
@Python_Codes_Pro
@Java_Codes_Pro
@jsCode0

Diffrent free Compiler bots
For group @CodeCompiler_Bot
For channel @IOChannel_bot
For Channel Cmpl @Compiler0bot
For inline @cmpbbot

info in @LogicBots

Discussion
@bca_mca_btech
// java string functions part 1

public class StringFunctionsProgram1 {
public static void main(String[] args) {
// STRING FUNCTIONS - PROGRAM 1

// 1. Length of the String
String str = "Hello, World!";
int length = str.length();
System.out.println("Length of the String: " + length);

// 2. Concatenation of Strings
String newStr = str.concat(" Welcome");
System.out.println("Concatenated String: " + newStr);

// 3. Extracting a Substring
String subStr = str.substring(7);
System.out.println("Substring: " + subStr);

// 4. Converting to Uppercase
String upperStr = str.toUpperCase();
System.out.println("Uppercase String: " + upperStr);

// 5. Converting to Lowercase
String lowerStr = str.toLowerCase();
System.out.println("Lowercase String: " + lowerStr);

// 6. Checking if a String starts with a specific prefix
boolean startsWith = str.startsWith("Hello");
System.out.println("Starts with 'Hello': " + startsWith);

// 7. Checking if a String ends with a specific suffix
boolean endsWith = str.endsWith("!");
System.out.println("Ends with '!': " + endsWith);

// 8. Finding the index of a specific character
int index = str.indexOf("o");
System.out.println("Index of 'o': " + index);

// 9. Replacing characters in a String
String replacedStr = str.replace("o", "x");
System.out.println("Replaced String: " + replacedStr);

// 10. Checking if a String contains a specific sequence of characters
boolean contains = str.contains("World");
System.out.println("Contains 'World': " + contains);
}
}
// java string functions part 2

public class StringFunctionsProgram2 {
public static void main(String[] args) {
// STRING FUNCTIONS - PROGRAM 2

// 1. Trimming leading and trailing whitespace
String str1 = " Hello, World! ";
String trimmedStr = str1.trim();
System.out.println("Trimmed String: " + trimmedStr);

// 2. Splitting a String into an array of substrings
String[] splitStr = str1.split(",");
System.out.println("Split Strings:");
for (String split : splitStr) {
System.out.println(split.trim());
}

// 3. Checking if two Strings are equal
String str2 = "Hello";
boolean isEqual = str1.equals(str2);
System.out.println("Strings are equal: " + isEqual);

// 4. Getting the character at a specific index
char charAt = str2.charAt(1);
System.out.println("Character at index 1: " + charAt);

// 5. Checking if a String is empty
boolean isEmpty = str2.isEmpty();
System.out.println("Is empty: " + isEmpty);

// 6. Removing leading and trailing characters
String removedStr = str1.strip();
System.out.println("Removed String: " + removedStr);

// 7. Reversing a String
StringBuilder reversedStr = new StringBuilder(str1).reverse();
System.out.println("Reversed String: " + reversedStr);

// 8. Checking if a String matches a regular expression
boolean matchesRegex = str1.matches("Hello.*");
System.out.println("Matches regular expression 'Hello.*': " + matchesRegex);

// 9. Formatting a String
String formattedStr = String.format("Price: $%.2f", 10.5);
System.out.println("Formatted String: " + formattedStr);

// 10. Comparing two Strings lexicographically
int comparison = str1.compareTo("Goodbye");
System.out.println("Comparison result: " + comparison);
}
}

// See t.me/Java_Codes_Pro/52
👍3
Learnt these functions ?
// java basic threads example

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

        // Creating and starting two threads
        Thread thread1 = new MyThread("Thread 1");
        Thread thread2 = new MyThread("Thread 2");
        thread1.start();
        thread2.start();
    }
}

class MyThread extends Thread {
    private String threadName;

    public MyThread(String name) {
        threadName = name;
    }

    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println(threadName + " - Count: " + i);
            try {
                Thread.sleep(1000); // Pause for 1 second
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
} // @Java_Codes_Pro
👍2
// Arrays in Java

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

int arr[] = {2, 3, 6};
//Array declaration and initialization

System.out.println(arr[0]);
//Showing Array First element

}
} // @Java_Codes_pro