0

I'm trying to write a code where it takes words from a text file, and puts each word in canonical order, and when run prints the original word next to its canonical form like this:

coding cdgino games aegms

All I have so far is this:

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

public class CanonicalWords
{
    public static void main(String[] args) throws Exception 
    {
        if (args.length<1)
        {
            System.out.println("You must provide an input file.");
            system.exit(0);
        }   
        String infileName = args[0];

        BufferedRead infile = new BufferedReader(new FileReader(infileName));

        while(infile.ready())
        {
            //arraylist.add(infile.readLine())
        }

        //sort arraylist


        for (int i=0;i<arrayList.size;i++)
        {

        }

    }
    static String canonical(String word)
    {
        char[] canonicalWord = word.toCharArray();
        Arrays.sort(canonicalWord);
        String cWord = new String(canonicalWord);
        return cWord; 
    }
}

Please let me know if you need clarification on anything I have writen. I do not know how to take these words to put them into canonical form.

Right now there is no real output, it doesn't even compile. I'm just very confused. If someone could help me to understand what is the basic formula (if there is one) to put words into canonical form and do what I stated above that'd be wonderful, but I understand that what I'm asking may come off as a bit confusing. Thank you.

4

1 回答 1

0

首先,从这段代码的外观来看:

BufferedRead infile = new BufferedReader(new FileReader(infileName));

应该是这样的:

BufferedReader infile = new BufferedReader(new FileReader(infileName));

请注意您完全正确地拼写;变量名,它们是数据类型!


还有一点需要注意的是,方法:

static String canonical(String word)

没有被调用。尝试在 main 方法中访问它。

于 2018-02-10T04:27:47.430 回答