【问题标题】:How can I print my array in a certain way? [closed]如何以某种方式打印我的数组? [关闭]
【发布时间】:2013-12-06 13:49:34
【问题描述】:

所以我想在列表中打印我的数组。看起来像这样。

      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));
 }
}

【问题讨论】:

  • 你正确的输出方式是什么?
  • 我在帖子的顶部有我希望它看起来像的正确结果。

标签: java arrays


【解决方案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();
    }
}

【讨论】:

  • 如果你想要更少的空间,只需删除\t
  • 是的,现在我只需要将它修复到它们都打印在同一个位置的位置,每个单词的结尾应该在 Word: 上的冒号结尾,每个数字应该在 Count 上的冒号结尾:
  • 数字 10 表示该行中没有多少字符。它用于对齐。因此,如果任何单词的字符数超过 10 个,格式就会关闭。如果你有超过10个字符的单词,你需要把所有的10s都改成最长单词的长度
  • 啊,我明白了,谢谢,所以这是一本书,如果我想添加另外两本书,我是否必须用完全相同的东西制作另外两种方法,只是名称不同?
  • 如果您将它们放在不同的数组中,那么是的,您需要对每本书执行相同的操作。
【解决方案2】:

当你在做的时候

System.out.println();

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

尝试使用

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

看看这个page,它解释了使用 System.printf 来对齐列。

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

【讨论】:

  • 这并不能真正解决列问题。
【解决方案3】:

您的数组有 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]);

        }

【讨论】:

    【解决方案4】:

    您必须正确对齐您的列。根据this stackoverflow question,列左对齐,前面有负号,右对齐,没有。您希望您的打印语句看起来像这样:

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

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

    另外:有人提到你可以通过使用print而不是println来避免在每条语句的末尾打印一行。

    【讨论】:

    • 什么是someStream?感谢您的帮助。
    • @user2934545 抱歉,我太专注于间距问题,我错过了输出语句在这个级别不是很清楚的事实。这可能更清楚,使用 System.out.printf 和 println 代替。
    • @user2310289 Lol 抱歉,星际法则禁止我直接引用其他帖子,因为帖子是临时的。
    【解决方案5】:
    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));
        }
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2013-08-02
      相关资源
      最近更新 更多