Question

Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to...

Java programming question:

Here is the feedback I received "sort fails. Use Arrays class method to sort."

The actual question:

Write a program as follows:

  • Declare a five element array of Strings.
  • Use a for loop and keyboard input to populate the array with the names of five friends.
  • Sort the array in alphabetical order.
  • Use a foreach loop to print the sorted array of friends, all on one line.

Sample Output (inputs in italics)

Enter the names of five good friends

Kenny Denny Penny Jenny Benny

My friends in alphabetical order: Benny Denny Jenny Kenny Penny

______________________________________________________________________

My code:

import java.util.Scanner;

public class Program2 {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       // declare array of 5 strings
       String[] name = new String[5];
       System.out.println("Enter the names of five good friends:");
       // use loop to enter names into array
       for (int i = 0; i < 5; i++) {
           name[i] = input.next();
       }
       // sort array in alphabetical order using selection sort
       for (int i = 0; i < 4; i++) {
           for (int j = i + 1; j < 5; j++) {
               String temp = name[i];
               name[i] = name[j];
               name[j] = temp;
           }
       }
       // print the sorted array using foreach loop
       System.out.print("My friends in alphabetical order: ");
       for (String temp : name) {
           System.out.print(temp + " ");
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
import java.util.Arrays;

public class prog2
{

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// declare array of 5 strings
String[] name = new String[5];
System.out.println("Enter the names of five good friends:");
// use loop to enter names into array
for (int i = 0; i < 5; i++) {
name[i] = input.next();
}

//call sort method of array class to sort the names
Arrays.sort(name);

// print the sorted array using foreach loop
System.out.print("My friends in alphabetical order: ");
for(int i=0;i<5;i++)
System.out.print(" "+name[i]);
}
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java programming: The feedback I received "responds badly to bad input. Fix 2" Original question: Write...

    Java programming: The feedback I received "responds badly to bad input. Fix 2" Original question: Write a program that creates a small (6-10) array of ints. Display your array elements, all on one line, using a foreach loop. In a try block, prompt the user to enter an index for the array and attempt to print the element with that index. Follow the try block with two catch blocks; one that detects an index out of bounds, and another that...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program...

    ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program at the bottom to sort an array using selection sort. Write a selection sort to report out the sorted low values: lowest to highest. Write another to report out the sorted high values – highest to lowest. Last but not least, the program should output all the values in the array AND then output the 2 requested sorted outputs. -----> MY OLD PROGRAM BELOW...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • On the following code there is an error bc you are not reverting back to the...

    On the following code there is an error bc you are not reverting back to the original order after a sort. For the next sort you are passing the same reference variable to the next method. But that will point to the same (already sorted) array on the memory. Hence after the first sorting method, all three sorting methods are working on the already sorted array. Do the following : Just copy each data set to 4 different arrays -...

  • How to write a recursive method named lessThanKFirst that receives an array of integers a and...

    How to write a recursive method named lessThanKFirst that receives an array of integers a and an integer k and rearranges the integers in a in such a way that all integers that are smaller than k come before any integers that are greater than or equal to k. As an example, suppose that a and k are: int[] a  = {35, 12, 57, 28, 49, 100, 61, 73, 92, 27, 39, 83, 52}; int k = 73; Then, the following...

  • Write and submit the source code for the following program. The program will use an integer...

    Write and submit the source code for the following program. The program will use an integer array of size 10 to store the prices of smartphones. It will then determine and print the prices of the most expensive and cheapest phones. Use the following variables: int[] prices = new int[10]; // Array of smartphone prices Assignment Ask the user for the price of each smartphone (using a for loop) Sort the list of smartphones (once) from low to high price...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT