Java for beginners
406 subscribers
49 photos
29 links
Learn Java daily with bite-sized lessons, practical code examples, output previews, and fun quizzes.
Download Telegram
🌟 **Day 9: String Manipulation in Java!** 🌟

Hello, future Java pros! πŸ‘‹ Today, we're diving into one of the most fundamental and frequently used data types: **Strings**! ✨ From names and messages to URLs and file paths, strings are everywhere. Java's `String` class is incredibly powerful, offering a plethora of methods to manipulate text with ease.

Let's explore some essential string manipulation techniques that will make your code robust and dynamic. Get ready to slice, dice, and transform your text! πŸš€

```java
public class StringManipulationDemo {
public static void main(String[] args) {
String originalText = " Hello Java Learners! Welcome to Day 9. ";

System.out.println("Original String: '" + originalText + "'");
System.out.println("Length of original: " + originalText.length());

// 1. Trimming whitespace
String trimmedText = originalText.trim();
System.out.println("Trimmed String: '" + trimmedText + "'");
System.out.println("Length after trim: " + trimmedText.length());

// 2. Getting a substring
// Substring from index 6 (inclusive) to 10 (exclusive)
System.out.println("Substring (index 6 to 10): " + trimmedText.substring(6, 10)); // "Java"

// 3. Finding the index of a character or substring
System.out.println("Index of 'Java': " + trimmedText.indexOf("Java")); // 6
System.out.println("Index of first 'e': " + trimmedText.indexOf('e')); // 1

// 4. Replacing parts of a string
String replacedText = trimmedText.replace("Java", "Awesome");
System.out.println("Text after replacing 'Java': " + replacedText);

// 5. Changing case
System.out.println("Uppercase: " + trimmedText.toUpperCase());
System.out.println("Lowercase: " + trimmedText.toLowerCase());

// 6. Checking if a string contains another string
System.out.println("Contains 'Learners'?: " + trimmedText.contains("Learners")); // true

// 7. Concatenating strings
String newGreeting = "Happy coding!";
System.out.println("Concatenated: " + trimmedText + " " + newGreeting);
}
}
```

Output example:
```
Original String: ' Hello Java Learners! Welcome to Day 9. '
Length of original: 44
Trimmed String: 'Hello Java Learners! Welcome to Day 9.'
Length after trim: 38
Substring (index 6 to 10): Java
Index of 'Java': 6
Index of first 'e': 1
Text after replacing 'Java': Hello Awesome Learners! Welcome to Day 9.
Uppercase: HELLO JAVA LEARNERS! WELCOME TO DAY 9.
Lowercase: hello java learners! welcome to day 9.
Contains 'Learners'?: true
Concatenated: Hello Java Learners! Welcome to Day 9. Happy coding!
```

**Small Challenge:** πŸ’ͺ
Take the string `"Coding is Fun and Powerful!"`. Write Java code to:
1. Extract the word "Fun".
2. Replace "Powerful" with "Creative".
3. Check if the modified string ends with "!".

Share your solutions or questions below! Happy learning! #Java #JavaLearning #StringManipulation #Programming #Day9

---
Here's your Telegram post for Day 17!

---

πŸŽ‰ Day 17 is here, and we're tackling a crucial skill for modern Java development: **JSON Data Handling!** πŸš€

JSON (JavaScript Object Notation) is everywhere – from web APIs to configuration files. Today, you'll learn how to seamlessly convert Java objects into JSON strings (serialization) and vice-versa (deserialization) using powerful libraries like **Jackson** or **Gson**. It's easier than you think and opens up a world of possibilities for your applications!

Let's dive into an example using Jackson. πŸ› οΈ

```java
// Product.java
// This is our simple Java POJO (Plain Old Java Object)
public class Product {
private int id;
private String name;
private double price;
private boolean available;

// Default constructor for deserialization
public Product() {}

public Product(int id, String name, double price, boolean available) {
this.id = id;
this.name = name;
this.price = price;
this.available = available;
}

// Getters and Setters (required by Jackson for serialization/deserialization)
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public boolean isAvailable() { return available; }
public void setAvailable(boolean available) { this.available = available; }

@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + ", available=" + available + "]";
}
}

// MainJsonHandler.java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class MainJsonHandler {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();

try {
// --- 1. Serialize Java Object to JSON String ---
Product laptop = new Product(101, "SuperLaptop", 1200.00, true);
String jsonOutput = objectMapper.writeValueAsString(laptop);
System.out.println("Serialized Product to JSON:n" + jsonOutput);

// --- 2. Deserialize JSON String to Java Object ---
String jsonInput = "{"id":202,"name":"GamingMouse","price":75.50,"available":false}";
Product mouse = objectMapper.readValue(jsonInput, Product.class);
System.out.println("nDeserialized JSON to Product:n" + mouse);

} catch (IOException e) {
e.printStackTrace();
}
}
}
```

**Note:** To run this code, you'll need the Jackson dependency. For Maven, add this to your `pom.xml`:
```xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version> <!-- Use the latest version -->
</dependency>
```

Output example:
```
Serialized Product to JSON:
{"id":101,"name":"SuperLaptop","price":1200.0,"available":true}

Deserialized JSON to Product:
Product [id=202, name=GamingMouse, price=75.5, available=false]
```

### Your Challenge! πŸš€
Modify the `MainJsonHandler` to:
1. Create a `List<Product>` with a few different product objects.
2. Serialize this `List<Product>` into a JSON array string.
3. Deserialize that JSON array string back into another `List<Product>`.
Can you do it? Show us your solution! πŸ‘‡

Happy coding, and see you tomorrow! #JavaLearning #JSON #Jackson #Serialization #Programming

---
Here's your Telegram post for Day 30!

---

πŸŽ‰ **Congratulations on Reaching Day 30!** πŸŽ‰

You've made incredible progress in just thirty days, diving into Java fundamentals! πŸš€ Now it's time to consolidate all your learning with a final project. How about building a simple *Command-Line To-Do List Manager*? πŸ“ It's a fantastic way to apply your understanding of classes, objects, methods, `ArrayList`s, and user input. Let's review some core concepts and get you thinking!

Here’s a quick look at a basic `Task` class and how you might manage a list of them:

```java
import java.util.ArrayList;
import java.util.List;

// Represents a single To-Do item
class Task {
private String description;
private boolean isCompleted;

public Task(String description) {
this.description = description;
this.isCompleted = false; // By default, a new task is not completed
}

public String getDescription() {
return description;
}

public boolean isCompleted() {
return isCompleted;
}

public void markCompleted() {
this.isCompleted = true;
}

@Override
public String toString() {
return "[" + (isCompleted ? "βœ…" : "⏳") + "] " + description;
}
}

// Main class to demonstrate To-Do list management
public class Day30Review {
public static void main(String[] args) {
List<Task> myTasks = new ArrayList<>(); // Use ArrayList for dynamic size

System.out.println("--- Initial To-Do List ---");
myTasks.add(new Task("Learn Java basics"));
myTasks.add(new Task("Start final project idea"));
myTasks.add(new Task("Review OOP concepts"));

for (Task task : myTasks) {
System.out.println(task);
}

System.out.println("n--- Updating Task Status ---");
// Mark the first task as completed
if (!myTasks.isEmpty()) {
myTasks.get(0).markCompleted();
System.out.println("Marked '" + myTasks.get(0).getDescription() + "' as completed.");
}

System.out.println("n--- Updated To-Do List ---");
for (Task task : myTasks) {
System.out.println(task);
}
}
}
```

Output example:
```
--- Initial To-Do List ---
[⏳] Learn Java basics
[⏳] Start final project idea
[⏳] Review OOP concepts

--- Updating Task Status ---
Marked 'Learn Java basics' as completed.

--- Updated To-Do List ---
[βœ…] Learn Java basics
[⏳] Start final project idea
[⏳] Review OOP concepts
```

### Small Challenge:
Your challenge for your To-Do List Manager:
1. **Add User Input:** Implement `Scanner` to allow users to add new tasks.
2. **Remove Tasks:** Create a method that allows users to remove tasks by their index in the list.
3. **List by Status:** Add functionality to display *only* completed tasks or *only* pending tasks.

Ready to build something awesome? You've got this! πŸ’ͺ Don't forget to share your progress! #JavaLearning #Day30 #FinalProject #CodingChallenge

---