0

I have 1 string which has to be between a specific range.

The range of the values are: "620/75R38" - "1050/50R38" as you guys can see, it should fit within the range. Yet i don't know how to put this in java code who can help me? I tried already with String.compareTo() function but somehow it doesn't give the right answeres.

EDIT

Here is what i've already tried.

private subMaat = "";
private maat = "650/65R38";
if(maat.contains("R")){
        subMaat = maat.substring(0, maat.lastIndexOf("R"));
    } else if (maat.contains("-")){
        subMaat = maat.substring(0, maat.lastIndexOf("-"));
    }

if(subMaat.compareTo("620/75") >= 0 && subMaat.compareTo("1050/50") <= 0){
   //do something
}
Baklap4
  • 3,914
  • 2
  • 29
  • 56
  • 1
    This is a very specific requirement. Can you post expected input/output? Does `65R38` fall between `75R38` and `50R38`? – Sotirios Delimanolis Aug 28 '13 at 12:29
  • I've posted my attempted code. My expected output would be that the last if statement would be true, yet it ain't true so it gives me the wrong output. (it's because of the "1050/50" that's what i know. Because compare to sees the strings not as numbers which is reasonable. But how to get it right? – Baklap4 Aug 28 '13 at 12:38

2 Answers2

0

How to check if an integer is in a given range? and compare both sizes like this:

 if ((size1[1] > 650 && size2[1]< 1050) && (size1[2] > 50 && size2[2]< 75)) { then }

Or you could put all the sizes in an ArrayList, given it's not that big (I guess these sizes are for tires?)

Community
  • 1
  • 1
Kelevra
  • 95
  • 10
  • Yes these sizes are for tires indeed, which i need to compare with static data given from some table to see if it's possible to repair the tire yes or no. – Baklap4 Aug 28 '13 at 12:39
0

Try this

   String lower="620/75R38";
   String upper= "1050/50R38";

   String str="50/65R38";


   if(str.compareTo(lower)>0 && upper.compareTo(str)<0){
       System.out.println("inside the range");
   }
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Why `"a".compareTo("b")`? – BackSlash Aug 28 '13 at 12:39
  • Thanks this seems to work, however how does it work from the inside? – Baklap4 Aug 28 '13 at 12:48
  • @Baklap4 check this out, `public int compareTo(String anotherString) { int len1 = value.length; int len2 = anotherString.value.length; int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }` – Ruchira Gayan Ranaweera Aug 28 '13 at 12:51