-3

When I run my program, this is the output:

run:
70.3   70.8   73.8   77.0   80.7   83.4   84.5   84.4   83.4   80.2   76.3   72.0   
69 67 66 64 66 69 67 67 70 69 69 70BUILD SUCCESSFUL (total time: 0 seconds)

I want my program to display like this, where the numbers are aligned with equal spacing vertically and horizontally:

70.3   70.8   73.8   77.0   80.7   83.4   84.5   84.4   83.4   80.2   76.3   72.0   
69     67     66     64      66    69     67     67     70     69     69     70

The problem is, I don't really know how to use this using printf on an array that is declared a String that holds one line of the numbers mentioned above that gets these numbers from reading a .txt file. Below is a snippet of my code:

// create token1
    String token1 = "";

    // create Scanner inFile1
    Scanner inFile1 = new Scanner(new File
    ("/Users/timothylee/KeyWestTemp.txt")).
            useDelimiter(",\\s*");

    // create temps1
    List<String> temps1 = new LinkedList<String>();

    // while loop
    while(inFile1.hasNext()){

        // find next
        token1 = inFile1.next();

        // initialize temps1
        temps1.add(token1);
    }

    // close inFile1
    inFile1.close();

    // create array
    String[] tempsArray1 = temps1.toArray(new String[0]);

    // for-each loop
    for(String s : tempsArray1){

        // display s
        System.out.printf(s + "\n");
    }

    // create token2
    String token2 = "";

    // create Scanner inFile2
    Scanner inFile2 = new Scanner(new File
    ("/Users/timothylee/KeyWestHumid.txt")).
            useDelimiter(",\\s*");

    // create temps2
    List<String> temps2 = new LinkedList<String>();

    // while loop
    while(inFile2.hasNext()){

        // find next
        token2 = inFile2.next();

        // initialize temps2
        temps2.add(token2);
    }

    // close inFile2
    inFile2.close();

    // create array
    String[] tempsArray2 = temps2.toArray(new String[0]);

    // for-each loop
    for(String ss : tempsArray2){

        // display ss
        System.out.printf("15s", ss);
    }
4
  • 1
    Can you replace any spaces with a tab "\t"? Commented Nov 8, 2013 at 19:23
  • I must be having some wild deja-vu. I could have sworn a question just like this was asked before. I'll give you the benefit of the doubt, though. Commented Nov 8, 2013 at 19:23
  • Start by finding the length of the longest token... Commented Nov 8, 2013 at 19:24
  • 3
    @Makoto Surely you don't mean this question :) Commented Nov 8, 2013 at 19:25

1 Answer 1

1

Considering you want the spacing equal for both vertical and horizontal, replacing the spaces with a tab will not work since the rows contain values of different lengths. The second row of data has to be aware of how much space its "parent" data element took up. Thus, simply specifying a common width parameter to a format will also not work.

The only exception to this would be if you could guarantee the temperature values never exceeded 99.9 or fell below 10.0, then you could assume the total space required for the data would be 4 + the amount of spacing you desire.

Either way, the documentation for Formatter will provide the best starting point.

Assumptions:

  1. The length of the humidity value cannot exceed the length of the temperature value.
  2. The number of temperature and humidity data elements are equal in size.

See my implementation for an example of how to handle the context sensitive spacing.

    public static void main (String[] args) throws java.lang.Exception
    {
      final List<String> temperatures = Arrays.asList("70.3", "700.8", "73.8", "77.0", "80.7", "83.4", "84.5", "84.4", "83.4", "80.2");
      final List<String> humidity = Arrays.asList("69", "670", "66",  "64", "66", "69", "67", "67", "70", "69");
      final Integer[] context = new Integer[temperatures.size()];

      final Integer spacing = 2; //declare the spacing you desire

      final StringBuilder sb = new StringBuilder();
     for(int i =0; i < temperatures.size(); ++i) {
        final String str = temperatures.get(i);
        final Integer totalSpace = str.length() + spacing;
        final String formatted = String.format("%-"+ totalSpace +"s",  str);
        context[i] = totalSpace; 
        sb.append(formatted);
      }

      sb.append("\n");

     for(int i =0; i < humidity.size(); ++i)  {
        final String str = humidity.get(i);
        final String formatted = String.format("%-"+ context[i] +"s",  str);
        sb.append(formatted); 
      }

      System.out.println(sb.toString());

      }

Result:

Success time: 0.08 memory: 380160 signal:0

70.3  700.8  73.8  77.0  80.7  83.4  84.5  84.4  83.4  80.2  
69    670    66    64    66    69    67    67    70    69    
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.