user11105104
user11105104

Reputation:

Java - To find the Second Highest Digit

I need to:

Write a function that accepts a string and returns the second highest numerical digit in the input as an integer.

The following rules should apply:

For example:

This is my code currently:

public class Solution {
    public static int secondHighestDigit(String input) {

    try{

        int k = Integer.parseInt(input); 

        char[] array = input.toCharArray(); 

        int big = Integer.MIN_VALUE;
        int secondBig = Integer.MIN_VALUE;


        for(int i = 0; i < array.length; i++){

            System.out.println(array[i]);

            for(int n = 0; n < array[i]; n++){
                if(array[i] > big)
                {
                    secondBig = big;
                    big = array[i];
                }else if(array[i] > secondBig && array[i] != big){
                    secondBig = array[i];
                }
            }

        }

        System.out.println(secondBig);

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

}

Tests:

import org.junit.*;
import static org.junit.Assert.*;

public class Tests
{

    @Test
    public void test1()
    {
        Solution solution = new Solution();

        assertEquals(3, solution.secondHighestDigit("abc:1231234"));
    }

    @Test
    public void test2()
    {
        Solution solution = new Solution();

        assertEquals(3, solution.secondHighestDigit("123123"));
     }
}

The program should print 3 for abc:1231234 and 123123 but instead it is returning -1 for both.

I am lost are where to go from here. I would be grateful if someone could help. Thanks.

Upvotes: 0

Views: 1099

Answers (3)

hossein
hossein

Reputation: 629

Run this code:

private static int secondHighestDigit(String input) {

    String pattern = "(\\d+)";
    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);
    // Now create matcher object.
    Matcher m = r.matcher(input);
    if (m.find( )) {
        String foundedDigits = m.group(0);
        System.out.println("Found value: " + m.group(0) );
        char[] characters = foundedDigits.toCharArray();
        List<Integer> integers = new ArrayList<>();
        for(char c : characters){
            integers.add(Integer.parseInt(c +""));
        }
        if(integers.size()==1)
            return -1;
        Collections.sort(integers);
        System.out.println("One Biggest int: " + integers.get(integers.size()-1));
        System.out.println("Two Biggest int: " + integers.get(integers.size()-2));
        return integers.get(integers.size()-2);
    }else {
        System.out.println("NO MATCH");
        return -1;
    }
}

Upvotes: 0

b.GHILAS
b.GHILAS

Reputation: 2303

One possible solution is to remove all non numeric characters and sort the string

public int secondGreatest(String s) {
    String newStr = s.replaceAll("[^0-9]*", "");
    if (newStr.isEmpty() || newStr.length() == 1) {
        return -1;
    } else {
        char[] c = newStr.toCharArray();
        Arrays.sort(c);
        return c[newStr.length() - 2] - '0';
    }
}

Upvotes: 2

sleepToken
sleepToken

Reputation: 1847

Don't try to convert your string to an Integer at first - since you already know that it's possible it may contain some non-numeric characters.

Instead, parse through each character in the String, if it is an integer, add it to a list of integers. Let's use an ArrayList so we can add to the list and have it dynamically sized.

Your code should look something like this: (giving you some work to do)

// Iterate through characters in the string
// You could also use "substring" so you don't have to deal with chars
for (int i = 0; i < s.length(); i++){
  char c = s.charAt(i);        
  if(Character.isDigit(c)) {
    // Convert c to an int appropriately and add it to your list of ints
  }
}

// Sort the list of ints in descending order

// Now, we write a seperate method
public int getNthBiggest(int n) {
  // Return the nth biggest item (you could just use 2)
}

Checking if the string has 0 or 1 "digits" should be trivial enough for you, if you've gotten this far.

Upvotes: 0

Related Questions