1

There are 2 arrays, one of int and one of Strings (words) ; sort both of them and then print it in a way that at odd places it should be words and at even numbers.

This is my code:

import java.util.*;
public class JavaApplication4 {


    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int num[]=new int[10];
        String str[]=new String[10];
        String str1[]=new String[20];
        for(int i=0; i<5; i++)//for taking strings
        {
            str[i]=in.next();
        }
        for(int i=0; i<5; i++)//for taking nums
        {
            num[i]=in.nextInt();
        }
        for(int i=0; i<5; i++)
        {
            System.out.println("The String are "+str[i]);
        }
        for(int i=0; i<5; i++)
        {
            System.out.println("The num are "+num[i]);
        }
         for (int i = 0; i < 5; i++) //for sorting nums
        {
            for (int j = i + 1; j < 5; j++) 
            {
                if (num[i]>(num[j])) 
                {
                    int temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }
        for (int i = 0; i < 5; i++)// for sorting strs
        {
            for (int j = i + 1; j < 5; j++) 
            {
                if (str[i].compareTo(str[j])>0) 
                {
                    String temp = str[i];
                    str[i] = str[j];
                    str[j] = temp;
                }
            }
        }
        System.out.println("The sorted strings are:");
        for(int i=0; i<10; i++)//for merging both
        {
            if((i+1)%2==0)
            {
                int k=0;
                str1[i]=String.valueOf(num[k]);
                 System.out.println("The String are "+str1[i]);
                k++;
            }
            else
            {
                int j=0;
                str1[i]=str[j];
                 System.out.println("The String are "+str1[i]);
                j++;
            }


        }
      /*   for(int i=0; i<10; i++)
        {
            System.out.println("The String are "+str1[i]);
        }
        */
    }
}

What I am getting the output:

The sorted strings are:
The String are ab
The String are 1
The String are ab
The String are 1
The String are ab
The String are 1
The String are ab
The String are 1
The String are ab
The String are 1

It's only taking the first element of both arrays.

2
  • And why do you expect something else? You set the variables k and j to 0 before accessing the array elements ... so what else than the first element should they retrieve? Commented Jun 23, 2016 at 13:55
  • You should try using meaningful names for your variables. It's really hard to say which index/array is referring to what. Commented Jun 23, 2016 at 13:57

3 Answers 3

2

You should initialize k and j to 0 before the loop, not inside the loop.

    int k=0;
    int j=0;
    for(int i=0; i<10; i++)//for merging both
    {
        if((i+1)%2==0)
        {
            str1[i]=String.valueOf(num[k]);
             System.out.println("The String are "+str1[i]);
            k++;
        }
        else
        {
            str1[i]=str[j];
             System.out.println("The String are "+str1[i]);
            j++;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can user Arrays class to sort the arrays.

String[] strArray = new String[] {"a","z","q","p"};
Arrays.sort(strArray);

Similarly, try to 2nd array.

Now, declare another array by adding size of two arrays:

String[] newArray = new String[sum];
int j=0;
for(int i=0;i<newArray.length;i+=2)
{
    newArray[i]=strArray[j];
    newArray[i+1] = otherArray[j++];
}

Comments

0

Other people has pointed the root cause. Beside this, why you initialize the arrays num[] str[] str1[] with capacity 10(and 20), which is a double of the needed capacity 5(and 10) ?

Another issue is that the name of str1[] is really bad.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.