Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

Students also viewed these Databases questions

Question

Use of assessments to determine trainees learning styles.

Answered: 1 week ago

Question

7. Discuss the advantages of embedded learning.

Answered: 1 week ago