Solved

Java Array Table Index | Days | Temp

Posted on 2012-12-28
3
320 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
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
Comment Utility
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
Comment Utility
Thanks a lot.

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

Many thanks
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now