Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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]);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote