Concatenate a substring of the original string sentence starting from index 0 and ending before the middle Character(s). Use the substring() method. Use an if statement to account for even or odd, the end index that you need substring()the end index that you need for substring() will be different

public static void main(String[] args) {        

        String sentence= "Need Help"
        String midChars = "";

        int sLength = 0;
        int midIndex= 0;

        char ch1
        char ch2


        sLength = sentence.length();
        if (sLength % 2 != 0) {
        midIndex = sLength/2;
        ch1 = sentence.charAt(midIndex);
        midChars = String.valueOf(ch1);
        } else {
        ch1 = sentence.charAt(midIndex);
        ch2 = sentence.charAt(midIndex-1);
        midChars = midChars + String.valueOf(sentence.charAt(midIndex - 1))
        + String.valueOf(sentence.charAt(midIndex));
        }
        }
        System.out.println(????);

        // call method called testFirstLast and send String sentence to it as an input
        testFirstLast(sentence);

Recommended Answers

All 9 Replies

Please use the CODE button to provide indentation, code coloring and line numbers. All worth doing.

What is your question?

Thank you for the feedback, my question is...
How do I concatenate a substring of the original string sentence starting from index 0 and ending before the middle Character(s). I need to use the substring() method and an if statement to account for even or odd, the end index I need substring()the end index that you need for substring() will be different

public static void main(String[] args) {

String sentence= "Need Help"
String midChars = "";

int sLength = 0;
int midIndex= 0;

char ch1
char ch2


sLength = sentence.length();
if (sLength % 2 != 0) {
midIndex = sLength/2;
ch1 = sentence.charAt(midIndex);
midChars = String.valueOf(ch1);
} else {
ch1 = sentence.charAt(midIndex);
ch2 = sentence.charAt(midIndex-1);
midChars = midChars + String.valueOf(sentence.charAt(midIndex - 1))
+ String.valueOf(sentence.charAt(midIndex));
}
}
System.out.println(????);

// call method called testFirstLast and send String sentence to it as an input
testFirstLast(sentence);

No, that's your assignment. What's your question?

No, that's your assignment. What's your question?

I am not sure how to get the correct output. I am not sure what to put for method and the proper place to put the method and the proper System.out statement.

The assignment then requires me to take the output and apply it in another way. I will spend more time on that portion of it before looking for help.

Thank you!

Once you get the substring [0..n] and assign it into a String variable, you can just print that. However, you're not getting that substring now.

ch1 = sentence.charAt(midIndex);
midChars = String.valueOf(ch1);

What is it that you think this is doing?

And, assuming you're using "Need Help" as your starting String, what is the output you're hoping to see eventually?

Once you get the substring [0..n] and assign it into a String variable, you can just print that. However, you're not getting that substring now.

ch1 = sentence.charAt(midIndex);
midChars = String.valueOf(ch1);

What is it that you think this is doing?

And, assuming you're using "Need Help" as your starting String, what is the output you're hoping to see eventually?

As I understand the problem I need to take the original sentence "Need Help" and make a substring of the first half of the letters.

ch1 = sentence.charAt(midIndex); - I thought would give a character for the middle variable in the string.

midChars = String.valueOf(ch1);

- I thought it would give value to the character so it could divided in half to return whther the half way point was an odd or even number.

The output I am looking for is "Need" so I guess my question now is how do I get the substring "need" from the original sentence?

I sincerely appreciate your help. Talking the problem out is helping me grasp the concepts.

You're right about charAt() - it returns the character at position n. However, you don't need that character for anything, do you? If you need to know whether the halfway point is an odd or even number, you would look at midIndex, since that's your calculated midpoint, but you don't need that, do you?
You need the substring from 0 to n, where the value of n will be calculated from the length of the original String.

For these strings, what are the correct outputs? If indexes start at 0, what is the range you're selecting?

question
answers
three
four
two


(You should navigate over to the Sun documentation for the String class and read what it has to say about "valueOf()" and "substring()", I think you'll find that very helpful. )

You're right about charAt() - it returns the character at position n. However, you don't need that character for anything, do you? If you need to know whether the halfway point is an odd or even number, you would look at midIndex, since that's your calculated midpoint, but you don't need that, do you?
You need the substring from 0 to n, where the value of n will be calculated from the length of the original String.

For these strings, what are the correct outputs? If indexes start at 0, what is the range you're selecting?

question
answers
three
four
two

(You should navigate over to the Sun documentation for the String class and read what it has to say about "valueOf()" and "substring()", I think you'll find that very helpful. )

I am doing the readings tonight, I always appreciate a reading recommendation.

question (0,4)
answers (0,4)
three (0,3)
four (0,3)
two (0,1) Is that what you were asking of me? If so what would be the best way to state the range, when the limit is a middle point of an even number?

I am indebted to you and sincerely appreciate you for walking me through this.

Is that what you were asking of me?

That's what I was asking - your answers for the right hand side of the ranges are correct IF you take ranges as exclusive. That is, if you were getting up to but not including the indicated right-hand boundary, these would be correct. However, substring's ranges are inclusive on both sides, so that range for "question" would get you "quest", which is not what you want.
This could be a problem with zero-index arithmetic (you were counting from one instead of from zero) or with your assumptions about the behavior of substring, I can't tell.
(Your answer for "two" is correct, however)

The range is going to be the leftmost n characters of the string, where n is length/2, discarding any remainder. At least, that's how I read "starting from index 0 and ending before the middle Character(s)." I could be wrong. If I read that more literally, I would assume that the "middle Character(s)" of "question" would be "st", in which case n would be (length-1)/2, discarding remainder.
Now, n is not the right-hand index you're looking for - remember, you're counting from zero, not from 1, so you have to correct for that.

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.