Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Having a java.lang.StringIndexOutOfBoundsException.

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Having a java.lang.StringIndexOutOfBoundsException.

    I am writing a code but I am getting a java.lang.StringIndexOutOfBoundsException: String index out of range: 2.Can anyone tell me what could be the reason for this exception?
    Thanks in advance.
    import java.util.Scanner;
    class Joueurs
    {
      public static void main(String[] args)
      {
        Scanner entrée = new Scanner(System.in);
        String joueur1 = entrée.nextLine();
        String joueur2 = entrée.nextLine();
        int longueur = 0;
        int longueur1= joueur1.length();
        int longueur2= joueur2.length();
        if(longueur1 > longueur2)
        {
          longueur = longueur1;
        }else if(longueur1 < longueur2){
          longueur = longueur2;}
        for (int iCar = 0; iCar < longueur ; iCar++)
        {
          if( joueur1.charAt(iCar)< joueur2.charAt(iCar)){
            System.out.println(1);
            iCar = longueur-1 ;
          }else if( joueur1.charAt(iCar)> joueur2.charAt(iCar)){
            System.out.println(2);
            iCar = longueur-1 ;
          }
          if((longueur1 < longueur) && (longueur1== iCar)){
            System.out.println(2);
            iCar = longueur-1 ;
          }
          else if((longueur2< longueur) && (longueur2== iCar))
          {
            System.out.println(1);
            iCar = longueur-1 ;
          }else if
            (longueur1 == longueur2)  {
            System.out.println("=");
                  }
        }
      }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,554
    Thanks
    52
    Thanked 2,317 Times in 2,273 Posts

    Default Re: Having a java.lang.StringIndexOutOfBoundsException.

    StringIndexOutOfBoundsException: String index out of range: 2.
    Can anyone tell me what could be the reason for this exception?
    The exception is caused by code that uses an index of 2 with a String that only has two characters or less. Remember that indexes start at 0. The String needs at least 3 characters to use an index of 2.

    What is the length of the String you are using charAt with? Make sure the max index used is less than the length of the String.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    abdoucasa (July 8th, 2018)

  4. #3
    Junior Member
    Join Date
    Jul 2018
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Having a java.lang.StringIndexOutOfBoundsException.

    Thanks for your help, Norm. I solved the problem. Here is the correct code according to my current level. I am a beginner in Java programming.
    {
      public static void main(String[] args)
      {
        Scanner entrée = new Scanner(System.in);
        String joueur1 = entrée.next();
        String joueur2 = entrée.next();
        int longueur = 0;
        int longueur1= joueur1.length();
        int longueur2= joueur2.length();
        int égalités = 0;
        if(longueur1 > longueur2)
        {
          longueur = longueur2;
        }else if(longueur1 < longueur2){
          longueur = longueur1;
        }else{
          longueur = longueur2;
        }
        for (int iCar = 0; iCar < longueur ; iCar++)
        {
          if( joueur1.charAt(iCar)< joueur2.charAt(iCar)){
            System.out.println(1);
            iCar = longueur ;
          }else if( joueur1.charAt(iCar)> joueur2.charAt(iCar)){
            System.out.println(2);
            iCar = longueur ;
          }else if (joueur1.charAt(iCar)== joueur2.charAt(iCar)){
            égalités = égalités + 1;
          }
          if((iCar ==longueur1-1) &&( iCar ==longueur2-1)){
            System.out.println("=");
            iCar = longueur ; 
          }else   if((iCar ==longueur1-1) &&( iCar !=longueur2-1)){
            System.out.println(2);
            iCar = longueur ;
          }else if((iCar ==longueur2-1) &&( iCar !=longueur1-1)){
            System.out.println(1);
            iCar = longueur ;} 
        }
        System.out.println(égalités);
      }
    }

Similar Threads

  1. Replies: 2
    Last Post: July 2nd, 2013, 05:59 PM
  2. java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPanel
    By Freshtilldeath0 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2012, 07:19 AM
  3. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 12:53 PM
  4. AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    By nasi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2010, 11:37 PM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM

Tags for this Thread