what is the error in this code?
if i am trying to run this and enter the no. of element as 4 than it is only taking 3 element as input but i have started the loop with i=0
please explain briefly
package exam.practice;
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("enter the size of the array");
int a=scan.nextInt();
String b[]=new String[a];
System.out.println("enter the elements of the array");
for (int i = 0; i < a; i++)
{
b[i]=scan.nextLine();
}
int min,i,j;
String t;
for(i=0;i
{
min=i;
for (j = i+1; j < a; j++)
{
if(b[min].compareTo(b[j])<0)
{
min=j;
}
}
t=b[i];
b[i]=b[min];
b[min]=t;
}
System.out.println("the decending order is:");
for (int k = 0; k < a; k++)
{
System.out.println(b[k]);
}
}
}
Answers
Answered by
0
inundation error.......
utkrishtbuttolia128:
explain it briefly what i haver to do to correct this
Answered by
2
Sample Program:
import java.util.Scanner;
class pratice
{
public static void main(String args[])
{
String t;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of the array");
int a = scan.nextInt();
String b[]=new String[a];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the elements of the array");
for(int i = 0; i < a; i++)
{
b[i]=sc.nextLine();
}
for(int i = 0; i < a; i++)
{
for(int j = i+1; j < a; j++)
{
if(b[i].compareTo(b[j])<0)
{
t = b[i];
b[i]=b[j];
b[j]=t;
}
}
}
System.out.print("The descending order is:");
for(int i = 0 ; i<=a-1; i++)
{
System.out.println(b[i]+ " ");
}
}
}
Output:
Enter the size of an array:
4
Enter the elements of the array:
a
d
c
b
The descending order is:
d
c
b
a
<Hope it helps!>
Attachments:
Similar questions
Math,
7 months ago
Social Sciences,
7 months ago
Hindi,
7 months ago
Business Studies,
1 year ago
Science,
1 year ago
English,
1 year ago
Math,
1 year ago
English,
1 year ago