I need help showing me where I went wrong.

Given problem is. Print to System.out a new string composed of every alternate character in sentence starting with the first. Use a loop to go through a string and retrieve characters from alternate index positions. Append these characters to another string and print. Output should equal "pes ep"

The code I have so far is... I am confident that the System.out parameters are wrong, but I might also have something else wrong. I could use help fixing.

String sentence = "please help"
int num1="0"

sentence.charAt(num1);
for (num1 = 0; num1 <= sentence.length(); num1 += 2) {
System.out.println(num1);

run:
0
2
4
6
8
10
12
14
16
18

Thank you,

Recommended Answers

All 4 Replies

public class Test1 {

    public static void main(String args[])
    {
    System.out.println("hello");
    String sentence = "please help";
    for (int i=0; i<sentence.length();i++)
    {
        System.out.print(sentence.charAt(i));
        i++;
    }
    }

}

why r u printing num???
import java.util.*;
class J
{
public static void main(String args[])
{
String sentence = "please help";
int num1=0;
for (num1 = 0; num1 <= sentence.length(); num1 += 2)
System.out.print(sentence.charAt(num1));
}
}

You did answer it correctly, I posed the question again because I wanted to see the difference of using a string vs. an array to solve. I was trying to get the answer without using char[] charSentence = sentence.toCharArray();

Thank you again for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.