Home > Mobile >  Incorrect conversion and display in the output
Incorrect conversion and display in the output

Time:10-18

My code should find the smallest digit in a given number, and indicate its position. If there are several such digits, then indicate all positions.

For example,

when entering 1231
Output: position - [1 4]

The digit that is the minimum in the entered number does not need to be displayed. As it seems to me, my problem is in the output.

Help me figure out how to convert correctly, or rather display the number's position. As I understand it, the whole problem is in the line

Integer[] s1 = getMinPos(s);

import java.util.Scanner;
import java.util.LinkedList;

public class test1 {

    public static void main(String[] args) {
        Scanner text = new Scanner(System.in);
        System.out.println("Enter: ");
        int k = text.nextInt();
        String s = Integer.toString(k);
        Integer[] s1 = getMinPos(s);
        System.out.println("position - "   s1);
    }

    static Integer[] getMinPos(String num) {
        LinkedList<Integer> list = new LinkedList<>();
        int maxVal = -1, counter = 1;
        for (char a : num.toCharArray()) {
            if (maxVal < a) {
                maxVal = a;
                list.clear();
            }
            if (a == maxVal) {
                list.add(counter  );
            }
        }
        return list.toArray(new Integer[0]);
    }
}

CodePudding user response:

You main issue is this line:

System.out.println("position - "   s1);

You cannot print an array like that. When you try to print an array variable, it prints the object reference. Not the elements of the array. You need something like this to print it.

System.out.println("position - ");
for (int i : s1) {
    System.out.print(i   " ");
}

Check how it prints the array by iterating through the elements.

When you print using this, you may find that your logic is not finding the position of the minimum value, but the position of the maximum value.

If you want to find the positions of the minimjum digit, the logic should be something like this:

static Integer[] getMinPos(String num) {
    LinkedList<Integer> list = new LinkedList<>();
    int minValue = '9'   1, counter = 1;
    for (char a : num.toCharArray()) {
        if (a < minValue) {
            minValue = a;
            list.clear();
            list.add(counter);
        } else if (a == minValue) {
            list.add(counter);
        }

        counter  ;
    }
    return list.toArray(new Integer[0]);
}

Notice here that since you are comparing the char values, I have used the char value of 9 ('9') and added 1 to make sure that the starting minValue is always higher than any digit.

Then, there's another issue where you do not add the first index to your list. I've fixed that too.

You also do not increase the counter when you find a value greater than a. (Or in the fixed version, when the value of a is greater than minValue. That will be causing a bug, where your program will return incorrect indexes. So, the incrementing the counter is moved out from the if-else blocks.

  • Related