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 17 of 17

Thread: Merging 2 arrays

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Merging 2 arrays

    i have to merge 2 arrays of size 3,im getting incorrect output plz help
    import java.util.*;
    class a
    {
    public static void main(String args[])
    {
    int i,j;
    int []a=new int[3];
    int []b=new int[3];
    int []c=new int[6];
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the elements of the first array");
    for(i=0;i<=2;i++)
    {
    a[i]=sc.nextInt();
    }
    System.out.println("Enter the elements of the second array");
    for(j=0;j<=2;j++)
    {
    b[j]=sc.nextInt();
    }
     
    for(i=0;i<=2;i++)
    {
    c[i]=a[i];
    }
    for(j=0;j<=2;j++)
    {
    c[i]=b[j];
    i++;
    }
    for(i=0;i<=5;i++)
    {
    System.out.println("The merged array");
    System.out.println(c[i]);
     
    }
    }
    }
    Last edited by severus1; June 30th, 2011 at 12:32 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Merging 2 arrays

    What output are you getting? What output did you expect?

    Do you really not indent your code?

    Hint: Print out the index you're updating when you populate your c array in both for loops.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Merging 2 arrays

    what im expecting
    input
    Enter the elements of the first array
    1
    2
    3
    Enter the elements of the second array
    4
    5
    6
    output
    1
    2
    3
    4
    5
    6
    im getting no output

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Merging 2 arrays

    I'd recommend you run through this with a debugger, or at least put in a bunch of print statements, to figure out what's going on. I already gave you a hint.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Merging 2 arrays

    If you want to loop 3 times, the end condition should be x < 3, not x < 2.

    Check that you're incrementing the correct index in your 'for' loops.

    ETA - sorry Kevin, I pre-empted you there...

  6. The Following User Says Thank You to dlorde For This Useful Post:

    severus1 (July 1st, 2011)

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Merging 2 arrays

    Quote Originally Posted by dlorde View Post
    ETA - sorry Kevin, I pre-empted you there...
    No problem. He's got enough problems in that code for more than one person. :p
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Merging 2 arrays

    Yes, quite!

  9. #8
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Merging 2 arrays

    Quote Originally Posted by severus1 View Post
    what im expecting
    input
    Enter the elements of the first array
    1
    2
    3
    Enter the elements of the second array
    4
    5
    6
    output
    1
    2
    3
    4
    5
    6
    im getting no output
    I don't really understand how you're getting no output? Although the problem can be solved differently, it seems to be your print statement screwing up the output. If you put System.out.println("The merged array"); outside the for-loop then it should run fine, as far as I can see.

  10. The Following User Says Thank You to Stockholm Syndrome For This Useful Post:

    severus1 (July 1st, 2011)

  11. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Merging 2 arrays

    still not getting the output
    import java.util.*;
    class a
    {
    public static void main(String args[])
    {
    int i,j;
    int []a=new int[3];
    int []b=new int[3];
    int []c=new int[6];
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the elements of the first array");
    for(i=0;i<=2;i++)
    {
    a[i]=sc.nextInt();
    }
    System.out.println("Enter the elements of the second array");
    for(j=0;j<=2;j++)
    {
    b[j]=sc.nextInt();
    }
     
    for(i=0;i<=2;i++)
    {
    c[i]=a[i];
    }
    for(j=0;j<=2;j++)
    {
    c[i]=b[j];
    i++;
    }
    System.out.println("The merged array");
    for(i=0;i<=5;i++)
    {
     
    System.out.println(c[i]);
     
    }
    }
    }

  12. #10
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Merging 2 arrays

    Hmm it's working for me mate so it should be working for you too. What IDE are you using? Could you show me how you're attempting to input? You could try merging the arrays using while loops as an alternative, something like this:

    int k=0; 
    while(k<a.length) {
    	c[k] = a[k];
    	k++;
    }
     
    int l=3; int m=0;
    while(l<c.length) { 
    	while(m<3) {
    		c[l] = b[m];
    		l++; m++;
    	}
    }

    But i suggest debugging your code using println() statements, it's a brilliant way of seeing where you're going wrong.

  13. #11
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Merging 2 arrays

    I've ran this through Netbeans IDE and it prints the output correctly. Can you be more specific when you say your not getting output, does this mean your obtaining an error during run time? Or does it mean that it's not printing correctly.

    Also, in order to make your code easier to read (so that in the future we can solve your problems easier) you should avoid using cryptic abbreviations and use whole names when declaring variables, you should also use some form of indentation. And spaces when you're using operators and declaring variables.

    Also when looping through arrays the Java programming language has what some people call a enhanced for loop, an example of an enhanced for loop (i've posted a tutorial below which explains this in depth) which could of been used in your program is this:
    for(int anyNameHere : arrayNameHere)
                b[anyNameHere] = sc.nextInt();

    Where anyNameHere is any name you want, and the arrayName is the array you want to loop through.

    Tutorials:

    http://download.oracle.com/javase/tu...bolts/for.html

    Code Conventions for the Java Programming Language
    Last edited by Karl; July 1st, 2011 at 02:50 PM.

  14. #12
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Merging 2 arrays

    im compiling the code on ideone.com

  15. #13
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Merging 2 arrays

    guys,can u plz help me out on how to get my program compiled correctly on ideone.com

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Merging 2 arrays

    Please copy and paste here the compile errors you are getting. I don't see them in the past posts.

  17. #15
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Merging 2 arrays

    The code you posted compiles and runs OK on ideone.com for me - although I have to wonder why you're using such an awkward way to compile and run your code. Why not use a local IDE that actually allows you to interact with your program directly?
    Last edited by dlorde; July 2nd, 2011 at 07:37 PM.

  18. #16
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Merging 2 arrays

    yea,now it worked,was doing a mistake in taking input
    Thanks

  19. #17
    Junior Member
    Join Date
    Dec 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Merging 2 arrays

    Just put the "System.out.println("The merged array");" statement before the last for loop as:-
    import java.util.*;
    class a
    {
    public static void main(String args[])
    {
    int i,j;
    int []a=new int[3];
    int []b=new int[3];
    int []c=new int[6];
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the elements of the first array");
    for(i=0;i<=2;i++)
    {
    a[i]=sc.nextInt();
    }
    System.out.println("Enter the elements of the second array");
    for(j=0;j<=2;j++)
    {
    b[j]=sc.nextInt();
    }
     
    for(i=0;i<=2;i++)
    {
    c[i]=a[i];
    }
    for(j=0;j<=2;j++)
    {
    c[i]=b[j];
    i++;
    }
    System.out.println("The merged array");
    for(i=0;i<=5;i++)
    {
     
    System.out.println(c[i]);
     
    }
    }
    }

    u will get what u r expecting.
    Last edited by helloworld922; July 3rd, 2011 at 09:03 AM.

Similar Threads

  1. Need help merging arraylists and sorting
    By oliminator in forum Collections and Generics
    Replies: 4
    Last Post: March 28th, 2011, 08:03 PM
  2. [SOLVED] Sorting and Merging
    By javapenguin in forum What's Wrong With My Code?
    Replies: 36
    Last Post: December 7th, 2010, 07:04 PM
  3. Merging of Trees..
    By Kumarrrr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 23rd, 2010, 11:39 PM
  4. Merging Arrays Vs. Linked List.
    By Kumarrrr in forum Collections and Generics
    Replies: 1
    Last Post: March 1st, 2010, 02:20 AM
  5. Problem in merging two java classes
    By madkris in forum Object Oriented Programming
    Replies: 11
    Last Post: March 16th, 2009, 09:02 AM