Debug this code. There are 4 errors. Do NOT re-write just make the changes. publ
ID: 3544170 • Letter: D
Question
Debug this code. There are 4 errors. Do NOT re-write just make the changes.
public class SortArrays
{
public static void main(String[] args)
{
String[] names = new String[5];
for(int i=0; i<5; i++)
{
names[i] = JOptionPane.showInputDialog("Please enter Name "+i);
}
for(int j=0; j<names.length();j++)
{
for (int i=j+1 ; i<names.length(); i++)
{
if(names[i]>names[j])
{
String temp= names[j];
names[j]= names[i];
names[i]=temp;
}
}
System.out.print(names[j]);
}
}
}
Explanation / Answer
import java.util.*;
import java.io.*;
import javax.swing.*;
class SortArrays
{
public static void main(String[] args)
{
String[] names = new String[5];
for(int i=0; i<5; i++)
{
names[i] = JOptionPane.showInputDialog("Please enter Name "+i);
}
for(int j=0; j<names.length;j++)
{
for (int i=j+1 ; i<names.length; i++)
{
if(names[i].compareTo(names[j])>0)
{
String temp= names[j];
names[j]= names[i];
names[i]=temp;
}
}
System.out.print(names[j]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.