Java Codes Basic to Advance
2.03K subscribers
4 photos
2 videos
1 file
48 links
Download Telegram
//12 Syntax:user input
import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner Obj = new Scanner(System.in);

System.out.println("Enter name, age and salary:");
String name = Obj.nextLine();
int age = Obj.nextInt();
double salary = Obj.nextDouble();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
👍1
//13 Syntax:Exception handling
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Oh No...");
}
}
}
//13 Syntax:Exception handling 02
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Oh No...");
} finally {
System.out.println("I will run whatever happen.");
}
}
}
👍1
//13 Syntax:Exception handling 03
public class Main {
public static void main(String[ ] args) {
throw new ArithmeticException("Access denied ");
}
//No Output only a error Msg will be shown
}
Exception in thread "main" java.lang.ArithmeticException: Access denied at Main.main(Main.java:3)

Output of 13 Syntax:Exception handling 03
👍1
//14 Syntax:File Create / write
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class CreateFile {
public static void main(String[] args) {
try {
File Obj = new File("Give Location where to create File");
if (Obj.createNewFile()) {
System.out.println("File created: " + Obj.getName());
FileWriter MWriter = new FileWriter("give Location of file which to write");
MWriter.write("What am I writting");
MWriter.close();
System.out.println("Successfully wrote to the file.");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
//Note This code can only run in Your PC
1👍1
Output:
File created: test.txt
Successfully wrote to the file

Output of 14 Syntax:File Create / write
//14 Syntax:File Read / Info
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {
public static void main(String[] args) {
File Obj = new File("Location of text File");
if (Obj.exists()) {
System.out.println("File name: " + Obj.getName());
System.out.println("Absolute path: " + Obj.getAbsolutePath());
System.out.println("Writeable: " + Obj.canWrite());
System.out.println("Readable " + Obj.canRead());
System.out.println("File size in bytes " + Obj.length());
}
else {
System.out.println("The file does not exist.");
}
try {
File ObjR = new File("Location of text file");
Scanner MReader = new Scanner(ObjR);
while (MReader.hasNextLine()) {
String data = MReader.nextLine();
System.out.println(data);
}
MReader.close();
}catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
//Note This code can only run in Your PC
👍2
Output:
File name: test.txt
Absolute path: /home/runner/compilers/./test/test.txt
Writeable: true
Readable true
File size in bytes 18
What am I writting

Output of 14 Syntax:File Read / Info
//14 Syntax:File Delete and folder
import java.io.File;

public class DeleteFile {
public static void main(String[] args) {
try{
File DObj = new File("Location Of File");
if (DObj.delete()) {
System.out.println("Deleted the file: " + DObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
}catch(Exception e){
System.out.println("Failed to delete the folder.");
}
finally{
File DFObj = new File("Location of Folder");
if (DFObj.delete()) {
System.out.println("Deleted the folder: " + DFObj.getName());
} else {
System.out.println("Failed to delete the folder.");
}
}
}
}
//Note This code can only run in Your PC
👍4
Output:
Deleted the file: test.txt
Failed to delete the folder.

Output of 14 Syntax:File Delete and folder
Remaining Topics like Inheritance, Polymorphism, Hash Map, Wrapper Classes, Regex, Threads, Lamda will be added soon
👍1
Java all Codes listed see pin message 👆

Or you can search any keyword

Example: "function" to see function related codes
#Taskforyou

Write a Java program that remove the extra spaces from the following string:

' I am learning Java object oriented programming '
👍1
Write code on task's comment, which code will be best that code should be posted here with those name

After 8pm today
👍1
#TaskAnswer

public class TaskAnswer {
public static void main(String[] args) {
String str = "I am learning Java object oriented programming";

str = str.replaceAll("\\s", "");
System.out.println(str);
}
}

Output:
Iamlearningobjectorientedprogramming
👍2
Explanation:

str = str.replaceAll("\\s", "") : This line replaces all whitespace characters in the string str with an empty string, effectively removing them. The \\s is a regular expression pattern that matches any whitespace character, such as spaces, tabs, or newlines. The double backslash \\ is necessary because \ is an escape character in Java strings, so to represent a single backslash in a regular expression, you need to escape it with another backslash.
Write your Code in Comment below 👇👇
👍2
Java Codes Basic to Advance
Write your Code in Comment below 👇👇
Solution:

import java.util.Scanner;
public class Pattern {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the value of n: ");
int n = scan.nextInt();
for (int i = 1 ; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print("1 ");
}
for(int k = 1; k <= i; k++){
System.out.print(i + " ");
}
System.out.println();
}
}
}
👍7