Computer Science, asked by utkrishtbuttolia128, 1 year ago

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 rishigandhi562
0

inundation error.......


utkrishtbuttolia128: explain it briefly what i haver to do to correct this
Answered by siddhartharao77
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:

Anonymous: Awesome
siddhartharao77: Thank you
utkrishtbuttolia128: but sir i understand your program but what is the problem in mine
siddhartharao77: Your code is typical for me to understand..Sorry!
Similar questions