Friday, 15 February 2013

Java Comparator on strings that contain integers -


I am trying to write a compiler to sort that string list that he or she has. East. H3232GHSD3 and H56RFRSFR4, the integer in the first string is 32323, whereas the second string integer is 564, so the second string is less than the previous one.

This is my code

  import java.util *. // Based on the strings, string int compact tool insertion in the string and lieutenant; String & gt; Stripe {@ Override Public Ent (String S1, String S2) {// String S1 = s1.replaceAll ("[^ \ \ d.]", ""); S1 = s1.replaceAll ("[^ \ \ d.]", ""); // change string integer int l1 = integer. From the end (s1); Int l2 = integer.parseant (s2); If (L1> L2) {return 1; } And if (L1 Lieutenant; L2) {return -1; } Return 0; }} Sample of Public Sector {Public Stable Zero Main (String [] Args) {List & lt; String & gt; Random string = new arreelist & lt; String & gt; (); RandomString.add ("HA4ZNV0WE1"); RandomString.add ("A3XHN20WE1"); RandomString.add ("D4VH3V0WE1"); collection. Support (Random String, New Intcomputer ()); For (string r: random string) {System.out.println (R); }}}   

and I get this error

  exception in thread "main" java.lang.NumberFormatException: for input string: "HA4ZNV0WE1" Java.lang.NumberFormatException.forInputString (unknown source) at java.lang.Integer.parseInt (unknown source) IntComparator on Int.Comparator.compare (sample.java.1) java.lang.Integer.parseInt (unknown source) On. Compare on Java.util.im (sample.java:1) java.util.TimSort.sort at java.util.TimSort.sort (unknown source) on java.util.Arrays unknown source (unknown source) java.   

Thank you, use .imSort.countRunAndMakeAscending (unknown source) on Sample.main (sample.java36) java.util.Collections.sort (unknown source)

Change your code to a typo -

  // strings s1 Strip non-integer from = s1.replaceAll ("[^ \ \. D." "," "); S1 = s1.replaceAll ("[^ \ \ d.]", "");   

this -

  // string s1 = s1.replaceAll ("[^ \\ d.]", "" Strip non-integer " ); S2 = s2.replaceAll ("[^ \ \ d.]", ""); // In your code, you have also written 1 here.   

I'm assuming That you have copied your first line and pasted it, and forgot to change the variable name. This is why sometimes it is called.

No comments:

Post a Comment