简体   繁体   中英

Printing an Array in reverse order, printf alignment issue

I have an array that is sorted into columns with 10 per column. I am trying to get the reverse of that but the sorting is off, and I'm not sure why. Can someone help me take a look? Thank you!

import java.util.Scanner;
import java.io.*;
import java.util.Arrays;

public class Pham_ArrayProcessing
{

    private static int[] fileArray;
    private static int arraySize;

// Main method

    public static void main(String[] args) throws IOException
    {
        inputData();
        printArray(fileArray);
        reverseArray(fileArray);
    }

// This method will ask the user for a file and read in the file location.
// If =/= exist, gives error msg and terminates program.
// Otherwise, creates array and store integers from file in the array.

    public static int[] inputData() throws IOException
    {
        System.out.println("");
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please enter input file: ");
        String userFilename = keyboard.nextLine();

        File userFile = new File(userFilename);
        Scanner inputFile = new Scanner(userFile);

        arraySize = inputFile.nextInt();
        fileArray = new int[arraySize];

        if(userFile.exists())
        {   
            for (int i = 0; i < arraySize; i++)
            {
                fileArray[i] = inputFile.nextInt();
            }
        }
        else
        {
            System.out.println(userFilename + " was not found.");
            System.exit(0);     
        }
        inputFile.close();
        return fileArray;
    }

// This method prints out the passed in array to the screen with 10 integers per line.
// Uses the printf method to align the data in columns.

    public static void printArray(int[] array)
    {
        System.out.println("");
        System.out.println("Printed Array: ");
        for (int j = 0; j < arraySize; j++)
        {
            System.out.printf("%5s", fileArray[j]);
            if (j % 10 == 9)
                System.out.println("");
        }
        System.out.println("");
    }   

// This method prints out the passed in array in reverse order to the screen.
// Uses the printf method to align the data in columns.

    public static void reverseArray(int[] array)
    {
        System.out.println("");
        System.out.println("Reversed Array: ");
        for (int j = arraySize - 1; j >= 0; j--)
        {
            System.out.printf("%5s", fileArray[j]);
            if (j % 10 == 9)
                System.out.println("");
        }
        System.out.println("");
    }

My output is:

c:\Users\name\Desktop>java Pham_ArrayProcessing

Please enter input file: inputFile5.dat

Printed Array:
  -5  -77   43   64  -82   -9    2  -28  -36  -32
  86  -49   10  -14  -42  -36  -86  -22   -6  -12
  45   28   48   60    0

Reversed Array:
   0   60   48   28   45  -12
  -6  -22  -86  -36  -42  -14   10  -49   86  -32
 -36  -28    2   -9  -82   64   43  -77   -5

I would like 10 integers per line.

In the reverse print function Before:

  if (j % 10 == 9)
        System.out.println("");

Updated:

  if ((arraySize - j) % 10 == 9)
        System.out.println("");
 if (j % 10 == 9)
                System.out.println("");

This line causes your problem. For example if your array had 19 entrys it would print the last entry then 19 % 10 == 9 would result as true. This would cause it to insert a newline right after.

Your if statement assumes it is counting the number of printed numbers so far. In the case of the reverse array, j isn't the number of elements printed so far, but rather the index of the array. The clearest way to write a fixed version would be to introduce a new variable that is the count of printed values similar to this.

       int n = 0;
       for (int j = arraySize - 1; j >= 0; j--)
        {
            System.out.printf("%5s", fileArray[j]);
            if (n++ % 10 == 9)
                System.out.println("");
        }

though you could just do some tricks with j to achieve the same result.

That should work, done some refactor also, don´t need to have an array size int, you can calculate actual size, and really, if you pass by param the array, you should print the array[j] of that array.

  public static void reverseArray(int[] array)
        {
    System.out.println("");

    System.out.println("Reversed Array: ");
    for (int j = array.size() - 1; j >= 0; j--)
    {
        System.out.printf("%5s", array[j]);
        if ((fileArray.size()-1 - j) % 10 == 9){
        System.out.println("");
    }
    }

But maybe a better way:

printArray(Collections.reverse(fileArray)) 

and edit the printArray method for print the array that you passed as param:

System.out.printf("%5s", array[j]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM