+ 1

I can't print my array with int a = arr.length

If I put a number instead "a" my sister.out.println(arr[a]); It works perfectly but if I use arr[a] it send a wrong import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); int a = 0 ; int x = arr.length; while (a <= x){ System.out.println(arr[a]); a++; } } }

14th Feb 2021, 11:23 PM
Francisco Guadarrama Sanchez
Francisco Guadarrama Sanchez - avatar
4 Answers
+ 2
Do you have a question? I don't see a question or problem here.
14th Feb 2021, 11:58 PM
Josh Greig
Josh Greig - avatar
+ 2
Yes, Ipang is right. You probably want < instead of <=. while (a < x){ System.out.println(arr[a]); a++; }
15th Feb 2021, 2:09 AM
Josh Greig
Josh Greig - avatar
+ 1
Sorry, the title is I can't instead I can
15th Feb 2021, 12:00 AM
Francisco Guadarrama Sanchez
Francisco Guadarrama Sanchez - avatar
+ 1
Your while loop should run while <a> less than <x>, not while <a> less than or equal to <x>. while( a < x )
15th Feb 2021, 2:05 AM
Ipang