PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

[FIXED] How do you get parallel user input arrays to print in an aligned table in java?

 August 17, 2022     alignment, arrays, java, output, printing     No comments   

Issue

I am working on a supply status dashboard and need to get my outputs in an aligned table. All arrays are populated with user input. Currently I have success with each output printing but only in a single column.

Code in question:

        System.out.println();
        System.out.println("Current status of all items being tracked:");
        for (int index = 0; index < items.length; index++)
            System.out.println(items[index] + " ");
        for (int index = 0; index < items.length; index++)
            System.out.println(supplyOnHand[index] + " ");
        for (int index = 0; index < items.length; index++)
            System.out.println(last24HourUsage[index] + " ");

Current output:

Current status of all items being tracked:
masks 
hoods 
Wipes 
100 
1000 
10000 
25 
250 
2500

Output wanted:

Current status of all items being tracked:
Masks     100     25
Hoods     1000    250
Wipes     10000   2500

I am unsure of how to exactly phrase the question and have come up empty.

Any specific help or general guidance to the required information would be helpful.

Thanks!


Solution

If you want to print in one line and your arrays are parallel you can just use one for loop and then concatenate all your values

for (int index = 0; index < items.length; index++)
    System.out.println(items[index] + " " + supplyOnHand[index] + " " + last24HourUsage[index]);

If you want to align your text in columns you need to get the longest values in every array and then append multiple spaces depending on the amount of characters that are missing from your values



Answered By - AmNotSmort
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing