Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So, I'm doing a project and I'm very confused with the second part. What I have to do is ask user to input two lists of any length (the first integer entered is the length of list, eg. Input: 5 1 2 3 4 5, actual array: 1 2 3 4 5.) Do that twice, then merge the two lists into one list and sort out the values and display them in numerical order. I'm so confused. Can someone show me what i need to do or at least get me started? Also, the sorting must be done in a seperate method, which i have not started yet.

What I have tried:

//Program05.java
//Mac Howard

import java.util.Scanner;

public class Program05 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner input = new Scanner(System.in);
		
		//Variables
		int[] list1;
		int[] list2;		
				
		//Input lists
		System.out.print("Enter list 1 (the first integer entered determines size of the list): ");
		int size1 = input.nextInt();
		list1 = new int[size1];
		for(int i = 0; i < list1.length; i++) {
			list1[i] = input.nextInt();
		}
		System.out.print("Enter list 2 (the first integer entered determines size of the list): ");
		int size2 = input.nextInt();
		list2 = new int[size2];
		for(int i = 0; i < list2.length; i++) {
			list2[i] = input.nextInt();
		}
		//Delete this
		for (int i = 0; i < list1.length; i++) {
			System.out.print( list1[i] + " ");
		}
		for (int i = 0; i < list2.length; i++) {
			System.out.print( list2[i] + " ");
		}
	}
	
	public static int[] merge(int[] list1, int[] list2) {
		
	}

}
Posted
Updated 8-Dec-17 5:33am

1 solution

Why write the same code twice? Create a method that reads the numbers and returns a list. Sort and merge are fairly simple to do. First sort each list, then read them both comparing the numbers and printing the lowest of the two each time.
 
Share this answer
 
Comments
Member 13455134 8-Dec-17 11:38am    
It's a project requirment for my class. I must use two methods. Here is the description,
Write the following method the merges two sorted lists into a new sorted list.

public static int[] merge(int[] list1, int[] list2)

Implement the method in a way that takes list1.length + list2.length comparisons. Write a test program that prompts the user to enter two sorted lists and displays the merged list. Here is a sample run. Note that the first number in the input indicates the number of elements in the list.

Enter list1: 5 1 5 16 61 111
Enter list2: 4 2 4 5 6
The merged list is: 1 2 4 5 5 6 16 61 111
Richard MacCutchan 8-Dec-17 12:15pm    
Right, so go and write things down on paper, think about the elements in each list and what you need to do to merge them. The whole point of homework is that you read your course notes and think about what needs to be done.
Member 13455134 8-Dec-17 12:33pm    
Well, yeah i get that and have been doing just that for 6 hours now. That's why I came here and asked some professionals. I just really want to get this done. I'm not asking for the answer, just a nudge in the right direction. I think what I need to get do is be able to identify each element in each list and compare them to one another and create the merged list accordingly. But I have absolutely no clue how to go about that code wise.
Richard MacCutchan 8-Dec-17 13:01pm    
Well I already gave you a hint, twice, but at the risk of repeating myself yet again:
Read the first item of each list
While more values exist:
- Compare them and print the lowest value
- Read the next item from the list that had the lowest value
Continue

If either list gets to the end you just need to read and print the remaining values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900