Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

nikhil davasam
nikhil davasam
2,182 Points

i am not understanding how he is referencing the objects ex-letter

class Game{
private String answer;
  private String hits;
  private String misses;

  public Game (String answer){
  this.answer = answer;
    hits = "";
    misses = "";
  }

  public boolean applyGuess (char letter){
  boolean isHit = answer.indexOf(letter) != -1;
    if (isHit){
    hits += letter;
    }else {
    misses +=letter;
    }
    return isHit;
  }
  public String getCurrentprogress() {
  String progress = "";
    for (char letter :answer.toArray()){
      char display = '-';
    }
    progress += display;
  }
  return progress;
     }
// and for the for methord it should be in the form 
for (initialization; termination;
     increment) {
    statement(s)
}

but why not????????????//

3 Answers

Is this question about the for loop construct?

You don't need to use the traditional construct in many modern languages so this:

for(int i = 0; i < 10; i++){
  // do stuff with 'i' to access arrays or string iterables

}

can still be used but isn't essential. Let's look at the loop that's causing you an issue:

    for (char letter :answer.toArray()){
      char display = '-';
    }

Here, the code is going to iterate over an array one element at a time; just like using array[i] in the first example. Inside the loop, we're creating a local variable called letter. We're then converting a string to a char array to make it an iterable object. The code does the rest! The statement(s) inside the braces will be executed and the contents of letter will hold each letter in turn of the string variable called answer. There's a method called toCharArray() that does the conversion of a String to an array of char. I'm not sure that toArray() will do the same - I think that's more to do with List but that's immaterial. The idea is that a variable that has a length is converted to something that can be iterated over. The for loop manages the rest without the need for a counter variable and incrementing etc. The for loop has evolved and improved!

The documentation for this "enhanced" for loop is contained on the exact page you copy & pasted your loop construct from:

for (initialization; termination;
     increment) {
    statement(s)
}
  • just scroll down a bit! Documentation here and there's a code demo here.

I hope that helps,

Steve.

nikhil davasam
nikhil davasam
2,182 Points

i understood the problem with letter but for the for loop //for (char letter :answer.toArray())// it doesnt have any connection with the treditional method and what does the above init refer to?

Hi Nikhil,

Sorry but I don't understand the question. What don't you understand - I'll try to walk you through it.

Steve.

nikhil davasam
nikhil davasam
2,182 Points

intead of this for (initialization; termination; increment) { statement(s)

how could they use this //for (char letter :answer.toArray())//

for a FORLOOP??

Hi Nikhil,

The enhanced for` loop works as I described in my first post.

The loop requires a local variable to hold each individual element of the 'thing' being iterated over, which you also pass into the loop at the outset. It is a common construct which can be seen in many languages, certainly in Python and Ruby.

So if we want to iterate over a String, as in this case, we first convert the string to an array of characters using the toCharArray() method. We then create a variable to hold each letter in the char array in turn. As the loop iterates over the converted string, each letter will be contained, in turn, in that variable.

for(char letter : answer.toCharArray()){
  // do something with letter
}

In this loop, above, each letter contained within the string answer will be held within letter. The loop will iterate over every letter in answer until it gets to the end of the string, then the loop will terminate itself.

The loop runs 'silently', so we don't have a counter variable that we iterate over. We can use the initialization, termination, increment way if you want. But there's a simpler method so the course encourages the use of that. It is just a newer construct that delivers the same result in a more effective manner. It's caused by the progression of the language over the years.

Here's the two ways of printing out each character in your name using the enhanced loop and the traditional loop. They do the same thing but the enhanced one has a lower overhead and probably runs more quickly.

public class Main {

    public static void main(String[] args) {

        String answer = "Nikhil Davasam";

        // enhanced loop

        for(char letter : answer.toCharArray()){
            System.out.println(letter);
        }

        // initialization; termination; increment ... 

        for(int i = 0; i < answer.length(); i++){
            char[] answerArray = answer.toCharArray();
            System.out.println(answerArray[i]);
        }
    }
}

I hope that helps.

Steve.

I think the line:

char[] answerArray = answer.toCharArray();

should be put before the loop, else we're creating a new variable every time the loops iterates! My bad ... :smile:

This is better:

char[] answerArray = answer.toCharArray();
for(int i = 0; i < answer.length(); i++){
    System.out.println(answerArray[i]);
}

:+1: