Results 1 to 8 of 8
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default One last quick question

    Yeah I know, I've been asking a lot of questions but this is the last one for a whileD

    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
    Java 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
    Java 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



    Java 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
    Last edited by jigglywiggly; 01-26-2009 at 02:13 AM.

  2. #2
    rahul_swe23 is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    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;
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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);
           }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    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.

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  7. #7
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Thanks again you guys, one of the most helpful forums everDD

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 PM
  2. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 02:26 PM
  3. Quick question about sorting
    By nbd223 in forum New To Java
    Replies: 10
    Last Post: 04-28-2008, 09:11 AM
  4. Quick Java question/help
    By Zedy in forum New To Java
    Replies: 6
    Last Post: 04-22-2008, 03:40 AM
  5. Quick Question (Functions)
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •