Question: What do I need to add to make it so if the gift and the name have the same first letter they are printed together
What do I need to add to make it so if the gift and the name have the same first letter they are printed together
import java.util.Scanner; import java.util.Arrays;
public class DSassignment1 { void arrays() { Scanner kb = new Scanner(System.in); String[] names = new String[15];
System.out.println("enter friends names | type *END* when done"); for (int i = 0; i < names.length; i++) {
String in = kb.nextLine();
names[i] = in; if (in.equals("*END*")) { break; } }
String[] gifts = new String[15]; System.out.println("enter gift names | type *END* when done"); for (int j = 0; j < gifts.length; j++) { String in = kb.nextLine();
gifts[j] = in; if (in.equals("*END*")) { break; } } for (int j = 1; j < names.length; j++) { String x = names[j]; int i = j; if (names[i] == null || names[i].equals("*END*")) continue; while (i > 0 && names[i - 1].compareTo(x) > 0) { names[i] = names[i - 1]; i--; } names[i] = x; } System.out.println("Names: " + Arrays.toString(names)); System.out.println("Gifts: " + Arrays.toString(gifts));
System.out.println(" Names:");
for (int i = 0; i < names.length && names[i] != null && !names[i].equals("*END*"); i++) { System.out.println(names[i]); } System.out.println(" Gifts:"); for (int i = 0; i < gifts.length && gifts[i] != null && !gifts[i].equals("*END*"); i++) { System.out.println(gifts[i]); } }
public static void main(String[] args) { DSassignment1 lab = new DSassignment1();
lab.arrays(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
