0

I am reading 2 arrays They are like:

{xxxxx, yyy, aaaaaa, rrrrrrrr}

and

{222, 44, 55, 77}

I want to have output as

xxxxx    === 222
yyy      ===  44
aaaaaa   ===  55
rrrrrrrr ===  77

How can I do that formatting?

I am trying this for achieve my goal

System.out.println(array[1] + "===" + array1[1]);

How can I achieve this?

4
  • You sure you didnt forget the second + in the exampel code? And whats weong with it? Just iterate instead of using "1" as index... Commented Oct 3, 2012 at 7:29
  • 1
    Do you know about and have read the documentation about String#printf and MessageFormat already? Commented Oct 3, 2012 at 7:30
  • Thanks Javir, can you post this as answer so that I can close this..using printf I am able to acheive this Commented Oct 3, 2012 at 7:38
  • @TheLearner IMHO stackoverflow is about covering non trivial programming problems, not about providing implementations for Fizz Buzz programming tasks. BTW#1: There are questions on text formatting, specifically on columns: stackoverflow.com/questions/1519260/java-text-formatting . BTW#2 I meant String#format (no such printf method in String) Commented Oct 3, 2012 at 7:49

6 Answers 6

2
String[] array = new String[]{"xxxxx", "yyy", "aaaaaa", "rrrrrrrr"};
String[] array1 = new String[]{"222", "44", "55", "77"};
for (int i = 0; i < array.length; i++) {
    System.out.println(String.format("%-9s===%4s", array[i], array1[i]));
}

Here is Formatter documentation

Sign up to request clarification or add additional context in comments.

Comments

0
int[] array1 = {222,44,55,77};
String[] array ={"xxxxx", "yyy", "aaaaaa", "rrrrrrrr"};

    for(int i=0; i<array.length; i++)
    System.out.println(array[i] + " ===== " + array1[i]);

Comments

0

Iterate on a loop.

String[] array = {"xxxxx","yyy","aaaaaa","rrrrrrrr"};
        String[] array1 = {" 222","44","55","77"};
        for (int i = 0; i < array1.length && i<array.length; i++) {
            System.out.println(array[i]+"==="+array1[i]);
        }

Comments

0
    String array1[] = {"xxxxx","yyy","aaaaaa"};
    int[] array2[] = {222,44,55,77};
    if(array1.length==array2.length) { 
     for(int i=0; i<array1.length; i++) {
    system.out.println(array1[i]+ "==="+array2[i]);
   }

Comments

0

Assuming you typed wrong your println call (one '+' is missing), if you want to tab the output, use \t.

Also, you'll need to use a loop to print all the array:

for(int i=0;i<array.length;i++){
    System.out.println(array[i] + "\t===\t" + array1[i]);
}

Comments

0

You need to format your output using printf method..

     String[] array1 = {"xxxxx","yyy","aaaaaa", "zzz"};
     int[] array2 = {222 ,44 ,55 ,77};

     if(array1.length==array2.length) { 

         for(int i=0; i<array1.length; i++) {
             System.out.printf("%-7s===%4d", array1[i], array2[i]);
             System.out.println();
         }
    }

Using %-7s left justifies your first string.. with a width of 7
Using %4d right justifies your last value.. With a width of 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.