# One last quick question

• 01-26-2009, 03:06 AM
jigglywiggly
One last quick question
Yeah I know, I've been asking a lot of questions but this is the last one for a while:D:D

this is the question
e) Write a loop that prints to the screen the last letter in each string stored in a String array named stringArray starting with the first String in the array and ending with the last. (Example: if stringArray = {“Hello”, “my”, “name”, “is”, “Bob”} your loop should print “oyesb” to the screen.)

So first in netbeans to test if my code even works I just created a stringArray

Code:

```  String stringArray[] = new String[5];       stringArray[0] = "hello";       stringArray[1] = "my";       stringArray[2] = "name";       stringArray[3] = "is";       stringArray[4] = "bob";```

So then I went ahead and tried this just to get an idea
Code:

```int j = stringArray.length;         for(int i=0; i<j; i++) {             int mn=stringArray[i].length();```
Course all that will do is tell how long the string is, as in int value.

How would I tell it to go to the last place and store it?

I tried something like this except it does not make much sense
Code:

```  for (int dra = stringArray[i].length() - 1; dra >= 0; dra--)             {             char g=stringArray[i].charAt(mn);             }```
This goes inside the other for loop

So together it looks like this

Code:

``` String stringArray[] = new String[5];       stringArray[0] = "hello";       stringArray[1] = "my";       stringArray[2] = "name";       stringArray[3] = "is";       stringArray[4] = "bob";       String d= "";               int j = stringArray.length;         for(int i=0; i<j; i++) {             int mn=stringArray[i].length();                  for (int dra = stringArray[i].length() - 1; dra>= 0; dra--)             {             char g=stringArray[i].charAt(mn);             }             }```
Oh and heres the error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:687)
at kitty.main(kitty.java:31)
Java Result: 1
• 01-26-2009, 04:39 AM
rahul_swe23
hello
i am rahul very new in java programing , i have doubt i trying to write a Tic-Tac_Tow game with help of book .some portion of code i am not able to under stand (high lighted code ).please explain
public void init(){
// Get the applet's content pane -
// all window components go there
Container appletContent = this.getContentPane();

//Set the applet's layout manager, font and background color
appletContent.setLayout(new BorderLayout());
appletContent.setBackground(Color.CYAN);

// Create the button New Game and register it
// with the action listener
newGameButton=new JButton("New Game");

JPanel topPanel=new JPanel();

JPanel centerPanel=new JPanel();
centerPanel.setLayout(new GridLayout(3,3));

// create an array to hold references to 9 buttons
squares=new JButton[9];

// Instantiate the buttons, store the references
// to them in the array, register them with the
// listeners, paint them in orange and add to panel
for(int i=0;i<9;i++){
squares[i]=new JButton();
squares[i].setBackground(Color.ORANGE);
}
}
/**
* This method will process all action events
* @param ActionEvent object
*/
public void actionPerformed(ActionEvent e) {

JButton theButton = (JButton) e.getSource();
// Is this a New Game button?
if (theButton ==newGameButton){
for(int i=0;i<9;i++){
squares[i].setEnabled(true);
squares[i].setText("");
squares[i].setBackground(Color.ORANGE);
}

emptySquaresLeft=9;
newGameButton.setEnabled(false);

return; // exit the method here
}

String winner = "";
// Is this one of the squares?
for ( int i=0; i<9; i++ ) {
if ( theButton == squares[i] ) {
squares[i].setText("X");

winner = lookForWinner();

if(!"".equals(winner)){
endTheGame();
} else {
computerMove();
winner = lookForWinner();
if ( !"".equals(winner))
{
endTheGame();
}
}
break;
}
} // end loop

if ( winner.equals("X") ) {
score.setText("You won!");
} else if (winner.equals("O")){
score.setText("You lost!");
} else if (winner.equals("T")){
score.setText("It's a tie!");
}
} // end actionPerformed

/**
* This method is called after every move to see
* if we have a winner.
* It checks every row, column and diagonal to find out
* three squares with the same label (other than blank)
* @return "X", "O", "T" for tie or "" for no winner
*/
String lookForWinner() {

String theWinner = "";
emptySquaresLeft--;

if (emptySquaresLeft==0){
return "T"; // it's a tie
}

// Check the row 1 - array elements 0,1,2
if (!squares[0].getText().equals("") &&
squares[0].getText().equals(squares[1].getText()) &&
squares[0].getText().equals(squares[2].getText()) ) {

the);Winner = squares[0].getText();
highlightWinner(0,1,2

// Check the row 2 - array elements 3,4,5
} else if (!squares[3].getText().equals("") &&
squares[3].getText().equals(squares[4].getText()) &&
squares[3].getText().equals(squares[5].getText())) {

theWinner = squares[3].getText();
highlightWinner(3,4,5);
// Check the row 3 - - array elements 6,7,8
} else if ( ! squares[6].getText().equals("") &&
squares[6].getText().equals(squares[7].getText()) &&
squares[6].getText().equals(squares[8].getText()) ) {

theWinner = squares[6].getText();
highlightWinner(6,7,8);
// Check the column 1 - array elements 0,3,6
} else if ( ! squares[0].getText().equals("") &&
squares[0].getText().equals(squares[3].getText()) &&
squares[0].getText().equals(squares[6].getText())) {

theWinner = squares[0].getText();
highlightWinner(0,3,6);
// Check the column 2 - array elements 1,4,7
} else if ( ! squares[1].getText().equals("") &&
squares[1].getText().equals(squares[4].getText()) &&
squares[1].getText().equals(squares[7].getText())) {

theWinner = squares[1].getText();
highlightWinner(1,4,7);
// Check the column 3 - array elements 2,5,8
} else if ( ! squares[2].getText().equals("") &&
squares[2].getText().equals(squares[5].getText()) &&
squares[2].getText().equals(squares[8].getText()) ) {
theWinner = squares[2].getText();
highlightWinner(2,5,8);
// Check the first diagonal - array elements 0,4,8
} else if ( ! squares[0].getText().equals("") &&
squares[0].getText().equals(squares[4].getText()) &&
squares[0].getText().equals(squares[8].getText())) {

theWinner = squares[0].getText();
highlightWinner(0,4,8);
// Check the second diagonal - array elements 2,4,6
} else if ( ! squares[2].getText().equals("") &&
squares[2].getText().equals(squares[4].getText()) &&
squares[2].getText().equals(squares[6].getText()) ) {

theWinner = squares[2].getText();
highlightWinner(2,4,6);
}
return theWinner;
}
• 01-26-2009, 04:42 AM
Fubarable
rahul, it's kind of rude to step on someone else's thread. Best to delete the text in your post and start a new thread so as not to hijack jiggly wiggly's.
• 01-26-2009, 04:44 AM
Eranga
Reason is to have that error is, charAt() method refers zero base index.

Code:

```      for(int i = 0; i < stringArray.length; i++) {           d += stringArray[i].charAt(stringArray[i].length() - 1);       }```
• 01-26-2009, 04:46 AM
Eranga
Quote:

Originally Posted by Fubarable
rahul, it's kind of rude to step on someone else's thread. Best to delete the text in your post and start a new thread so as not to hijack jiggly wiggly's.

Yes rahul, it's mess up all. I'm too here. Please ask your question in the correct place rahul. Don't send yours in others, if yours not related to it. And please read our FAQ page as well.
• 01-26-2009, 04:50 AM
angryboy
jigglywiggly, you should use s.charAt() not s[i].length.
i think this same problem has been asked and solved in another thread a week ago. anybody remember the link?
• 01-26-2009, 07:53 AM
jigglywiggly
Thanks again you guys, one of the most helpful forums ever:D:D:D:D
• 01-26-2009, 09:53 AM
Eranga
If you have solve the problem, please mark it as solved.