Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Alphabetical order

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Alphabetical order

    This is a program where the user types a bunch of words and then the program rearranges them into alphabetical order and then prints them.
    But this code keeps crashing on line 35 at "for (int count = 0; count < word1.length() ; count++)". Could anyone tell me whats wrong? (You can only input words that has the same length)

    The error it gives is : java.lang.StringIndexOutOfBoundsException
    it only gives the error when the user doesnt type it in alphabetical order. IDK why

    Here is the code:


    import java.awt.*;
    import hsa.Console;
     
    public class Alphabeticall_order
    {
        static Console c;           // The output console
        public static void main (String[] args)
        {
            c = new Console ();
            c.println ("How many words do you want to put in?");
            int slots = c.readInt ();
            String[] words = new String [slots];
            for (int y = 0 ; y < slots ; y++)
           {
                c.println ("Input the words with the same length");
                words [y] = c.readLine ();
           }
            for (int h = 0 ; h < slots ; h++)
            {
     
                for (int g = 0 ; g < slots - 1 ; g++)
                {
                    words [g] = swaplow (words [g], words [g + 1]);
                    words [g + 1] = swaphigh (words [g], words [g + 1]);
                }
     
            }
            for (int d = 0 ; d < slots ; d++)
                c.println (words [d]);
        }
     
     
        public static String swaplow (String word1, String word2)
        {
            String index = "";
            for (int count = 0; count < word1.length() ; count++)
            {
                if (word1.charAt(count) < word2.charAt(count))
                {
                    index = word1;
                    break;
                }
                else if (word1.charAt (count) > word2.charAt (count))
                {
                    index = word2;
                    break;
                }
            }
            return index;
        }
     
     
        public static String swaphigh (String word1, String word2)
        {
            String index = "";
            for (int count = 0; count < word1.length() ; count++)
            {
                if (word1.charAt (count) > word2.charAt (count))
                {
                    index = word1;
                    break;
                }
                else if (word1.charAt (count) < word2.charAt (count))
                {
                    index = word2;
                    break;
                }
            }
            return index;
        }
    }
    Last edited by sbjibo; May 15th, 2012 at 09:07 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Alphabetical order

    What exception or error is being thrown?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Alphabetical order

    Not sure, but your code is assuming that word2 has at least as many characters as word1.

    If it encounters say

    word1 = "mongoose" word2="snake" then you've got a problem.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alphabetical order

    Ya i know. It tells the users to enter the words with the same length

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Alphabetical order

    May I ask what the h variable is for. It's not doing anything. Or at least you never use it.

    Also, in the h loop you're going from 0 to slots and in the other one, the inner one, you're going from 0 to slots - 1.

    Unfortunately, I can't manage to import the Console class into JGrasp so it won't even compile for me.

    Also, what happens if some of the characters happen to be the same

    word1 = ostriches
    word2 = penguins

    Your code might leave index as null.
    Last edited by javapenguin; May 15th, 2012 at 09:59 PM.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alphabetical order

    It is like a bubble sort. you switch the position of 2 consecutive items in the array. And you loop it for the number of elements the array has. The 'h' variable is to keep track of how many more times you have to loop it

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alphabetical order

    And to compile, you can use the Scanner method intead of console. But you have to modify the code a bit

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alphabetical order

    for (; count < word1.length() ; count++)
    {
    if (word1.charAt (count) > word2.charAt (count))
    {
    index = word1;
    break;
    }
    else if (word1.charAt (count) < word2.charAt (count))
    {
    index = word2;
    break;
    }
    }

    SO count is keep going to increase until it reaches value that is different. I am assuming the words are all different but the same length

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Alphabetical order

    Funny, when I used Scanner, it compiled. No exceptions.

    However, it is printing blanks for all the values in array. I think that might be due to index being null when characters happen to be the same.

       import java.awt.*;
       ;
     
       public class Alphabeticall_order
       {
          static java.util.Scanner console;           // The output console
          public static void main (String[] args)
          {
             console = new java.util.Scanner(System.in);
             System.out.println ("How many words do you want to put in?");
             int slots = console.nextInt ();
             String[] words = new String [slots];
             for (int y = 0 ; y < slots ; y++)
             {
                System.out.println ("Input the words with the same length");
                words [y] = console.nextLine ();
             }
             for (int h = 0 ; h < slots ; h++)
             {
     
                for (int g = 0 ; g < slots - 1 ; g++)
                {
                   words [g] = swaplow (words [g], words [g + 1]);
                   words [g + 1] = swaphigh (words [g], words [g + 1]);
                }
     
             }
             for (int d = 0 ; d < slots ; d++)
                System.out.println (words [d]);
          }
     
     
          public static String swaplow (String word1, String word2)
          {
             String index = "";
             for (int count = 0; count < word1.length() ; count++)
             {
                if (word1.charAt(count) < word2.charAt(count))
                {
                   index = word1;
                   break;
                }
                else if (word1.charAt (count) > word2.charAt (count))
                {
                   index = word2;
                   break;
                }
             }
             return index;
          }
     
     
          public static String swaphigh (String word1, String word2)
          {
             String index = "";
             for (int count = 0; count < word1.length() ; count++)
             {
                if (word1.charAt (count) > word2.charAt (count))
                {
                   index = word1;
                   break;
                }
                else if (word1.charAt (count) < word2.charAt (count))
                {
                   index = word2;
                   break;
                }
             }
             return index;
          }
       }

     

    How many words do you want to put in?
    9
    Input the words with the same length
    Input the words with the same length
    mongoose
    Input the words with the same length
    penguins
    Input the words with the same length
    ostriches
    Input the words with the same length
    octopuses
    Input the words with the same length
    tarantula
    Input the words with the same length
    political
    Input the words with the same length
    warmonger
    Input the words with the same length
    Australia













    Last edited by javapenguin; May 15th, 2012 at 10:11 PM.

  10. #10
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alphabetical order

    it still gives exceptions
    Last edited by sbjibo; May 16th, 2012 at 01:51 PM.

  11. #11
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Alphabetical order

    You could simply store each word in a SortedSet and they would be arranged in alphabetical order then

  12. The Following User Says Thank You to Parranoia For This Useful Post:

    sbjibo (May 16th, 2012)

  13. #12
    Junior Member
    Join Date
    May 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Parranoia View Post
    You could simply store each word in a SortedSet and they would be arranged in alphabetical order then
    That's exactly what I was thinking...though I was thinking about a ListSet.

  14. #13
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Alphabetical order

    i figured it out: When you type this
    words [g] = swaplow (words [g], words [g + 1]);
                   words [g + 1] = swaphigh (words [g], words [g + 1]);
    you have type this in its place:
    String p = words [g];
                    String q = words [g + 1];
                    words [g] = swaplow (words [g], words [g + 1]);
                    words [g + 1] = swaphigh (p, q);

    It was not working because words[g + 1] would be equal to words[g] without a variable p to hold words[g] in the unswapped form
    Last edited by sbjibo; May 16th, 2012 at 03:34 PM.

Similar Threads

  1. Problem with sorting by alphabetical order
    By pikapika in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 6th, 2012, 09:58 PM
  2. RPN Help Please! (Order of Operations)
    By yuvalb in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 30th, 2011, 10:53 AM
  3. Threads in some order...
    By aps135 in forum Threads
    Replies: 6
    Last Post: March 11th, 2011, 05:54 PM
  4. Sort in Cyrilic order
    By cselic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 5th, 2010, 03:08 PM
  5. Stack Order?
    By TimW in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2009, 07:33 AM

Tags for this Thread