-5

This code is not that difficult but I'm a little confused as to what some of it means. It's goal is to return a new string made of every other character starting with the first.

I don't get what String result = ""; means. Is that null?

public String stringBits(String str)   
{  
    String result = "";  
    int i = str.length();  
    for(int j = 0; j<i; j+=2)  
    {  
        result = result + str.substring(j, j+1);  
    }  
    return result;  
}  

Any help would be appreciated. Thanks

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Asphalt45
  • 13
  • 2
  • Please format your code. – Sheikh Jan 03 '16 at 23:29
  • 2
    `""` is an empty literal String. It's not null. `String result = null;` would set the string to null. or if you had `private String result;` as a declaration in the class, that would set result to null (the default value for un-assigned reference fields). – Hovercraft Full Of Eels Jan 03 '16 at 23:30
  • 5
    You whether result is null -- and I want to suggest that you not ask this in the future but rather ***test*** it. You have an amazingly powerful laboratory at your fingertips, so use it. Experiment, play, write code, run it, change it, push it to the limit and then go beyond, find out what works what doesn't work. Trust me, you're not going to blow up your computer, you're not going to bring on doom and damnation from the effort. For simple questions that can be answered by testing, don't ask us here -- find out for yourself. That's what learning and what programming is all about. – Hovercraft Full Of Eels Jan 03 '16 at 23:36
  • Entirely unrelated, but I have no idea why someone would think it's a good idea to name a variable `i` and use it in a loop but *not* as a loop counter variable. – Jamal Jan 03 '16 at 23:46

4 Answers4

4

This is a very inefficient code that uses string concatenation in a loop, by adding one-character strings to the result. It starts result off with an empty string, and then chops off one-character chunks from the original string starting at even positions.

A better approach would use StringBuilder, and append individual characters, not substrings, to the result:

StringBuilder result = new StringBuilder();
for(int j = 0; j < i; j += 2) {
    // Append character at j to result
    result.append(str.charAt(j));
}
return result.toString();
Peter Hinson
  • 103
  • 6
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Here's a commented version of the code you posted:

// Declare a public method stringBits that accepts a String str
public String stringBits(String str) 
{
    // Declare a String literal named result containing an empty string ("")
    String result = "";

    // Store the length of the passed String str in integer i
    int i = str.length();

    // Loop while j (loop index) is less than the length of str (j < i)
    // j starts at 0 (int j = 0)
    // Increase j by 2 each iteration (j += 2)
    for(int j = 0; j < i; j += 2)
    {
        // Extract the character between index j and j+1, append to result
        result = result + str.substring(j, j+1);
    }

    // Once the loop has finished, return the result string to the calling method
    return result;
}

As others have mentioned, String result = "" simply means defined a String and sets the value to "". Strings can contain zero characters and still be valid, not null.

Good luck! This sort of stuff will come naturally soon enough.

Peter Hinson
  • 103
  • 6
1

The code receives the String str as an argument, takes out characters on even indices of String str and appends them to the result String. For example, if your String str is equal to "abcdef", the value of your String result will be "ace".

umairaslam
  • 367
  • 3
  • 22
0

String result = "" just means that the String is empty. It is not null though. You can put any text between those 2 quotation marks.

Sheikh
  • 539
  • 1
  • 7
  • 29