How to use formatted console output in Java

3 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        int[] arr = new int[] {2, 5, 3, 5, 5, 1, 5, 7, 3};
 
        for (int i = 0; i < arr.length; i++) {
             System.out.printf("%d : %d\n", i, arr[i]);
        }
    }
}



/*
run:

0 : 2
1 : 5
2 : 3
3 : 5
4 : 5
5 : 1
6 : 5
7 : 7
8 : 3

*/

 



answered Feb 22, 2019 by avibootz
edited Feb 22, 2019 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        int[] arr = new int[] {2, 5, 3, 5, 5, 1, 5, 7, 3};
 
        String output;
        for (int i = 0; i < arr.length; i++) {
             output = String.format("%d : %d", i, arr[i]);
             System.out.println(output);
        }
    }
}



/*
run:

0 : 2
1 : 5
2 : 3
3 : 5
4 : 5
5 : 1
6 : 5
7 : 7
8 : 3

*/

 



answered Feb 22, 2019 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        int[] arr = new int[] {2, 5, 3, 5, 5, 1, 5, 7, 3};
 
        for (int i = 0; i < arr.length; i++) {
             System.out.format("%d : %d\n", i, arr[i]);
        }
    }
}



/*
run:

0 : 2
1 : 5
2 : 3
3 : 5
4 : 5
5 : 1
6 : 5
7 : 7
8 : 3

*/

 



answered Feb 22, 2019 by avibootz

Related questions

1 answer 152 views
152 views asked Jul 23, 2016 by avibootz
3 answers 301 views
1 answer 158 views
158 views asked Jun 11, 2015 by avibootz
1 answer 123 views
123 views asked Apr 1, 2021 by avibootz
1 answer 190 views
...