Results 1 to 2 of 2
  1. #1
    tripline is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default How Can i already be stuck? String play java

    public class test
    {public static void main (String[] args)
    {
    String word = "rotatemeplease";
    String LastFour = "";
    LastFour =word.substring(9, 4);
    System.out.println(LastFour);
    }
    }
    I should be able to get the last 4 letters into LastFour. Help. out of range error

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How Can i already be stuck? String play java

    @tripline: Look at the String api to see how to use substring with two parameters correctly. The second parameter is not the length of the substring but the second index.

    Also, you'll want to use code tags when posting code to this forum so your code will look like this:

    Java Code:
    public class Test {
       public static void main(String[] args) {
          String word = "rotatemeplease";
          String LastFour = word.substring(word.length() - 4, word.length());
          System.out.println(LastFour);
       }
    }
    Not like this:

    public class Test {
    public static void main(String[] args) {
    String word = "rotatemeplease";
    String LastFour = word.substring(word.length() - 4, word.length());
    System.out.println(LastFour);
    }
    }

    @skaterboy: Please look at the API too so you will now know how to use substring with two parameters correctly and can give good advice for a similar question. Also please don't encourage the use of "magic" numbers.

Similar Threads

  1. How to play movie using java
    By sithara in forum New To Java
    Replies: 3
    Last Post: 02-20-2013, 01:29 PM
  2. how to play a video from a file using jmf in a java
    By 200808147 in forum New To Java
    Replies: 0
    Last Post: 07-10-2011, 01:20 PM
  3. help to play file.wav in java
    By jperson in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 04:10 AM
  4. I want play chess in Java
    By ganzorig in forum Advanced Java
    Replies: 6
    Last Post: 11-15-2009, 05:53 AM
  5. how can I play an mp3 file in java?
    By iliana in forum Networking
    Replies: 1
    Last Post: 05-30-2008, 08:32 PM

Posting Permissions

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