Solved

Java Array Table Index | Days | Temp

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simplest php form 3 63
Beginner to Unreal Engine 4 5 61
table example 4 24
How to get time difference in minutes and seconds only between 2 dates 2 26
This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

867 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

15 Experts available now in Live!

Get 1:1 Help Now