tags:

views:

114

answers:

5

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);

    }
}
+2  A: 

Since this is homework I'll help you out a little without giving the answer:

If you want the output to only be inbetween letters IE: A,B,C instead of A,B,C, which is what I imagine you are asking about. Then you need to look at your for loop and check the boundary conditions.

Woot4Moo
if i changed the loop to Str1.length()-1,,, it will give me A,B, excluding the C...
WM
What I mean is check the condition at which you append a comma
Woot4Moo
Try using an if statement inside your loop.
Amber
or just change your output when you get out of the loop (you know it always ends in a comma, so....)
Miles
A: 

Just don't append the comma when the last item of the loop is to be appended. You have the item index by i and the string length by Str2.length(). Just do the primary school math with a lesser-than or a greater-than operator in an if statement.

BalusC
A: 

Try regular expressions:

String input = scanner.next();
String output = input.replaceAll(".", "$0,");

With spaces it would be a bit easier since you don't need to abandon last 'odd' comma:

output = output.substring (0, ouput.length() - 2);
Roman
far too complex for a CS101 course problem.
Woot4Moo
maybe, but it's not wrong.
Roman
I didnt say it was, the down vote was because of an added layer of complexity that is not needed at this point in the learning process
Woot4Moo
pedants are so pedantic..
Roman
It's not pedantry if the answer is unnecessarily complex, more confusing and less useful to the asker.
Paul
I can only suppose that none of you both know this solution. Anyway, let it be here just for the sake of some variety in answers.
Roman
@Roman: FWIW, I also gave a regex-solution the first time OP asked this question http://stackoverflow.com/questions/2753313/write-a-program-that-allows-the-user-to-enter-a-string-and-then-prints-the-letter/2753331#2753331
polygenelubricants
A: 

When you've figured out the loop-solution, you could try the following ;)

System.out.println(Arrays.toString("HELLO".toCharArray()).replaceAll("[\\[ \\]]", ""));
aioobe
If you're doing `replaceAll`, might as well do it without the `Arrays.toString/toCharArray`: http://stackoverflow.com/questions/2753313/write-a-program-that-allows-the-user-to-enter-a-string-and-then-prints-the-letter/2753331#2753331
polygenelubricants
Nice! .........
aioobe
A: 

The following snippet should be instructive. It shows:

  • How to use StringBuilder for building strings
  • How to process each char in a String using an explicit index
    • How to detect if it's the first/last iteration for special processing

    String s = "HELLO";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
        char ch = s.charAt(i);
        if (i == 0) { // first
            sb.append("(" + ch + ")");
        } else if (i == s.length() - 1) { // last
            sb.append("<" + ch + ">");
        } else { // everything in between
            sb.append(Character.toLowerCase(ch));
        }
    }
    System.out.println(sb.toString());
    // prints "(H)ell<O>"
polygenelubricants