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

Please help fix my code. I\'d like to write a java program that prompts the user

ID: 3868197 • Letter: P

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

}

}

Explanation / Answer

Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.

_______________

Lab15.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Lab15 {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList < String > list = new ArrayList < String > ();

// Getting the words entered by the user
System.out.println("Enter a list of words (enter 'quit' to quit) : ");
Scanner in = new Scanner(System.in);
String word = in .next();

/*
* This while loop continues to execute until the user enters a the word
* "quit"
*/
while (!word.equals("quit")) {
list.add(word);
word = in .next();
}

// Calling the sort() method on the Collections class
Collections.sort(list);

// Displaying the Words after sorting in alphabetical order.
System.out.println("After Sorting words in Alphabetical order :");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}

}

}

____________________

Output:

Enter a list of words (enter 'quit' to quit) :
hello
monkey
job
pen
pencil
book
cab
iron
zebra
lion
quit
After Sorting words in Alphabetical order :
book
cab
hello
iron
job
lion
monkey
pen
pencil
zebra

________________Thank You

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