I try to print reverse of the entering number without any for loop. But I've got some issues with printing the Arraylist. How can I print the Arraylist [3, 5, 4, 1] as 3541 - without brackets, commas and spaces?

If not possible, how can I add my ArrayList elements to stringlist then print?

public static void main(String[] args) {

    int yil, bolum = 0, kalan;
    Scanner klavye = new Scanner(System.in);
    ArrayList liste = new ArrayList();
    //String listeStr = new String();
    System.out.println("Yıl Girin: "); // enter the 1453
    yil = klavye.nextInt();

    do{ // process makes 1453 separate then write in the arraylist like that [3, 5, 4,1]

        kalan = yil % 10;
        liste.add(kalan);
        bolum = yil / 10;
        yil = bolum;

    }while( bolum != 0 );

    System.out.println("Sayının Tersi: " + ....); //reverse of the 1453
    klavye.close();
}
share|improve this question
Duplicate of stackoverflow.com/questions/196628/… – r0ast3d Oct 22 '12 at 17:14
why do you need to avoid the for loop?? – jlordo Oct 22 '12 at 17:15
You can read in String instead of number (or you can convert from int to String) and use StringBuilder reverse(). – nhahtdh Oct 22 '12 at 17:17
You may want to check out stackoverflow.com/questions/196628/… – user1595254 Oct 22 '12 at 17:18
1  
StringBuilder reverse my lead into wrong values. – Christian Kuetbach Oct 22 '12 at 17:23
feedback

4 Answers

up vote 2 down vote accepted
public static void main(String[] args) {


    int yil, bolum = 0, kalan;
    ArrayList liste = new ArrayList();
    System.out.println("Yıl Girin: "); // enter the 1453
    yil = 1453;

    String s="";
    do { // process makes 1453 separate then write in the arraylist like that [3, 5, 4,1]

        kalan = yil % 10;
        liste.add(kalan);
        s= s + kalan;  // <------- THE SOLUTION AT HERE  -------

        bolum = yil / 10;
        yil = bolum;

    } while (bolum != 0);

    System.out.println("Sayının Tersi: " + s ); //reverse of the 1453

}
share|improve this answer
1  
Thank you so much! – android93 Oct 22 '12 at 17:30
2  
StringBuilder would be better here for appending characters but this seems to be most appropriate to solve the overall FIFO problem here so +1! – Reimeus Oct 22 '12 at 17:35
feedback
  • Read the entry as a String
  • Reverse it with String reverse = new StringBuilder(input).reverse().toString();
  • Optional: parse it to an int if you need to do some calculations with that int.
share|improve this answer
[1,23] will become 321 insted of 231. I think tht ws the intented output. – Christian Kuetbach Oct 22 '12 at 17:25
My understanding is that the user enters one number (1453 in the given example) and the goal is to reverse that number (to 3541 in the given example). I might have misunderstood. – assylias Oct 22 '12 at 17:26
but, wht is, if one of the numbers lrger than 9? I think that is not clear. – Christian Kuetbach Oct 22 '12 at 17:28
feedback
List strings = ...
List stringsRevers = Collections.reverse(strings);
// Have to reverse the input
// 1,23 -> 231 
// otherwise it would be 321   
StringBuilder sb = new StringBuiler();
for(String s: stringsRevers ){
    sb.ppend(s);
}
String out = sb.toString();
share|improve this answer
feedback

- Reverse can be easily obtained using Collections.reverse(List<?> l)

Eg:

ArrayList<String> aList = new ArrayList<String>();

Collections.reverse(aList);

- Use For-Each loop to print it out.

Eg:

for(String l : aList){

    System.out.print(l);

}
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.