java - I keep getting an exception in main error after i enter my first array -
java - I keep getting an exception in main error after i enter my first array -
for reason getting error:
exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: 11 @ java.lang.string.charat(string.java:646) @ identicalarrays.main(identicalarrays.java:28)
/* * class: cs 2301/08 * term: fall 2014 * name: clarence e. hollins iii * instructor: rashad jones * assignment: 4 */ //create programme receives 2 strings user , prints whether or not 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 = 0; < firstlist.length; i++) { firstlist[i] = s.charat(i+1); } system.out.println("please come in list 2: "); int[] secondlist = new int[size]; (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 sec list is: " + secondlist.length + " " + secondlist); } public static boolean equals(int[] list1, int[] list2) { arrays.sort(list1); arrays.sort(list2); for(int = 0; < list1.length && < list2.length; i++) { if (list1[i] != list2[i]) { homecoming false; } } homecoming true; } }
it's hard 100% sure, but, if you're going do...
char sizeofarray = s.charat(0);
and then...
for (int = 0; < firstlist.length; i++) { firstlist[i] = s.charat(i + 1);
you want cut down number of loops 1, you've taken first character out of string
now, utilize string#substring
cut down input 1 character or cut down number of iterations one...
for (int = 0; < firstlist.length - 1; i++) { firstlist[i] = s.charat(i + 1);
this go sec loop well
now, having said that, given fact ignoring size
value anyway, should more like...
//char sizeofarray = s.charat(0); int size = s.length(); int[] firstlist = new int[size]; (int = 0; < size; i++) { firstlist[i] = s.charat(i); }
mind you, based on requirements...
create programme receives 2 strings user , prints whether or not identical
something string#equals(string)
quicker solution...;)
java arrays sorting boolean compare
Comments
Post a Comment