简体   繁体   中英

Just need a little help debugging my code

I have 3 files, "MyFile" , "myOtherFile" , "yetAnotherFile" that my code will be drawing words from to put them in an array, check to see if they start with an uppercase, and if they do, it will also sort them alphabetically. all 3 have 3 or more words, one has only one word that starts with a lowercase so I can test that invalid input print line

I am somehow getting all 3 to print the invalid line

Added a counter so if counter > 0 it then does the print statement

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.*;

   public class StringSorter {
    private String inputFileName;
    //private String line;
    public StringSorter(String fileName) {
        inputFileName = fileName;
        }

    public void sortStrings() throws IOException {
        FileReader input = new FileReader(inputFileName);
        BufferedReader myReader = new BufferedReader(input);
        String line, data = "";

        String[] words;

        int posCount = 0;


        while ((line = myReader.readLine()) != null)
            data += line;
        words = data.split(",");

        for(int posi = 0; posi < words.length; posi++) {
            if(!Character.isUpperCase(words[posi].charAt(0))) {
            posCount++;
            }
        }
        if(posCount > 0) {
            System.out.print("Invalid input. Word found which does not     start with an uppercase letter.");
        }
        else {
        for (int k = 0; k < words.length; k++) {
            for (int i = k - 1; i >= 0; i--) {
                if (words[i].charAt(0) < words[k].charAt(0)) {

                    String temp = words[k];
                    words[k] = words[i];
                    words[i] = temp;
                    k = i;
                }
        }

        }           
                    for(int print = 0; print < words.length - 1; print++){ 

                        System.out.print(words[print].trim() + ", ");
                }

                System.out.print(words[words.length-1]);

                        }
                     input.close();
                            myReader.close();
                    }

                }


     import java.io.*;
    public class TestStringSorter {

    public static void main(String[] args) throws IOException {
        StringSorter sorterA = new StringSorter("MyFile.txt");
        sorterA.sortStrings();

        StringSorter sorterB = new StringSorter("myOtherFile.txt");
        sorterB.sortStrings();

        StringSorter sorterC = new  StringSorter("yetAnotherFile.txt");
        sorterC.sortStrings();
    }

}

 Invalid input. Word found which does not start with an uppercase letter.

Invalid input. Word found which does not start with an uppercase letter. Invalid input. Word found which does not start with an uppercase letter.

I see what might be the problem. You're splitting on ',', but you have spaces after the comma. So you're going to have a "word" like " Dog", and if you test the first character of that, you're going to get a failure, because a space is not an uppercase letter.

Try splitting on:

words = data.split("[,\\s]+");

that would fix the problem with the spaces in the data.

I see another problem that will cause you to probably not get the results you expect. You're concatenating multiple lines together, but not putting anything between the lines, so the last word on one line is going to combine with the first word on the next. You probably want to put a "," between each line when you concatenate them together.

I guess you want to write your own sort. I'll leave that to you or others to debug. But you could just:

Arrays.sort(words)

也许您在每个单词之前放置一个空格,这就是您要检查它是否为大写的内容...

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