简体   繁体   中英

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.

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).

Calling pw.print(ci) instead of pw.write(ci) in saveCI() should do the trick.

Try to replace in your code in this way:

printWriter.write(""+totalP);

(Concat totalP with an empty string)

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