import java.io.*;
class Slip2
{ public static void
main(String a[]) throws IOException
{
BufferedReader
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter
how many cities");
int n =
Integer.parseInt(br.readLine());
String s[]=new
String[n];
for(int
i=0;i
{
System.out.println("Enter
city name ");
s[i]=br.readLine();
}
for(int
i=0;i
{
for(int
j=0;j
{
if(s[j].compareTo(s[j+1])<1 span="">1>
{
String
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
System.out.println("Cities
in Descending order : ");
for(int
i=0;i
{
System.out.println(s[i]);
}
}
}
:\BCA_JAVA\Slip2.java:18: ')' expected
ReplyDeleteif(s[j].compareTo(s[j+1])<1 span="")
^
F:\BCA_JAVA\Slip2.java:18: ';' expected
if(s[j].compareTo(s[j+1])<1 span="")
^
2 errors
Tool completed with exit code 1
for loop incomplete
ReplyDeleteI have made corrections to the above code and it compiles correctly:
ReplyDeleteimport java.io.*;
class Slip2
{ public static void main(String a[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter how many cities");
int n = Integer.parseInt(br.readLine());
String s[]=new String[n];
for(int i=0;i<n; i++)
{
System.out.println("Enter city name ");
s[i]=br.readLine();
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(s[j].compareTo(s[i])<0)
{
String temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
System.out.println("Cities in Descending order : ");
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
}