Using charAt in java

Posted by ThuL?nå F?rñ?nDö on Stack Overflow See other posts from Stack Overflow or by ThuL?nå F?rñ?nDö
Published on 2012-10-16T15:48:05Z Indexed on 2012/10/16 23:01 UTC
Read the original article Hit count: 222

Filed under:
|

This is my assignment:

Write a program where the user enters a string, and the program echoes it to the monitor with one character per line:

C:\>java LinePerChar
Enter a string:
Octopus

O
c
t
o
p
u
s

I have tried, but I'm getting some compilation errors. Here's my code:

import java.util.*;

class CharactorEcho{
    public static void main(String args []){

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a string :");

        try {
            String inputString = sc.nextLine();
            for(int i=0; i < sc.length(); i++) {
                char c = inputString.charAt(i);
                System.out.println("" + c);
            }
        } catch(IOException e) {

        }
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about compiler-errors