0
Extra-terrestrials Java
Ok I'm dumb in Java but why am I getting an error. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner stri = new Scanner(System.in); String str = stri.next(); for (int i = str.length() - 1; i >= 0; i--) { System.out.println(str[i]); } } } It says str is not an array
3 Réponses
+ 3
String doesn't work with [] but with a method which is called charAt ()
So change this line to this:
System.out.println (str.charAt (i));
+ 2
Epsilon Yes str is not an array. It's a string. To convert into array just use split method with seperator like
String str = "AJ Anant";
//here str is String because of keyword String
String [] strArr = str.split(" ");
//strArr is array because of []
0
Hint: there is an easy way in the StringBuilder class to do this (reverse a String)
You could also convert the string to a char array.