Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java Array Merge Input Beginner Question

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    Philippines
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Array Merge Input Beginner Question

    Hello guys, i'm a beginner on programming and currently making a program right now that will input 5 integer values for the first array and another 5 values for the second array, then merging a two lists of integers into a single list of integers. Store the merge list into another array in a sorted manner.

    Example:
    arrayOne = {5, 9, 3, 0, 2} arrayTwo = {7, 1, 8, 6, 4}

    mergedArray: 5, 9, 3, 0, 2, 7, 1, 8, 6, 4
    sortedArray: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class arrayMerge {
     
    	/**
    	 * @param args
    	 * @throws IOException 
    	 * @throws NumberFormatException 
    	 */
     
     
                public static void main (String[] args) throws NumberFormatException, IOException 
     
     
     
                int[] arrayone = new int [4];
    	    int[] arraytwo = new int [4];
    	    int[] arrayMerge = new int[10];
     
                BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
     
     
    	    for (int ctr = 0; ctr < arrayOne.length; ctr++ ){
     
    	    System.out.print("Enter 5 Numbers for the first array: ");
     
    	    arrayone[] = Integer.parseInt(dataIn.readLine()); // cannot resolve into a type, syntax error.
     
    	    System.out.print("array 2 {" + ctr + "}" + " = [" arrayone "]"); //syntax error, operator undefined. 
     
    	    } for (int ctr = 0; ctr < arraytwo.length; ctr++) {
     
    	    System.out.print("Enter 5 Numbers for the second array: ");
     
    	    arraytwo[] = Integer.parseInt(dataIn.readLine()); // cannot resolve into a type, syntax error.
     
    	    System.out.print("array 2 {" + ctr + "}" + " = [" arraytwo "]"); //syntax error, operator undefined.
    	    } 
     
    	    arrayMerge = arrayone + arraytwo;
     
    	    System.out.print (arrayMerge);
     
    	    }
     
    }

    To be honest, what makes me confuse is on how to merge them, i tried many simple solutions for this problem that i understand so far and it appears that it didnt work quite well.
    Last edited by grumpy_int05; January 27th, 2013 at 03:19 AM. Reason: added an example


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,289
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Java Array Merge Input Beginner Question

    it didnt work quite well.
    Please post the program's input and output that shows what it did and what didn't work the way you want.

    A useful method for debugging with arrays:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member Mitch1337's Avatar
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Array Merge Input Beginner Question

    First off, your first and second arrays contain 4 indexes each. The merged should then contain only 8, not 10.

    Secondly,
     arrayone[] = Integer.parseInt(dataIn.readLine()); // cannot resolve into a type, syntax error.

    This is not a valid way to populate an array.
    You should do it in a form like:

    arrayone[ctr] = Integer.parseInt(dataIn.readLine());

    Finally, when merging in the way you want, I would do it in this sort:
    for(int i = 0; i < arrayOne.length; i++)
    mergedArray[i] = arrayOne[i];
     
    for(int j = 4; j < mergedArray.length; j++)
    mergedArray[j] = arrayTwo[j-4]

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,289
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Java Array Merge Input Beginner Question

    This would be better:
    for(int j = arrayOne.length; j < mergedArray.length; j++)
       mergedArray[j] = arrayTwo[j-arrayOne.length]
    Don't hard code magic numbers. Use variable values. What if the size of the input arrays changed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member Mitch1337's Avatar
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Array Merge Input Beginner Question

    Should have done that, very good point Norm.

Similar Threads

  1. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 01:57 PM
  2. Replies: 2
    Last Post: August 30th, 2012, 04:26 AM
  3. Beginner question on where to put an array
    By Diplo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 3rd, 2011, 04:56 PM
  4. Quick, beginner-level Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2011, 01:06 PM
  5. Replies: 4
    Last Post: November 14th, 2010, 11:44 AM