如何以某种方式打印我的阵列?

时间:2013-12-06 04:49:24

标签: java arrays

所以我想在列表中打印我的数组。那将是这样的。

      Word:        Count:
      Myths             2
         Of            15
  Babylonia            25

我似乎无法弄清楚如何以正确的方式打印它,这是我到目前为止的代码。感谢任何帮助,谢谢!

package program6;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;

public class Program6 {

static String[] stringArray = new String[100];
static int[] intArray = new int[100];
static String fileName = "myths.txt";
static int currentWordIndex = 0;

public static void main(String[] args) throws FileNotFoundException {

      Scanner input = new Scanner(new File(fileName));

      while (input.hasNext()){
        String word = input.next();
        boolean alreadyExists = false;
        for (int i = 0; i < currentWordIndex; i++) {

            if(stringArray[i].equals(word)){
                alreadyExists = true;
                intArray[i]++;
                break;
            }
        }


      if(!alreadyExists && currentWordIndex <100){
        stringArray[currentWordIndex] = word;
        intArray[currentWordIndex++] = 1;
      }
    }

    System.out.println("Myths of Babylonia and Assyria");
    System.out.println("Word:          Count:");
    System.out.println(Arrays.toString(stringArray));
    System.out.println();
    System.out.println(Arrays.toString(intArray));
 }
}

5 个答案:

答案 0 :(得分:1)

使用格式并使用循环

System.out.println("Myths of Babylonia and Assyria");
System.out.println("Word:\t\tCount:");
for (int i = 0; i < stringArray.lengthl i++){
    System.out.printf("%s\t\t%d\n", stringArray[i], intArray[i]);

}

使用正确对齐进行编辑

    System.out.println("Myths of Babylonia and Assyria");
    System.out.printf("%10s%10s\n", "Word", "Count");
    for (int i = 0; i < array1.length; i++) {
        System.out.printf("%10s%10d", array1[i], array2[i]);
        System.out.println();
    }

编辑:使用其他图书的方法

public static void main(String[] args) throws FileNotFoundException{

    printCounts("myth.txt", "Babylonia and Assyria");
    System.out.println();
    printCounts("someOther.txt", "Some Other Title");
    System.out.println();
    printCounts("another.txt", "Another Title");
    System.out.println();
}

public static void printCounts(String filename, String title) throws FileNotFoundException {

    String[] stringArray = new String[100];
    int[] intArray = new int[100];
    int currentWordIndex = 0;
    Scanner input = new Scanner(new File(filename));

    while (input.hasNext()) {
        String word = input.next();
        boolean alreadyExists = false;
        for (int i = 0; i < currentWordIndex; i++) {

            if (stringArray[i].equals(word)) {
                alreadyExists = true;
                intArray[i]++;
                break;
            }
        }

        if (!alreadyExists && currentWordIndex < 100) {
            stringArray[currentWordIndex] = word;
            intArray[currentWordIndex++] = 1;
        }
    }

    System.out.println(title);
    System.out.printf("%10s%10s\n", "Word", "Count");
    for (int i = 0; i < stringArray.length; i++) {
        System.out.printf("%10s%10d", stringArray[i], intArray[i]);
        System.out.println();
    }
}

答案 1 :(得分:1)

当你做的时候

System.out.println();

它实际上是在输出的末尾打印一个新行。

尝试使用

System.out.print("foo ");
System.out.println("bar");

请查看此page,其中解释了使用System.printf来对齐列。

System.out.printf( "%-15s %15s %n", heading1, heading2);

答案 2 :(得分:1)

您的阵列有100个元素,因此打印了大量零,使用Arrays.copyOf创建更小的阵列。
对于表格格式,请使用printf

所以你应该替换以下代码:

System.out.println("Word:          Count:");
System.out.println(Arrays.toString(stringArray));
System.out.println();
System.out.println(Arrays.toString(intArray));

使用:

        String[] stringArray2 = Arrays.copyOf(stringArray, totalWordCount);
        int[] intArray2 = Arrays.copyOf(intArray, totalWordCount);

        stringArray = null;
        intArray = null;


        System.out.println("Myths of Babylonia and Assyria");
        System.out.printf("\n%15s%15s", "Word:","Count:");
        for (int i = 0; i < stringArray2.length; i++){
            System.out.printf("\n%15s%15d", stringArray2[i], intArray2[i]);

        }

答案 3 :(得分:0)

您必须正确对齐您的列。根据{{​​3}},列左对齐,前面有一个负号,右边没有。您希望print语句看起来像这样:

System.out.printf("%60s %3d", stringArray[i], intarray[i]));

您可以通过这种方式改变列宽。

此外:有人提到您可以使用print代替println来避免在每个语句末尾打印一行。

答案 4 :(得分:0)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Program6 {

static String fileName = "myths.txt";
private static Scanner input;

public static void main(String[] args) throws FileNotFoundException {

    input = new Scanner(new File(fileName));

    Map<String, Integer> map = new HashMap<String, Integer>();

    while (input.hasNext()){
        String word = input.next();
        if ( map.containsKey(word) ){
            int temp = map.get(word) + 1;
            map.put(word, temp);
        } else {
            map.put(word, 1);
        }
    }

    // get all the set of keys
    Set<String> keys = map.keySet();

    // iterate through the key set and display key and values
    System.out.printf("%-10s\t\t%s\n", "Word", "Count:");
    for (String key : keys) {
        System.out.printf("%-10s\t\t%d\n", key, map.get(key));
    }

}
}