简体   繁体   English

JAVA:印刷机打印奇怪的符号?

[英]JAVA: printwriter printing weird symbols?

I am working on a project that reads data from a file, puts data into a linked node, sorts the linked node using bubble sort, and prints to an output file.我正在处理一个从文件读取数据、将数据放入链接节点、使用冒泡排序对链接节点进行排序并打印到输出文件的项目。 Each time a swap/exchange/pass is made in bubble sort I am supposed to count it and print out how many swaps/exchanges/passes it took to sort any given list.每次在冒泡排序中进行交换/交换/传递时,我应该计算它并打印出对任何给定列表进行排序所需的交换/交换/传递次数。 My code seems to be working fine and it will print to my screen just fine but in my output file instead of numbers I get weird symbols like "Ⴖ"??我的代码似乎工作正常,它会很好地打印到我的屏幕上,但是在我的输出文件而不是数字中,我得到了像“Ⴖ”这样的奇怪符号?? Thanks in advance!!提前致谢!!

package assignment1;

import java.io.PrintWriter;

public class Sorting {

    private int comparisons;
    private int exchanges;
    private int totalC;
    private int totalE;
    private int totalP;
    private long time;
    private int pass;
    private static PrintWriter printWriter;
    private static int n; //size list 

    public Sorting(int size, PrintWriter pw) {
        n = size;
        printWriter = pw;
    }

}

The method that sorts my list.对我的列表进行排序的方法。 (it has been sorting successfully - no issues there. However, my variable updating or my calls to my display method must be wonky. It is supposed to print after each 'pass' through the method and print how many comparisons and exchanges it made each pass. Then I am supposed to print the total amount of everything at the end. This is the part that isn't working. When I tried printing the variables for each iteration I got a very long string of random symbols and letters. (它已成功排序 - 那里没有问题。但是,我的变量更新或我对显示方法的调用必须是不稳定的。它应该在每次“通过”该方法后打印,并打印它每次进行的比较和交换次数通过。然后我应该在最后打印所有内容的总量。这是不起作用的部分。当我尝试为每次迭代打印变量时,我得到了一长串随机符号和字母。

public void bubbleSort(LinkedNode head) {

    LinkedNode previous; 
    LinkedNode current; 
    LinkedNode next; 
    comparisons = 0;
    exchanges = 0;
    time = 0;
    pass = 0;
    totalC = 0;
    totalE = 0;
    totalP = 0;

    boolean isSorted = true;

    //if list is empty or only 1 item is in list -> it is sorted
    if (head == null || head.getNext() == null) {
        return;
    }

    long start = System.currentTimeMillis(); //begin count for bubbleSort

    while(isSorted) {
        comparisons = 0;
        exchanges = 0;
        previous = null;
        current = head;
        next = head.getNext();
        isSorted = false;

        while(next != null) {
            comparisons ++; //increment counter for each comparison made

            if (current.getElement() > next.getElement()) {
                if (head == current) {
                    head = next;
                }
                else {
                    previous.setNext(next);
                }
                current.setNext(next.getNext());
                next.setNext(current);
                isSorted = true;
                current = next;
                exchanges++;    

            }
            previous = current;
            current = previous.getNext();
            next = current.getNext();
        }

        pass++; //increment counter for each run through
        totalP += pass;
        totalE += exchanges;
        totalC += comparisons;
    }

    long elapsedTimeMillis = System.currentTimeMillis()-start;
    time = elapsedTimeMillis;
}

The only part that outputs to my file correctly is the time variable.唯一正确输出到我的文件的部分是时间变量。 On my screen I get the number of passes to be 4,239 but in my output file I get the strange symbol.在我的屏幕上,我得到的传递次数为 4,239,但在我的输出文件中,我得到了奇怪的符号。

void bubbleSortDisplay(LinkedList myList, PrintWriter pw) {
    System.out.printf("time   pass    cmp     exch ");
    printWriter.write("time   pass    cmp     exch ");
    printWriter.write("\n----------------------------\n");

    System.out.printf("\n", time);
    printWriter.write(time + "ms     ");
    System.out.print(time + "ms     ");
    printWriter.write(totalP);
    System.out.print(totalP);
}

The problem is that the write(int c) method of PrintWriter interprets its argument as a character, so the zero gets written to the file as the invisible NUL character (numeric value 0) rather than the desired '0' character (numeric value 48).问题是PrintWriterwrite(int c)方法将其参数解释为字符,因此零被写入文件作为不可见的 NUL 字符(数值 0)而不是所需的“0”字符(数值 48 )。

Calling pw.print(ci) instead of pw.write(ci) in saveCI() should do the trick.pw.print(ci)调用pw.print(ci)而不是pw.write(ci) saveCI()应该可以解决问题。

Try to replace in your code in this way:尝试以这种方式替换您的代码:

printWriter.write(""+totalP);

(Concat totalP with an empty string) (用空字符串连接 totalP)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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