Solved

Java Array Table Index | Days | Temp

Posted on 2012-12-28
3
327 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

816 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

12 Experts available now in Live!

Get 1:1 Help Now