0

PROBLEM STATEMENT : Consider an array of numeric strings, , where each string is a positive number with anywhere from to digits. Sort the array's elements in non-decreasing (i.e., ascending) order of their real-world integer values and print each element of the sorted array on a new line.

"As input array consist strings of integers, to sort it in ascending order we need to compare evry element present in it but as integers are in the form of strings it need to be converted into Int data type.

if someone could suggest me where i am going wrong in my code. i would be exteremly grateful!

I am not sure whether there is some logical error in my code or not but but a runtime error " array IndexOutOfBounds" occures during execution of code.

PS:I have tried my level best and still can't able to figure out my Mistake. so plz do not downvote its a request.

public static void main(String[] args)
    {
  int temp=0;;
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    String[]arr = new String[n];//array of numeric string
    int[]array = new int[n];
    for(int unsorted_i=0; unsorted_i < n; unsorted_i++){
        arr[unsorted_i] = in.next();
    }
    for(int i=0;i<n-1;i++)//logic for sorting arrays
   {
   for(int j=0;j<n;j++)
       {
      array[i]  =Integer.parseInt(arr[i]);//arr is array of string datatype
       array[j]  =Integer.parseInt(arr[j]);
   if(array[i]>array[j])
       {
     temp=array[i];
       array[i]=array[j];
       array[j]=temp;
   }
 }
  }
    for(int i=0;i<n;i++)
    System.out.println(array[i]);
}
 }

Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Solution.main(Solution.java:23)

Sinchit Batham
  • 103
  • 1
  • 2
  • 10

6 Answers6

2

Change the line for(int j=0;j<n;i++) to for(int j=0;j<n;j++)

kk.
  • 3,747
  • 12
  • 36
  • 67
2

Case 1. In your original example the mistake here:

for (int i = 0; i < n-1; i++)//logic for sorting arrays
{
    for (int j = 0; j < n; j++) {

change to :

for (int i = 0; i < n; i++)//logic for sorting arrays
{
    for (int j = 0; j < n-1; j++) {

OUTPUT:

3
9
5
8
9
8
5

Case 2. Also, you can avoid so more transformations if it's an interesting. One of the way how you can simplify your code, for example:

import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Ex {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Integer[] arr = new Integer[n];//array of numeric string

        for (int unsorted_i = 0; unsorted_i < n; unsorted_i++) {
            arr[unsorted_i] = Integer.parseInt(in.next());
        }

        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

         Arrays.sort(arr, Collections.reverseOrder());
        System.out.println(Arrays.toString(arr));
    }
}

OUTPUT:

5
101
5
25
89
1
[1, 5, 25, 89, 101]
[101, 89, 25, 5, 1]
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
  • this code works fine for alpha numeric strings and sorts the array lexicographically.. However the Question is to sort the String array on the basis of integer values. for example according to your code 10 < 4. – Sinchit Batham Jun 09 '17 at 21:36
  • I was checking and corrected the code for you. Now it works perfectly – Vasyl Lyashkevych Jun 09 '17 at 22:19
1

At first glance it's very suspicious that you increment i on the line

  for(int j=0;j<n;i++)

instead of j

Loonquawl
  • 1,058
  • 1
  • 11
  • 16
1

for(int j=0;j

change this line to

for(int j=0;j

Jade Tang
  • 321
  • 4
  • 23
0

In the inner loop you are incrementing i even though you should be incrementing j:

for(int i=0;i<n-1;i++)
{    
    for(int j=0;j<n;j++) // This was i++ in the original code
    {
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

finally I have solved it correctly :)

public class Solution {
    public static void main(String[] args) {
        String temp = "";
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] arr = new String[n];
        for (int unsorted_i = 0; unsorted_i < n; unsorted_i++) {
            arr[unsorted_i] = in.next();
        }
        for (int i = 0; i < n - 1; i++)//logic for sorting arrays
        {
            for (int j = i + 1; j < n; j++) {
                if ((arr[i].length()) > (arr[j].length())) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;

                } else if ((arr[i].length()) == (arr[j].length())) {
                    if (Long.parseLong(arr[i]) > Long.parseLong(arr[j])) {
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
        }
        for (int i = 0; i < n; i++) {
            System.out.println(arr[i]);
        }
    }
}
Dirk
  • 3,030
  • 1
  • 34
  • 40
Sinchit Batham
  • 103
  • 1
  • 2
  • 10
  • Well done! The truth, your code works only in single direction, and you use so many resources for that. Try to use the fastest algorithms in the future. – Vasyl Lyashkevych Jun 09 '17 at 22:35