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

---