2

I'm working on a small program to compare service levels, the user will input the service level 2 times (current and requested) and then the input will be scanned and compared and show a message.

For example:

current = 9*5 NBD (a)
requested = 24*7 SBD (b)

I want to know how in Java I can tell the compiler that (b) is greater than (a) Because I want to use if statement like this

if (b > a) then show message.

I tried to use string.equals, but didn't help me too much. I was not successful to convert string to number to do such comparison.

7
  • 7
    if ( b.compareTo( a ) > 0 ) System.out.println("message"); Commented Dec 27, 2015 at 23:36
  • 1
    What is logic you want to apply for these messages? In other words which part of string you want to use and how you want to use them? Commented Dec 27, 2015 at 23:41
  • 1
    Have you tried using the String.split() and Integer.parseInt() to just do the math yourself? Commented Dec 28, 2015 at 0:39
  • 1
    There are different rules for comparing strings. The default set of collation rules used by String is called a *lexicographical comparison." You should read about those rules so that you understand why you are getting the results you see. The rules are specified in the API documentation for String.compareTo(). If you require a specific set of rules for collation, then you should write your own comparator that is based on the rules you need. Commented Dec 28, 2015 at 1:44
  • 1
    As an aside, storing separate field values in a String is a bad practice in Java. The Object-oriented way to do what you're doing would involve defining your own ServiceLevel class whose instance fields would contain the values that are currently in your String. Having those fields represented in that way would let you define your own custom collation rules easily by overriding Object.equals(). Commented Dec 28, 2015 at 1:52

3 Answers 3

2

Try following statement if(a.compareTo(b) > 0);

First thing: you can't override String.compareTo(), because it's final. you can create class with String field and write compareTo() for this class. This is not best idea.

But you can compare two strings by putting them into array and creating implementation of Comparator interface in sort() method.

    String current = "9*5 NBD";
    String requested = "24*7 SBD";

    String[] test = {current, requested};
    Arrays.sort(test, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            //Your impl goes here
            return 0;
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

0

Where do these strings come from? Surely they must be from some kind of table that contains the service level details together with the cost of subscribing to that service level. What you want to check is whether the required service level costs more than the service level the client already has. Suppose the service level details come from a Map<ServiceLevel, BigDecimal> that gives the cost for a certain service level. Then all you need to do is:

BigDecimal costOfCurrentSL = serviceLevelCosts.get(currentSL);
BigDecimal costOfRequiredSL = serviceLevelCosts.get(requiredSL);
if (costOfRequiredSL.compareTo(costOfCurrentSL) > 0) {
    // ... tell client he needs to purchase a top-up
}

Comments

0

Thank you all for the willing to help :) After a lot of thinking I found another way which helped me a lot I created 2 new integers and called them Values of what I need. and used if statement, that if the entered is 9*5 NBD so the value will be zero, and if it is SBD, the value will be 1 and so on, then created new if statement to compare the values and show me a message if the A is greater than B, and it really worked. Here is a part of my code

String WA_SLA = "", REQ_SLA = "";
int Va_WA_SLA = 0, Va_REQ_SLA = 0;
  if(WA_SLA.equalsIgnoreCase("9*5 SBD"))
  {
      Va_WA_SLA = 1;
  }
  if(REQ_SLA.equalsIgnoreCase("9*5 NBD"))
  {
      Va_REQ_SLA = 0;
  }
      if(Va_WA_SLA > Va_REQ_SLA)
  {
      JOptionPane.showMessageDialog(frame,"Warranty SLA is Higher than Requested SLA " ,null, JOptionPane.INFORMATION_MESSAGE);
  }

Thaaaaaaaaaaaaank you a lot

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.