1

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.

Tyler R
  • 25
  • 4
  • I added a print statement to check what words is printing just to see.. and it seems to be reading the array of words correctly, but still getting an error – Tyler R Feb 14 '19 at 02:51
  • 2
    What did you find out by stepping through your code with your debugger? – Dawood ibn Kareem Feb 14 '19 at 02:58
  • 1
    This would be easier to diagnose if we could see your input data – CryptoFool Feb 14 '19 at 03:01
  • The output would suggest fairly conclusively that `posCount` is greater than `0`. Time to step through the preceding loop in your debugger and see what happens. See what `words` contains, what `words[posi]` is, etc. – David Feb 14 '19 at 03:02
  • This is where the novice in me comes in. I can't ever seem to know where to put that break point to test correctly. I am actually doing that now, but I can't make sense of it. I will try this with some basic code and learn what I am doing. I apologize. I was hoping for something that was pretty obvious I might of missed that was an issue – Tyler R Feb 14 '19 at 03:03
  • 1
    input data can be any words you want. for "MyFile" I am using Gator, boy, whoa...."myOtherFile" has Cat, Dog, Boy... my "yetAnotherFile" just has Zebra, Apple, Dog, Lima. Does that help? – Tyler R Feb 14 '19 at 03:04
  • What do you think happens to those spaces when you split on `,`? And is space an upper case letter? – Dawood ibn Kareem Feb 14 '19 at 03:05
  • No, so do I need to add another trim somewhere? I am removing the spaces in my files to test what happens when there is no space. – Tyler R Feb 14 '19 at 03:08
  • I would recommend splitting on `",\\s*"` instead - include any spaces as part of what you split on. – Dawood ibn Kareem Feb 14 '19 at 03:09
  • That did fix the issue, but the sorting isn't going through now. Thanks for the help on that part ! – Tyler R Feb 14 '19 at 03:11
  • You're trying to sort them in reverse order of their first character? That's what your code looks like to me, at first glance. `i` comes before `k`, and you're swapping entries if the first letter of the word at `i` is before the first letter of the word at `k`. – Dawood ibn Kareem Feb 14 '19 at 03:16
  • You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Feb 14 '19 at 03:20

2 Answers2

1

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)
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Thanks! That works. What exactly is that say? to account for a space after a comma? That's neat. Just isn't sorting yet. – Tyler R Feb 14 '19 at 03:17
  • Presumbly OP doesn't want to split where there's no comma. I would recommend the regular expression in my comment under the question, not this one. – Dawood ibn Kareem Feb 14 '19 at 03:19
  • Both worked fine. What would stop it from sorting now? Could to many brackets be a problem? – Tyler R Feb 14 '19 at 03:21
  • Good point. But words are words, so maybe that is what he wants if he happens to leave out a comma. His call. Either way, we've pointed him in the right direction. – CryptoFool Feb 14 '19 at 03:21
  • Also, I need to sort it via insertion sort. From what I understand that is what I did. I wish I could just use arrays.sort, but we aren't allowed to use built in libraries – Tyler R Feb 14 '19 at 03:22
  • I do have the sort written. It worked in class when we were sorting integers. Isn't Collections a library? I appreciate it though! I really wish we could use those. I know about 4 other people who would benefit from it because they can't get the insertion sort down ha – Tyler R Feb 14 '19 at 03:27
  • Did it. It was literally just swapping around my < sign to > – Tyler R Feb 14 '19 at 03:29
  • Appreciate all the help! – Tyler R Feb 14 '19 at 03:30
  • You might want to do some tests where you have several words beginning with the same letter, to ensure that the sort is doing what you think. – Dawood ibn Kareem Feb 14 '19 at 03:30
  • Oh you're right.. Doesn't sort past the first Letter. Like still put Boy before Bat.. Hmm that is a whole other demon I need to think about.. DOn't think he will test it that way, but thanks for that suggestion – Tyler R Feb 14 '19 at 03:33
  • Yes, but if I noticed that by looking at your code, then so might your professor. – Dawood ibn Kareem Feb 14 '19 at 03:36
  • If you don't have the requirement of having to do your comparison character by character, look at String.compareTo(). It does <, ==, or > for a whole string: "words[i].compareTo(words[k]) < 0" means words[i] < words[k]. There's also a version that ignores case. – CryptoFool Feb 14 '19 at 03:41
  • that's convenient. Is that as simple as putting that statement "words[i].compareTo(words[k])" in an if statement? – Tyler R Feb 14 '19 at 03:44
  • yes, just like I show: if (words[i].compareTo(words[k]) < 0) should do it for you – CryptoFool Feb 14 '19 at 03:46
  • [Read the Javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#compareTo-java.lang.String-) and see if you can figure it out for yourself. – Dawood ibn Kareem Feb 14 '19 at 03:46
  • The answer to 99% of Java problems is either "step through with a debugger" or "read the Javadoc". Learn to do both. Yesterday, preferably. – Dawood ibn Kareem Feb 14 '19 at 03:46
  • Appreciate it Steve!. You're right Dawood. I should get better with debugger, but reading has never been how I learn. I learn by doing. Aka why I was good at my job in the military . – Tyler R Feb 14 '19 at 03:51
0

Maybe you are putting a space before each word and this is what you are trying to check if it is upper-case...

Juan Franco
  • 199
  • 1
  • 7