0

Some issues

I wrote this code in Code Coach - Symbols and it gives me OutOfBoundExceptions, anyone know why? https://code.sololearn.com/cOSevzAD6kfm/?ref=app

8th Jul 2020, 1:31 PM
Gaming2nd
Gaming2nd - avatar
6 odpowiedzi
+ 1
for (int I = 0; I < containSymbol.length; I++) { check = noSymbol.get(I); if (use.CheckForSymbol(check)) { continue; } else { noSymbol.remove(I); } From this code, you are running loop for charecter array length and first it is same as ArrayList length but in loop you are removing ArrayList items so it size decreases.. So for ex: input length say 5, if item is removed from list, then list size is Less than 5 so ex list[5] raise that exception by noSymbol.get(I). Hope you get it.. if condition help to avoid that..
8th Jul 2020, 2:06 PM
Jayakrishna 🇮🇳
+ 2
You should have used noSymbol.size() instead (this will get rid of the error, but introduce another issue) , but really if you just change your logic a bit and only add the characters to the ArrayList, or better yet, just output the character, if CheckForSymbol() only finds ' ', 0-9, a-z or A-Z. This will eliminate the need for a good amount of your code.
8th Jul 2020, 2:15 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Hai Dong You can use the for-in loop to loop over the chars in the toCharArray() of the input String. Then just pass each char to CheckForSysmbol(c). If it returns true output the char with System.out.print(c). This leaves your main method as; Scanner input = new Scanner(System.in); String newString = input.nextLine(); Program use = new Program(); char[] containSymbol = newString.toCharArray(); for(char ch: containSymbol) { if(use.CheckForSymbol(ch)) System.out.print(ch); // or add to ArrayList } That is all that is needed. If you wanted to you could add the char (ch) in the loop to the ArrayList and then just add another for-in loop that loops over the ArrayList. for(Character ch: noSymbol) { System.out.print(ch); }
8th Jul 2020, 7:13 PM
ChaoticDawg
ChaoticDawg - avatar
0
This is the fixed code but now it prints No output, I guess either copy the array or string doesn't work while (count < noSymbol.size()) { if (use.CheckForSymbol(noSymbol.get(max))) { max++; // i added max int continue; } else { noSymbol.remove(max); } } char[] normalNoSymbol = new char[noSymbol.size()]; for (int I = 0; I < noSymbol.size(); I++) { normalNoSymbol[I] = noSymbol.get(I); } String output = ""; for (int i2 = 0; i2 < normalNoSymbol.length; i2++) { output = output + (String)normalNoSymbol[i2]; } System.out.println(output);
8th Jul 2020, 3:16 PM
Gaming2nd
Gaming2nd - avatar
0
Hai Dong in 2nd for loop, justing adding this if condition to your previous code works fine.. if(I<noSymboks.size()) check = noSymbol.get(I); Or just take noSymbols.size() instead of normalNoSymbol.length in for loop condition also works...
8th Jul 2020, 4:43 PM
Jayakrishna 🇮🇳
0
Jayakrishna I found out that I didn't use the max varaiables so I replace it with count which exists instead. Also when I replace normalNoSymbol.length with noSymbol.size() it got an error that char can't convert into string so I delete (string) and it worked! Thanks!
9th Jul 2020, 4:33 AM
Gaming2nd
Gaming2nd - avatar