java - Print Elements of ArrayList in order of input string -
what trying is, input string user. add arraylist since have play characters later. first want print contents of arraylist in order check in their. here code
public static void main(string[] args) { scanner sc = new scanner(system.in); list <string> vowels = new arraylist<string>(); system.out.println("what string? "); string abc = sc.nextline(); int n = abc.length(); for(int i=0; <= n-1 ; i++) { vowels.add(sc.nextline()); } for(int j=0; j< vowels.size();j++) system.out.println(vowels.get(j)); } it not printing or not showing error. highly appreciated.
here problem:
for(int i=0; <= n-1 ; i++) { vowels.add(sc.nextline()); ^ } "it not printing anything" because waiting input
what need is:
public static void main(string[] args) { scanner sc = new scanner(system.in); list <string> vowels = new arraylist<string>(); system.out.println("what string? "); string abc = sc.nextline(); int n = abc.length(); for(int i=0; < n ; i++) { vowels.add(new stringbuilder().append("").append(abc.charat(i)).tostring()); } for(int j=0; j< vowels.size();j++) system.out.print(vowels.get(j)); } input : burger
output: burger
if want output [b, u, r, g, e, r], do:
system.out.println(vowels); //without loop
Comments
Post a Comment