Home / Programs / Example 2: String Array: Write a Java program to update array elements at three different positions: start, middle, and end. Provide 3 different examples using user input for string arrays.
🚀 Programming Example

Example 2: String Array: Write a Java program to update array elements at three different positions: start, middle, and end. Provide 3 different examples using user input for string arrays.

👁 25 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 Information & Algorithm

Example 2: String Array

💻 Program Code

import java.util.Scanner;

public class UpdateStringArrayUser {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] arr = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};

        System.out.println("Original Array:");
        for(String fruit : arr){
            System.out.print(fruit + " ");
        }

        // User input to update start, middle, and end
        System.out.print("\nEnter new value for start: ");
        arr[0] = sc.nextLine();

        System.out.print("Enter new value for middle: ");
        arr[arr.length / 2] = sc.nextLine();

        System.out.print("Enter new value for end: ");
        arr[arr.length - 1] = sc.nextLine();

        System.out.println("Updated Array:");
        for(String fruit : arr){
            System.out.print(fruit + " ");
        }
    }
}

                        
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.