Solved

Java Array Table Index | Days | Temp

Posted on 2012-12-28
3
333 Views
Last Modified: 2012-12-28
Hello

I am a Java newbie and I am trying to answer the following question. However my Temp column is not lining up evenly

Index        Days              Temp
0             Monday            12
1             Tuesday            15
2             Wednesday      9
3             Thursday            12
4             Friday            8
5             Saturday            14
6             Sunday            13

public class ArrayDays
{
	public static void main (String [] args)
	{
	
		System.out.println ("Index \t Days \t\t Temp");
		
		String arrayDays[]= {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
		int arrayTemps[]= {12, 15, 9, 12, 8, 14, 13};
		
		for(int i = 0; i <arrayDays.length; i ++)
		{
	
		}
		for(int i = 0; i <arrayTemps.length; i ++)
		{
			System.out.println(i + "\t " +arrayDays[i]+"\t\t"+ arrayTemps[i]);
		}

		
	}
}

Open in new window

0
Comment
Question by:Kevin_Coors
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 38727906
printf will do it

public class ArrayDays {
    public static void main(String[] args) {
        String[] arrayDays = {
                "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
                "Saturday", "Sunday"
            };
        int[] arrayTemps = { 12, 15, 9, 12, 8, 14, 13 };

        System.out.printf("%-6s%-10s%4s\n", "Index", "Days", "Temp");

        for (int i = 0; i < arrayDays.length; i++) {
            System.out.printf("%-6d%-10s%4d\n", i, arrayDays[i], arrayTemps[i]);
        }
    }
}

Open in new window

0
 

Author Comment

by:Kevin_Coors
ID: 38728106
Thanks a lot.

However is there a way to output the result without printf please?

Many thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38728356
However is there a way to output the result without printf please?
Not really. You'd have to do all your padding explicitly - good luck ;)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Simple Linear Regression

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question