?
Solved

Java Array Table Index | Days | Temp

Posted on 2012-12-28
3
Medium Priority
?
334 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 900 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

765 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