-1

enter image description hereDisplay the population of each country from the least to the highest.

[Input Format: First input refers to the no of countries, second one is an array to get the country names and the third one is an array to get the population of each country (the population is given in crores)]

[Assumption: no two countries will have same population]

Sample Input1:

5

Hong Kong

china

japan

australia

america

135

133

12

2

32

Sample Output1:

australia

japan

america

china

Hong Kong

2

12

32

133

135
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        String coun[] = new String[size];
        int pop[] = new int[size];
        for (int i = 0; i < size; i++) {
            coun[i] = sc.next();
        }
        for (int i = 0; i < size; i++) {
            pop[i] = sc.nextInt();
        }
        String sort_coun[] = new String[size];
        int[] sort_pop = Arrays.copyOf(pop, size);
        Arrays.sort(sort_pop);
        for (int i = 0; i < size; i++) {
            int findindex = findindex(pop, sort_pop[i]);
            if (findindex != -1)
                sort_coun[i] = coun[findindex];
        }
        for (int i = 0; i < size; i++) {
            System.out.println(sort_coun[i]);
        }
        for (int i = 0; i < size; i++) {
            System.out.println(sort_pop[i]);
        }

    }

    public static int findindex(int[] a, int target) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] == target)
                return i;
        }
        return -1;
    }

}
azro
  • 35,213
  • 7
  • 25
  • 55
  • Remove what error? Edit your question and add a stacktrace of the error. – Amongalen May 26 '20 at 09:17
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – MC Emperor May 26 '20 at 22:35

2 Answers2

1

Just add sc.nextLine(); after int size = sc.nextInt();

sc.nextInt() will not read the new line character after hitting enter. So the first character in count array will be an empty string.

Please find the working code:

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        String coun[] = new String[size];
        int pop[] = new int[size];
        sc.nextLine();
        for (int i = 0; i < size; i++) {
            coun[i] = sc.nextLine();
        }
        for (int i = 0; i < size; i++) {
            pop[i] = sc.nextInt();
        }
        String sort_coun[] = new String[size];
        int[] sort_pop = Arrays.copyOf(pop, size);
        Arrays.sort(sort_pop);
        for (int i = 0; i < size; i++) {
            int findindex = findindex(pop, sort_pop[i]);
            if (findindex != -1)
                sort_coun[i] = coun[findindex];
        }
        for (int i = 0; i < size; i++) {
            System.out.println(sort_coun[i]);
        }
        for (int i = 0; i < size; i++) {
            System.out.println(sort_pop[i]);
        }

    }

    public static int findindex(int[] a, int target) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] == target)
                return i;
        }
        return -1;
    }

}
Joby Wilson Mathews
  • 7,870
  • 2
  • 42
  • 43
0

I don't think Joby's answer is a correct one. The InputMismatchException as in attached picture is something that occurrs if after typing N String values you unexpectedly type something that's not a number. And the problem still pops up even with the accepted answer's code. The thing is, with this code:

    for (int i = 0; i < size; i++) {
        pop[i] = sc.nextInt();
    }

you will always get an InputMismatchException for anything else than a number. To get rid of the error, it needs to be replaced with something alomg the lines of:

    int validInputCount = 0;
    while (validInputCount < size) {
        try {
            String input = sc.next();
            pop[validInputCount] = Integer.parseInt(input);
            validInputCount++;
        } catch (NumberFormatException e) {
            System.out.println("Please enter a valid integer number:");
        }
    }
pafau k.
  • 1,610
  • 11
  • 20