Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Practical Applications of String Methods | String
Java Basics

bookPractical Applications of String Methods

Now, let's explore a few practical applications of these methods through the following example tasks:

Task 1

Create a program to securely encode a message. Display the encrypted message on the console.

We will write our own encryption method that shifts each character in the original string 3 positions forward in the ASCII table to encrypt the message.

Main.java

Main.java

copy
12345678910111213141516
package com.example; public class Main { public static void main(String[] args) { String secretMessage = "hello world"; // Original message char[] codedMessage = secretMessage.toCharArray(); // Convert message to a char array // Loop through each character and shift by 3 positions in the ASCII table to encrypt for (int i = 0; i < codedMessage.length; i++) { // Encrypt character codedMessage[i] = (char) (codedMessage[i] + 3); } // Print the encrypted message System.out.println(new String(codedMessage)); } }

This code demonstrates a simple encryption method for a password or secret message. The string hello world is converted into a character array, and each character is shifted forward by 3 positions in the ASCII table using (char) (codedMessage[i] + 3). The encrypted message is then printed to the console.

Task 2

You upload a document title, and it needs to be converted to uppercase with underscores _ added between words to create a system-friendly filename.

Main.java

Main.java

copy
12345678910
package com.example; public class Main { public static void main(String[] args) { String documentTitle = "Quarterly financial report"; // Document title String systemFileName = documentTitle.toUpperCase().replace(" ", "_"); System.out.println(systemFileName); // Output } }

This code processes a document title to create a system-friendly filename. It uses toUpperCase() to convert all letters to uppercase and replace(" ", "_") to replace spaces with underscores, ensuring consistency and compatibility for file storage. The result is then displayed on the console.

1. What will be printed to the console?

2. What will be printed to the console?

3. What will be printed to the console?

question mark

What will be printed to the console?

Select the correct answer

question mark

What will be printed to the console?

Select the correct answer

question mark

What will be printed to the console?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me the code for the encryption method?

How can I decode the encrypted message back to its original form?

Can you explain why shifting ASCII values is considered a simple encryption?

bookPractical Applications of String Methods

Swipe to show menu

Now, let's explore a few practical applications of these methods through the following example tasks:

Task 1

Create a program to securely encode a message. Display the encrypted message on the console.

We will write our own encryption method that shifts each character in the original string 3 positions forward in the ASCII table to encrypt the message.

Main.java

Main.java

copy
12345678910111213141516
package com.example; public class Main { public static void main(String[] args) { String secretMessage = "hello world"; // Original message char[] codedMessage = secretMessage.toCharArray(); // Convert message to a char array // Loop through each character and shift by 3 positions in the ASCII table to encrypt for (int i = 0; i < codedMessage.length; i++) { // Encrypt character codedMessage[i] = (char) (codedMessage[i] + 3); } // Print the encrypted message System.out.println(new String(codedMessage)); } }

This code demonstrates a simple encryption method for a password or secret message. The string hello world is converted into a character array, and each character is shifted forward by 3 positions in the ASCII table using (char) (codedMessage[i] + 3). The encrypted message is then printed to the console.

Task 2

You upload a document title, and it needs to be converted to uppercase with underscores _ added between words to create a system-friendly filename.

Main.java

Main.java

copy
12345678910
package com.example; public class Main { public static void main(String[] args) { String documentTitle = "Quarterly financial report"; // Document title String systemFileName = documentTitle.toUpperCase().replace(" ", "_"); System.out.println(systemFileName); // Output } }

This code processes a document title to create a system-friendly filename. It uses toUpperCase() to convert all letters to uppercase and replace(" ", "_") to replace spaces with underscores, ensuring consistency and compatibility for file storage. The result is then displayed on the console.

1. What will be printed to the console?

2. What will be printed to the console?

3. What will be printed to the console?

question mark

What will be printed to the console?

Select the correct answer

question mark

What will be printed to the console?

Select the correct answer

question mark

What will be printed to the console?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3
some-alt