I keep getting an exception in main error after i enter my first array

For some reason I am getting the error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 11 at java.lang.String.charAt(String.java:646) at IdenticalArrays.main(IdenticalArrays.java:28)

/* * Class: CS 2301/08 * Term: Fall 2014 * Name: Clarence E. Hollins III * Instructor: Rashad Jones * Assignment: 4 */ //Create a program that receives two strings from the user and prints whether or not they are identical

import java.util.Scanner;
import java.util.Arrays;
public class IdenticalArrays
{
   public static void main(String [] Args)
  {
     Scanner input = new Scanner(System.in);



     System.out.println("Enter list: ");
     String s = input.nextLine();
     char sizeofArray = s.charAt(0);

     int size = (int)sizeofArray; 

     int[] firstList = new int [size];
        for(int i = 0; i < firstList.length; i++)
           {
              firstList[i] = s.charAt(i+1);
           }



     System.out.println("Please enter list 2: ");
     int[] secondList = new int[size];

           for (int i=0; i<10; i++)
              {
                 secondList[i] = s.charAt(i+1);
              } 


              boolean status = equals(firstList, secondList);
     if(status == true)


     System.out.println("The first list is: " + firstList.length + " " + firstList);
     System.out.println("The second list is: " + secondList.length + " " + secondList);

  }

   public static boolean equals(int[] list1, int[] list2)
  {

     Arrays.sort(list1);
     Arrays.sort(list2);

     for(int i = 0; i < list1.length && i < list2.length; i++)
     {
        if (list1[i] != list2[i])
        {
                        return false;
        } 
     }
     return true;

  }      
}         

Upvotes: 0

Views: 46

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201507

Just use String.toCharArray() like

char[] firstList = s.toCharArray();

If those characters are supposed to be digits, you could do

char[] firstList = s.toCharArray();
int[] digits = new int[firstList.length];
for (int i = 0; i < digits.length; i++) {
    digits[i] = Character.digit(firstList[i], 10);
}

Finally, to test if two int arrays are equal you can use Arrays.equals(int[],int[]).

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

It's hard to be 100% sure, but, if you're going to do...

char sizeofArray = s.charAt(0);

And then...

for (int i = 0; i < firstList.length; i++) {
    firstList[i] = s.charAt(i + 1);

You will want to reduce the number of loops by 1, as you've taken the first character out of the String

Now, you could use String#subString to reduce the input by one character or you could simply reduce the number of iterations by one...

for (int i = 0; i < firstList.length - 1; i++) {
    firstList[i] = s.charAt(i + 1);

This will go for your second loop as well

Now, having said that, given the fact that you are basically ignoring the size value anyway, you should probably do something more like...

    //char sizeofArray = s.charAt(0);
    int size = s.length();

    int[] firstList = new int[size];
    for (int i = 0; i < size; i++) {
        firstList[i] = s.charAt(i);
    }

Mind you, based on the requirements...

Create a program that receives two strings from the user and prints whether or not they are identical

Something like String#equals(String) would be a quicker solution...;)

Upvotes: 0

Related Questions