KnowledgeBoat Logo
|

Computer Applications

Consider the following statement:
"26 January is celebrated as the Republic Day of India"

Write a program (using scanner class) to change 26 to 15; January to August; Republic to Independence and finally print as:

"15 August is celebrated as the Independence Day of India"

Java

Input in Java

ICSE 2006

12 Likes

Answer

import java.util.Scanner;

public class KboatWordReplace
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a sentence ending with a space & terminating with (.):");
        while (true) {
            if (in.hasNextInt()) {
                int n = in.nextInt();
                if (n == 26)
                    System.out.print("15 ");
                else
                    System.out.print(n + " ");
            }
            String word = in.next();
            if (word.equals("."))
                break;
            else if (word.equals("January"))
                System.out.print("August ");
            else if (word.equals("Republic"))
                System.out.print("Independence ");
            else
                System.out.print(word + " ");
                
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Consider the following statement: "26 January is celebrated as the Republic Day of India" Write a program (using scanner class) to change 26 to 15; January to August; Republic to Independence and finally print as: "15 August is celebrated as the Independence Day of India"

Answered By

3 Likes


Related Questions