3

I have to write a function that accepts a string and returns the second highest numerical digit in the user input as an integer. These are the rules I have to follow:

  • Inputs with no numerical digits should return -1
  • Inputs with only one numerical digit should return -1
  • Non-numeric characters should be ignored
  • Each numerical input should be treat individually, meaning in the event of a joint highest digit then the second highest digit will also be the highest digit

My code:

public static int secondDigit(String input) {

    try {
        int userInput = Integer.parseInt(input);

        char[] array = input.toCharArray();

        int biggest = Integer.MIN_VALUE;
        int secondBiggest = Integer.MIN_VALUE;

        for(int i =0; i < array.length; i++) 
        {
            System.out.println("Array: "+array[i]);

            for(int j = 0; j < array[i]; j++)
            {
                if(array[i] > biggest) 
                {
                    secondBiggest = biggest;
                    biggest = array[i];
                }
                else if(array[i] > secondBiggest && array[i] != biggest)
                {
                    secondBiggest = array[i];
                }
            }

        }

        System.out.println(secondBiggest);

        return userInput;
    }catch(Exception e) {
        System.out.println("-1");

    }
    return -1;
    // Your code goes here
}

"abc:1231234" should return 3
"123123" should return 3

Currently this is the code and it returns 51 when i give it an input of "123123".

luk2302
  • 55,258
  • 23
  • 97
  • 137

4 Answers4

6

Your code is almost correct, the only problem is that what you print is the char at that position, the char is '3' and has an ascii value of 51. To fix this you need to parse an int from the char.

luk2302
  • 55,258
  • 23
  • 97
  • 137
1

Why not just write a function to get the n-th biggest int?

Iterate over your array, if you find an int add it to an ArrayList. Then sort the ArrayList using Collections.

Now, you can grab the second-biggest (or nth-biggest) by accessing that position of your sorted ArrayList.

sleepToken
  • 1,866
  • 1
  • 14
  • 23
1

To convert from the ascii value to the numeric value you can substract the value of the character '0'.

int decimalValue = asciiValue - '0';

If you use a Stream to solve your problem, the solution would be very concise:

    public static int secondDigit(String input) {
        return input.chars() // Get the Stream from your input
                .filter(i -> i >= '0' && i <= '9')  // Filter non-numeric values
                .boxed()  // Convert to Integer to sort the Stream
                .sorted(Comparator.reverseOrder()) // Sort in descending order
                .skip(1)  // Skip the maximum to get the second value
                .map(i -> i - '0') // Convert the ascii value to decimal
                .findFirst()  // Get the second maximum value
                .orElse(-1);  // If not exists, returns the -1 value
    }
David SN
  • 3,389
  • 1
  • 17
  • 21
0

I agree with luk2302 as if you compare a char with an int. In this case, you should use Character.getNumericValue() if you want to convert, say '6', to the integer 6.

Laser Infinite
  • 253
  • 1
  • 15