Question: Please help fix my code. I'd like to write a java program that prompts the user for a list of words (quit to end the

Please help fix my code. I'd like to write a java program that prompts the user for a list of words ("quit" to end the list), then prints the list in sorted order.

My Code So far

import java.util.Scanner;

public class Lab15 {

public static void main(String[] args) {

System.out.println("Enter a list of words (enter 'quit' to quit) : ");

Scanner in = new Scanner(System.in);

String word = in.nextLine();

String[] list = new String[word];

do {

word = in.nextLine();

}while (!word.equals("quit"));

sortedList(list);

for (int i = 0; i < list.length; i++) {

System.out.println(list[i]);

}

in.close();

}

//insertion sort

public static void sortedList(String[] list ) {

String key;

for (int i = 0; i < list.length; ++i) {

key = list[i];

int j = i - 1;

if (j>i)

{

swap(list, i,j);

}

}

}

public static void swap(String[] list, int i, int j ) {

String pos=list[i];

list[i]=list[j];

list[j]=pos;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!