java word scramble problem

On lesson 12.3,  and after entering the code as she has it written,  the part that is supposed to remove a letter from the original word is not working.  She says it should, but it isn't.  I can't find my mistake, and have checked to make sure I have the code that I'm supposed to.... (used the cheat sheet in the back of the book to see complete code).

Time to google again and see what I'm missing.  Will update when I find the issue.

Found it....  I did not have the code like she said to...
The blue line should be :   String firstString = word.substring(0, index);
not :   String firstString = word.substring(0, index +1);
Google didn't help, so I played with it, and realized I hadn't compared it to the cheat thoroughly.

package nellie.tobey.wordscramble;
import java.util.Random;
import nellie.tobey.mywindow.MyWindow;
public class WordScramble extends MyWindow {
 public WordScramble() {
  String word = "ANIMALS";
  String scrambled = scramble(word);
  print(scrambled);

 }
    private String scramble(String word) {
     String scrambled = "";
     //pick random letter from word
     Random rand = new Random();
     int length = word.length();
     int index = rand.nextInt(length);
     String letter = word.substring(index, index + 1);
     //remove letter from original word
     String firstString = word.substring(0, index +1);
     String secondString = word.substring(index + 1);
     word = firstString + secondString;
     //add that letter to a new word
     //repeat until all letters removed from original
   
     System.out.println("index = " + index);
     System.out.println("letter = " + letter);
     System.out.println("word = " + word);
     return scrambled;
    }
 public static void main(String[] args) {
  new WordScramble();
 }
}

Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame