Answers for "java int with specific didgits"

1

has integer java

// To Check The Given Number is Integer Or Not In Java
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter The Number");
		System.out.println(sc.hasNextInt());
	}
}
Posted by: Guest on February-04-2021
1

compare string integer java

/**
    * Similar to compareTo method But compareTo doesn't return correct result for string+integer strings something like `A11` and `A9`
*/

private int newCompareTo(String comp1, String comp2) {
  // If any value has 0 length it means other value is bigger
  if (comp1.length() == 0) {
    if (comp2.length() == 0) {
      return 0;
    }
    return -1;
  } else if (comp2.length() == 0) {
    return 1;
  }
  if (TextUtils.isDigitsOnly(comp1)) {
    int val1 = Integer.parseInt(comp1);
    if (TextUtils.isDigitsOnly(comp2)) {
      int val2 = Integer.parseInt(comp2);
      return Integer.compare(val1, val2);
    } else {
      return comp1.compareTo(comp2);
    }

  } else {

    int minVal = Math.min(comp1.length(), comp2.length()), sameCount = 0;

    // Loop through two strings and check how many strings are same
    for (int i = 0;i < minVal;i++) {
      char leftVal = comp1.charAt(i), rightVal = comp2.charAt(i);
      if (leftVal == rightVal) {
        sameCount++;
      } else {
        break;
      }
    }
    if (sameCount == 0) {
      return comp1.compareTo(comp2);
    } else {
      String newStr1 = comp1.substring(sameCount), newStr2 = comp2.substring(sameCount);
      if (TextUtils.isDigitsOnly(newStr1) && TextUtils.isDigitsOnly(newStr2)) {
        return Integer.compare(Integer.parseInt(newStr1), Integer.parseInt(newStr2));
      } else {
        return comp1.compareTo(comp2);
      }
    }
  }
}
Posted by: Guest on November-21-2019

Code answers related to "java int with specific didgits"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language