Write a program that allows the user to enter a string and then prints the letters of the String sep

Posted by user330776 on Stack Overflow See other posts from Stack Overflow or by user330776
Published on 2010-05-02T11:33:57Z Indexed on 2010/05/02 11:37 UTC
Read the original article Hit count: 309

Filed under:
|

Sample Run:

  • Enter a string: Rabbit
  • Output: R,a,b,b,i,t

© Stack Overflow or respective owner

Write a program that allows the user to enter a string and then prints the letters of the String sep

Posted by WM on Stack Overflow See other posts from Stack Overflow or by WM
Published on 2010-05-02T21:12:28Z Indexed on 2010/05/02 21:17 UTC
Read the original article Hit count: 309

Filed under:
|

The output is always a String, for example H,E,L,L,O,. How could I limit the commas? I want the commas only between letters, for example H,E,L,L,O.

import java.util.Scanner;

import java.lang.String;

public class forLoop 

{
    public static void main(String[] args)
    {
        Scanner Scan = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String Str1 = Scan.next();

       String newString="";
       String Str2 ="";
        for (int i=0; i < Str1.length(); i++)
        {
                newString = Str1.charAt(i) + ",";

                Str2 = Str2 + newString;  

        }
       System.out.print(Str2);

    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about homework