我的anagram程序不起作用

时间:2015-02-27 21:34:43

标签: java

由于某种原因,当我运行这个程序时,它不起作用...... 假设从包含几个单词的文本文件中读取,然后将这些单词的字谜打印成新文件,将每个单词及其字谜打印成一行,然后将下一个单词及其字谜打印到下一行。

示例文本输入文件包含

" hpesvy

wounxppzu

xznoug

ehsypv

zpwuonxpu

xrqryptcb

uzngxo

gzuonx

ysepvh

uozgnx "

Java Ass2 inputfile.txt output

我收到错误消息" null"

并且所有编译都很好。我不知道发生了什么事,有人可以帮忙吗?

代码有两个文件。

import java.io.*;
import java.io.FileWriter;
import java.util.*;


public class Ass2 {
    public static void main(String[] args) {

        if (args.length == 2){
            String inFile = args[0];
            String outFile = args[1];

            try{
                FileReader fr = new FileReader(inFile);
                LineNumberReader lnr = new LineNumberReader(fr);

                int linenumber = 0;

                while (lnr.readLine() != null){
                    linenumber++;
                }
                FileInputStream fstream = new FileInputStream(inFile);
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));

                String strLine;
                String [] array = new String[linenumber];
                //Read File line by line
                int i = 0;
                while ((strLine = br.readLine()) != null){
                    array[i] = strLine;
                    i++;
                }
                in.close();
                //String[] sorted = InsertionSort(array);
                //fileWrite(sorted, outFile);
                isAnagram(array);
            }
            catch(Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
        }
        else{
            System.out.println("I need 2 arguments");   
            }
    }

    private static void fileWrite(String[] array, String outFile) {
        BufferedWriter filewrite = null;
        try{
            filewrite = new BufferedWriter( new FileWriter(outFile));
            filewrite.newLine();
            for(int i=0; i<array.length; i++){
                filewrite.write(array[i]);
                filewrite.newLine();
            }
            filewrite.close( );
            }
        catch ( IOException e){}        
    }

    public static char[] InsertionSort(char[] array){
        //Insertion Sort algorithm from class
        int j;
        for(int i=1; i<array.length; i++){
            char tmp = array[i];
            for(j=i; j>0 && tmp<array[j-1]; j--){
                array[j] = array[j-1];
            }
            array[j] = tmp; 
        }
        return array;
    }

    public static void isAnagram(String [] sorted){
        Node[] linkedListArray = new Node[sorted.length];
        String tmp1 = "";
        String tmp2 = "";
        String [] myarray = new String [sorted.length];

        for (int i = 0; i < sorted.length-1; i++) {
            for(int a=i+1; a<sorted.length; a++){
            tmp1 = sorted[i];
            tmp2 = myarray[a];

            if (tmp1.length() == tmp2.length()){
                 char [] a1 = tmp1.toCharArray();
                 a1 = InsertionSort(a1);
                 char [] a2 = tmp2.toCharArray();
                 a2 = InsertionSort(a2);
                 int j = 0;
                 boolean isAnan = true;
                 while (j<a1.length && isAnan == true){
                     if (a1[j] != a2[j]){
                         System.out.println(tmp1+ " is not An "+tmp2 );
                         Node newLink = new Node(tmp1);
                         linkedListArray[j] = newLink;

                    }
                     j++;
                 } 
            }
        }
    }
    }
    }

public class Node {
        public String item;
        public Node next;
        public Node(String tmp1){
            this.item = tmp1;
        }
    }

    class InsertionSort{
        private Node head;
        public InsertionSort(){
            head = null;
        }
        public InsertionSort(Node[] linkedListArray){
            head = null;
            for(int j=0; j<linkedListArray.length; j++)
                insert( linkedListArray[j] );
        }
        public void insert(Node newNode){
            Node previous = null;
            Node current = head;
        while(current != null && current.item.compareTo(newNode.item)>0){
            previous = current;
            current = current.next;
        }
        if(previous==null)
            head = newNode;
        else
            previous.next = newNode;
            newNode.next = current;
        }
        public Node nextNode(){
            Node temp = head;
            head = head.next;
            return temp;
        }
    }

2 个答案:

答案 0 :(得分:3)

兄弟,你犯了一个主要的罪。

    catch (IOException e) {
    }

这完全隐藏了代码出错的证据。你需要做的第一件事是改变它:

    catch (IOException e) {
        e.printStackTrace();
    }

接下来的事情错了,你正在处理这样的例外:

    System.err.println("Error: " + e.getMessage());

这就是您的消息(Error: null)的来源,因为NullPointerException通常没有消息。所以,你需要做的第二件事是将它改为同一件事e.printStackTrace();

然后您将获得有关出错的完整信息。

这绝不是关于Java中正确异常处理的综合教程。我只是在指导您调试所需的最基本信息。

事实上,对于你正在做的事情,你真的不应该抓住任何例外。您应该删除try..catch块,并将main方法中的任何异常抛出:

public static void main(String[] args) throws Exception

答案 1 :(得分:2)

您的错误在isAnagram方法中。请参阅以下摘录:

    String[] myarray = new String[sorted.length];

    for (int i = 0; i < sorted.length - 1; i++) {
        for (int a = i + 1; a < sorted.length; a++) {
            tmp1 = sorted[i];
            tmp2 = myarray[a];

            if (tmp1.length() == tmp2.length()) {

myarray是一个仅包含null值的数组。因此tmp2也将是null,因此tmp2.length()会抛出NullPointerException。