I have a program,but there is an error, please help me, thanks
program to include six string data (name of city), then do the sorting of the six cities data and print data before and after sorting. but in terms of input data strings, there is an error, please help, thank you

import javax.swing.JOptionPane;
public class city {

  public static String []namecity(String i)
  {
  String []irawan=new String [i];
  	for(int k=0;k<i.length;k++ )
  	{
 irawan [k] =JOptionPane.showInputDialog("Please enter  name of city ["+k+"]: ");
  	}
  	return irawan;
  }

  	public static void printcity(String []p)
  	{
  		for(int k=0;k<p.length;k++)
  		{
  		System.out.print(p[k] + " ,");
  		}
  	}


    public static void main(String[] args) {
        // TODO code application logic here
       String [] hery = namecity("6");
        printcity(hery);
    }



}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

String []irawan=new String [i];

The 'i' needs to be an integer. You initialize like this:
String []irawan=new String [5];
Not like this:
String []irawan=new String ["5"];

Also you loop the array like this:
for(int k=0;k<p.length;k++) p is the array.

Not like this:
for(int k=0;k<i.length;k++ ) i is not the array. irawan is.

but, I still have a problem in sorting, can you help me again?

this is my code :

import javax.swing.JOptionPane;
public class kota {

  public static String []namecity(int i)
  {
  String []irawan=new String [6];
  	for(int k=0;k<i;k++ )
  	{
 irawan [k] =JOptionPane.showInputDialog("Please enter name of city-["+k+"]: ");
  	}
  	return irawan;
  }

  	public static void printcity(String []p)
  	{
  		String [] bangun =namecity(6);
  		int a;
  		for(int k=0;k<p.length;k++)
  		{
  		System.out.print(p[k] + " ,");
  		for( a=0;a<7;a++ )
  		System.out.print(bangun.sorting(bangun[a]));
  		}
  	}

 public static void sorting(int m, int n)


{
	String uno[]= namecity(6);
    String temp;

	for(int a=0;a<6;a++)
		for(int b=1+a;b<7;b++)
			if(uno[a].compareTo(uno[b])>0)
			{
				temp=uno[a];uno[a]=uno[b];uno[b]=temp;
			}

}


    public static void main(String[] args) {
        // TODO code application logic here
       String [] hery = namecity(6);
        printcity(hery);
    }



}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

search for Collections.sort, some examples contains f.e. Comparator<Integer> r = Collections.reverseOrder();

Edited 5 Years Ago by mKorbel: n/a

At the namecity why do you initialize the array with 6 and you ignore the argument 'i'

At the printcity method why do you ignore the argument and you call the namecity method with argument 6 even though inside the namecity you ignore the argument
and why do you have 2 loops where you loop both arrays when you need to loop only the argument!

And USE the length of the array at the loop, not hard coded values:
for (int i=0;i<array.length;i++)

Edited 5 Years Ago by javaAddict: n/a

you have bangun.sorting() see arrays donot have metthods defined.
simply use sorting(bangun[a])
same with the sorting() function

you can use InsertionSort,SelectionSort or linearSort which i think is the easiest,since you are working with strings make sure you use compareTo() method in place of logical operation of greater than or less than signs and also during initialization of array you must specify the array length,thanks.....

This article has been dead for over six months. Start a new discussion instead.