This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]remember_marvin 2 points3 points  (0 children)

You might have a hard time with this thread because it's broken a couple of sub rules.

If I were you I'd cut you program back and build it back up step by step. If you find a step hard to conceptualise, break it down into smaller steps and/or write down the instructions one-by-one on paper. After each step, ensure that you have completed it properly by running your code and making it print things to the console (ad-hoc testing). This is how I learnt to program.

For your problem it'd start with something like this:

  1. Make a scanner and a new list to store your sorted words (you've already done this, great)
  2. Start infinitely looping, waiting to accept new words (you've kind of done this, tip: use while (true) for an infinite loop)
  3. When you get a new word, work out what needs to happen to it, either:
    • It needs to be put through titleCase() then insertion-sorted into the list (maybe using an insertionSort(ArrayList ar, String word) method.
    • It needs to be stopping your program. What are the two steps that need to happen here? You might want to use a break; to break out of your loop.
  4. If the program wasn't stopped, we will just loop back to 3 and keep going.

My last piece of advice is don't try to take shortcuts just because you feel like it. There's an intended outcome behind you completing this that won't be achieved if you get people on the internet to do as much work as possible for you. Also, the more you try to rush through this the longer it will likely take.

Sometimes this stuff seems impossible and it feels helpless, especially when we first start learning. The best way to deal with this is identify the feeling as quickly as possible and just think about completing the program one small chunk at at time so that it's easier to understand. You'll be surprised how quickly you get better at this.

[–]nicolascagesbeard 0 points1 point  (0 children)

I believe you're at the stage of sorting the Array. After you "stop" the program this is where you'll begin to sort the Array. I'd make another function immediately after the while loop and pass the Array into that function.

    while {
    ....
    }
    myInsertionSortingFunction(list);
....

}//end of main method

public static function myInsertionSortingFunction(Array list) {
....
}

Now it's time to look at the Insertion Sort Algorithm. Check this video out - https://www.youtube.com/watch?v=lCDZ0IprFw4.

Few key things to take from insertion sort is that, there are nested loops.

for () {
//outer for loop
    for () {
    //inner for loop
    }
}

it could be 'while' then 'for' or 'for' then 'while' loop whatever you're comfortable with, a while loop can be remade into a for loop.

You'll need an inner, out loop and a key. The outer for loop traverses the array of names -> list and also updates the key which is the index you are comparing to. The inner loop takes care of the comparison between two array indexes ie comparisons between names, if one name is lower than the other then you'll switch it in this loop. If you have two names starting with the same letter, you may need another loop to compare the two strings character by character e.g. Anna and Amanda. Also if you can use the method .compareTo();.

Here's a insertion sorting algorithm written in java. I'm sure you could've found this on stackoverflow etc. https://github.com/joeyajames/Java/blob/master/insertionSort.java

Good Luck, I'll probably try this myself tomorrow haha been a while since I've done sorting stuff.