Object-Oriented Programming with Java, part I – Exercise 51

This program takes a String input by the user and then prints it out character by character.

My Code

import java.util.Scanner;

public class SeparatingCharacters {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.print("Type your name: ");
        String name = reader.nextLine();
        int i = 0;

        while (i < name.length()) {
            System.out.println((i + 1) + ". character: " + name.charAt(i));
            i++;
        }
    }
}

Model Code

import java.util.Scanner;

 

public class SeparatingCharacters {

 

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        System.out.print("Type your name: ");

        String name = reader.nextLine();

        

        int i = 0;

        while (i < name.length()) {

            System.out.println((i + 1) + ". character: " + name.charAt(i));

            i++;

        }

    }

}

Comments

My code is exactly the same as the model code.

Leave a Reply

Your email address will not be published. Required fields are marked *