Im stuck on my loop program, I have to create a loop program for arrays which only stops if a user inputs quit or the user has input 1000 entries, the program then has to sort the arrays in order of composer. I've tried so many different kinds of algorithms that my mind has gone back to basics

import javax.swing.*;

class ClassicMusic
{
    public static void main (String [] args)
    {
        
      String composer[] = new String[999];
      String piece[] = new String[999];    
      
      Input(composer,piece);
        
      System.exit(0);
    }
    
    public static void Input(String [] composer, String [] piece)
    {
        int i = 0;
        composer [i] = "";
        while(!composer[i].equals("q"))
        {
            //for(int i = 0; i<=999; i++)
            
                composer [i] = JOptionPane.showInputDialog("input composer");
                piece [i] = JOptionPane.showInputDialog("input piece");
            
                i = i+1;
        }
        SortA(composer,piece);
    }
    
    public static void SortA(String []composer, String [] piece)
    {
        for (int j = 0; j<=999; j++)
        {
            if(composer[j].equals(j++))
            {
                System.out.println(composer[j]);
                System.out.println(piece[j]);
            }
        }
    }
}

thank you.

Recommended Answers

All 6 Replies

1. make sure it is clear to the user how to exit your program. So change your print statement to something like "Enter the composer's name or 'q' to quit."

2. You declared your arrays as having 999 elements, not 1000. If that was your intent, that is fine, just letting you know.

3. It seems like your main area of struggle is with sorting the array. You want to sort it alphabetically, by composer, correct? You'll have to be careful to make sure that the piece and the composer always stay in the same index for each array (So if you move the composer to index 3, their piece has to be moved to index 3 as well). Check out this daniweb thread on sorting Strings alphabetically: http://www.daniweb.com/forums/thread133150.html . Also, the String class has a compareToIgnoreCase method that I (think) would do what you want (compareTo typically returns an integer which indicates whether the first object was < = or > the second object).

OK firstly sorry i didn't comment, the q is for quit but don't worry about my messages im still in the testing stages, secondly i thought that arrays start at index 0 so thats why i put 999, but i see that i was thinking of it the wrong way, and thirdly i did check out the thread link you posted, but the code supplied there was way to complicated for me to understand at this stage.

if there is a sort function in java e.g. sort.array i would not be able to use it as i have to show my sorting with loops.

Here is an example to show you how to create arrays.

String[] aBunchOfStrings = new String[10];
for (int i = 0; i < 10; i++){
aBunchOfStrings = "";
}

So basically, when you create the array you need to use the full amount that you want, 10 in this case, but when you index the array, you use 0-9 (in this case).

And on the thread that I linked you to, I wanted you to look at post #3 by JavaAddict. He usually gives good advice so I think what he is suggesting will work. In any case, it is very easy to implement so it's worth a try. You can basically copy and paste that code, you'll just need to call the Array.sort(yourArray) method each time you want to sort your array.

Your input method seems correct but there are a few things you need to add:

public static void Input(String [] composer, String [] piece)
    {
        int i = 0;
        while( i<composer.length )
        {  
                String comp = JOptionPane.showInputDialog("input composer");
                 if (comp.equals("q"))  {
                       break;  
               } else {
                       composer [i] = comp;
                       piece [i] = JOptionPane.showInputDialog("input piece");
                        i = i+1;
               }
        }
        SortA(composer,piece, i);
    }

The if-statement is used so in case the user enters "q" not to insert that at the arrays.

With the previous code you would never exit the while:
The user enters "q", you put that at the array: composer [i] = JOptionPane.showInputDialog("input composer"); But then you increase the "i" so at the while loop you do not check the current composer entered, but the next element, which is null. Also you do not check the length of the array. If the user continues to enter values the "i" will exceed the length of the array.

That's why the loop has the condition: i<composer.length so not to exceed the limit and if the user enters "q" you call the "break" command which exits the loop.
Also you will need to pass the "i" variable at the sort method, because you don't want to sort the entire array but only the elements given. In the sort method you will use as upper limit the "i" not the length of the array.

Also use this: for (int j = 0; j<composer.length; j++) at the for-loops instead of this: for (int j = 0; j<=999; j++) In that way if you change the declaration of the array you will not need to go through the code changing all the loops

i thought that arrays start at index 0 so thats why i put 999

Yes they do start at 0 but when you do this:
String [] array = new String[999];

You say that the array has length 999 and indexes from 0 to 998

Thank you JavaAddict you cleared my mind for the input, but im gonna stop right now on this array search and maybe come back to it in a later point in my life.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.