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

30th Apr 2020, 5:51 AM
Epsilon ︻╦̵̵͇̿̿̿̿╤──
Epsilon ︻╦̵̵͇̿̿̿̿╤── - avatar
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));
30th Apr 2020, 5:54 AM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
+ 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 []
30th Apr 2020, 6:13 AM
A͢J
A͢J - avatar
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.
30th Apr 2020, 6:16 AM
ChaoticDawg
ChaoticDawg - avatar