Aligning Issues in the Output
Hi.
I'm having trouble aligning my information. The output leaves the columns below each other and not next to each other. Here's my code (sorry, not sure how to put code tags around it):
Code java:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Hurricanes2
{
public static void main(String[] args) throws IOException
{
Scanner inFile = new Scanner(new File("hurcdata2.txt"));
int numLines = 0;
int knots = 0;
while(inFile.hasNextLine())
{
inFile.nextLine();
numLines++;
}
inFile.close();
inFile = new Scanner(new File("hurcdata2.txt"));
int [] years = new int [numLines];
String [] name = new String [numLines];
int [] category = new int [numLines];
int [] pressure = new int [numLines];
double [] wind = new double [numLines];
String [] month = new String [numLines];
System.out.printf("%45s\n", "Hurricanes 1980 - 2006");
System.out.println();
System.out.println("Year Hurricane Catergory Pressure (mb) Wind Speed (mph)");
System.out.println("=======================================================================");
int i=0;
while(inFile.hasNext())
{
years[i] = inFile.nextInt();
month[i] = inFile.next();
pressure[i] = inFile.nextInt();
wind[i] = inFile.nextDouble();
name[i] = inFile.next();
i++;
}
inFile.close();
for(int y: years)
{
System.out.println(y);
}
for(String n: name)
{
System.out.printf("%18s\n", n);
}
for(double w: wind)
{
System.out.printf("%25.0f\n", w);
}
for(int p: pressure)
{
System.out.printf("%18d\n", p);
}
}
}
Re: Aligning Issues in the Output
First, many of your print commands also issue new lines ('\n' - System.out.println will also do the same automatically but System.out.print just prints the contents), which will result in a single column output. So first you should remove those until you actually need a new line. Second, if you wish the columns to line up perfectly you may need to write some code which determines the correct spacing between each columns, outputs the value and the correct number of spaces between each output. For example, if each column has 10 spaces and you wish to print out the value '18.0', you could
Code java:
//right aligns the output text to 10 spaces
String st = "18.0":
for ( int i = st.length(); i < 10; i++ ){
System.out.print(" ");
}
System.out.print(st);
Re: Aligning Issues in the Output
Another way to get your output to be in columns is to use tab (\t) characters before the String for the next column.
The results will depend on what program is displaying the tabbed output. Not all programs honor tabbing.
Re: Aligning Issues in the Output
I tried both methods, but this is what ends up happening:
http://i103.photobucket.com/albums/m...allProgram.jpg
The year column is displayed on one side and the hurricane name column is displayed below the year column. The spacing between them is correct, but I'm not sure how to get the columns next to each other.
Re: Aligning Issues in the Output
It would depend on what order you print them in.
The above shows you printed all of the numbers first and then printed all of the names and then the next ones.
Change the loop to output one item from each array on the same row/line. You'll have to use the old style for loop
Re: Aligning Issues in the Output
i would create a class Hurricane that holds all your hurricane fields and then inside the main-method i would declare an list of arrays like ArrayList<Hurricane) hurricanes = new ArrayList<Hurricane>(); each time your read a hurricane record assign the data to the fields and then use the add-method of the ArrayList to add the record in your collection. the ArrayList has also a method size() that returns the number of elements in the list. so you don't have to iterate twice over your data file. the class Hurricane could also overwrite the toString method that returns your format string. ok, this is up to you.
i hope, this piece of code give you an idea how you could resolve your initial question:
Code :
for (int i = 0; i < years.length; i++) {
System.out.printf("%1d %13s %5s %14d %18.0f\n", years[i], name[i],
category[i], pressure[i], wind[i]);
}
i used a different approach to print all your elements, assuming that all years have all datas needed. if you change your data structure to an ArrayList then also the loop must change. have fun.