Results 1 to 8 of 8
Thread: One last quick question
- 01-26-2009, 02:06 AM #1
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
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
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
Java 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
Course all that will do is tell how long the string is, as in int value.Java Code:int j = stringArray.length; for(int i=0; i<j; i++) { int mn=stringArray[i].length();
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
This goes inside the other for loopJava Code:for (int dra = stringArray[i].length() - 1; dra >= 0; dra--) { char g=stringArray[i].charAt(mn); }
So together it looks like this
Oh and heres the errorJava 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); } }
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: 1Last edited by jigglywiggly; 01-26-2009 at 02:13 AM.
- 01-26-2009, 03:39 AM #2
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
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");
newGameButton.addActionListener(this);
JPanel topPanel=new JPanel();
topPanel.add(newGameButton);
appletContent.add(topPanel,"North");
JPanel centerPanel=new JPanel();
centerPanel.setLayout(new GridLayout(3,3));
appletContent.add(centerPanel,"Center");
score=new JLabel("Your turn!");
appletContent.add(score,"South");
// 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].addActionListener(this);
squares[i].setBackground(Color.ORANGE);
centerPanel.add(squares[i]);
}
}
/**
* 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;
score.setText("Your turn!");
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;
}
-
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, 03:44 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Reason is to have that error is, charAt() method refers zero base index.
Java Code:for(int i = 0; i < stringArray.length; i++) { d += stringArray[i].charAt(stringArray[i].length() - 1); }
- 01-26-2009, 03:46 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-26-2009, 03:50 AM #6
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?USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 01-26-2009, 06:53 AM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Thanks again you guys, one of the most helpful forums ever
D
D
- 01-26-2009, 08:53 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you have solve the problem, please mark it as solved.
Similar Threads
-
Quick Question
By Graeme in forum New To JavaReplies: 4Last Post: 01-08-2009, 08:01 PM -
Quick Question
By Spenc in forum New To JavaReplies: 3Last Post: 09-22-2008, 02:26 PM -
Quick question about sorting
By nbd223 in forum New To JavaReplies: 10Last Post: 04-28-2008, 09:11 AM -
Quick Java question/help
By Zedy in forum New To JavaReplies: 6Last Post: 04-22-2008, 03:40 AM -
Quick Question (Functions)
By ibanez270dx in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks