Tuesday, 15 May 2012

string - Problems with charAt() method in Java -


I am trying to create a text-based Hangout in Java.

This is my Java code: package executioner; Import java.util.Random; Import java.util.Scanner; Import java.lang.String; Public Class Hellman {public static zero main (string [] algs) {// Todo code application logic here scanner chase = new scanner (system.); Scanner waffle = new scanner (System.in); Random randomGenerator = new random (); StringBiller display word = new string builder ("______________________________"); String theWord = null; String preteller; // string sbDisplayWord; Four letters = 0; Int randomNumber; Int lengthOfWord; Int number operator inward = 0; Int gDisplay; Int estimate Word number = 0; String estimate word; RandWord troll = new random word (); RandomNumber = randomGenerator.nextInt (12); // fill the word with the word TheWord = troll.wordDecide (random number); System.out.println ("Welcome to jailman!"); LengthOfford = theWord.length (); System.out.println ("The word" + lengthAwards + "is the word");); System.out.println ("You have 20 estimates."); (Int g = 19; g> = 0; g--) {System.out.println ("If you want to guess the word, then type 0. If you want to guess a letter, type 1 "); GuessWordNumber = chez.nextInt (); If (Estimate Word Number == 0) {System.out.println ("Enter the word now. Remember, do not capitalize it."); GuessWord = waffle.nextLine (); If (Estimate Word.equals (theWord)) {System.out.println ("You Win"); System.exit (0); } Else {System.out.println ("Sorry, this was not the right word."); }} And if (Estimate Word Number == 1) {System.out.println ("Please enter the letter that you can guess."); //System.out.println (This will tell you whether you have guessed correctly for any letter. If it is empty, this means that no letters match.); PreLetter = chez.nextLine (); Letter = preLetter.charAt (0); Println (""); For (int i = 0; i & lt; = lengthOfWord -1; i ++) {// - ifh (letter == theWord.charAt (i)) {numberOfLetterInWord = I + 1; System.out.println ("This letter coincides with the letter number" + "WordOfferInrode +" in the word "); DisplayWord.setCharAt (i, letter); } And {numberOfLetterInWord = i + 1; System.out.println ("This letter does not match the word number" + word-in-word "in the word". "); }} System.out.println (""); System.out.println ("the word is yet" + displayWord); Println (""); GDisplay = g + 1; System.out.println ("You have" + G display + "estimate abandoned."); } Else {System.exit (0); }} System.out.println ("Game over"); System.exit (0); }} Package Hangman; Public Class RandWord {Private Static String [] Keywords = {"Psychology", "Keratin", "Nostalgia", "PyroManyak", "Chlorophyll", "Derivative", "Unidored", "Pattodactile", "Gialophone", "Exclusive "," Obituary "," infinite "," inferior ",}; Public String Word Decad (Int Random Number) {String the Word; TheWord = WordRay [Random Number]; Return The Word; NetBIANS is giving me this error:

Exceptions in the "main" java.lang.StringIndexOutOfBoundsException thread: outside the string index range: 0 hangman.Hangman.main (hangman.java:56)
Java results: 1

This is probably happening when you call fourte (0) on a string of length 0. Check to see that the wire is not empty before calling the method.

No comments:

Post a Comment