2016-05-19 4 views
0

Ich habe eine HashMap, die Schlüssel und Wert als 'String' enthält. Ich bekomme diese Werte von einer Webseite in meinem Selen Automation Script.Alphanumerische Zeichenfolge in Ganzzahl konvertieren?

mein hashmap hat folgende

<Italy, 3.3 millions> 
<Venezuela, 30.69 millions> 
<Japan, 127.1 millions> 

Wie kann ich alle Zeichenfolge alphanumerische Werte auf ganze Zahlen konvertieren, so dass ich auf dem hashmap Sortierung anwenden können?

Ich muss das Wort "Millionen" anzeigen.

+0

Warum möchten Sie "milions" anzeigen, wenn Sie an erster Stelle Millionen in Integer umgewandelt haben? – sauumum

+0

Ich habe noch nichts in Integer umgewandelt. Ich habe auf das Element auf der Seite zugegriffen und die getText() -Eigenschaft verwendet, um den Text zu erhalten. Jetzt, wenn ich es drucke, zeigt es 3,3 Millionen. Alles, was ich will, ist die Zahl (3.3) des Strings '3.3 Millions' in Integer zu konvertieren, so dass ich meine hashmap sortieren und die Top 3 Länder nach ihrer Grundgesamtheit finden kann. Mein Ziel ist es, die Ergebnisse zu protokollieren als: Beispiel, Top bevölkertes Land ist (ein Land) mit 3,3 Millionen Menschen. – Uziii

+0

Bis hierher gekommen. Jetzt möchten Sie 3,3 Millionen auf 3,3 * 1000000 ändern und in Collection stellen, damit Sie diesen Wert sortieren können. Sobald der Wert sortiert ist, möchten Sie den Wert in einer bestimmten Reihenfolge ausgeben (aufsteigend/absteigend). Hier, was Sie "3,3 Millionen" oder "3300000" drucken möchten? – sauumum

Antwort

0

Wenn Sie nur Millionen haben, können Sie so etwas wie dieses

String str = "3.3 Millions"; 
    String[] splitted = str.split(" "); 
    double i = Double.valueOf(splitted[0])*1000000; 
    System.out.println(i); 

oder machen Sie Ihre Berechnung in Abhängigkeit von der Teilkette versuchen

+0

Ich muss das Wort "Millions" – Uziii

1

Soweit ich aus Ihrer Frage verstehen, was Sie tun müssen, um sein in der Lage, diese Werte zu sortieren, also was Sie brauchen, ist ein Comparator. Hier

ist die Comparator, die den Trick tun könnte:

Comparator<String> comparator = new Comparator<String>() { 
    @Override 
    public int compare(final String value1, final String value2) { 
     return Double.compare(
      Double.parseDouble(value1.substring(0, value1.length() - 9)), 
      Double.parseDouble(value2.substring(0, value2.length() - 9)) 
     ); 
    } 
}; 
System.out.println(comparator.compare("3.3 millions", "30.69 millions")); 
System.out.println(comparator.compare("30.69 millions", "30.69 millions")); 
System.out.println(comparator.compare("127.1 millions", "30.69 millions")); 

Ausgang:

-1 
0 
1 
+0

anstelle von Doppel durch subtracting sie anzeigen und casting zu int, ich empfehle Ihnen, Methode 'Double.compare' – user902383

+0

@ user902383 nice one thx –

+0

@ user902383 Antwort aktualisiert thx verwenden –

0

nicht sicher, ob dies ist, was Sie suchen .. Wenn ich es richtig Sie Ändern Sie Ihre Karte von

<String, String> to <String, Double>. 

Siehe meine e Beispiel:

import java.text.ParseException; 
import java.util.HashMap; 
import java.util.Map; 

public class NewClass9 { 

public static void main(String[] args) throws ParseException{  
    Map<String,String> oldMap = new HashMap<>(); 
    oldMap.put("Italy", "3.3 millions"); 
    oldMap.put("Venezuela", "30.69 millions"); 
    oldMap.put("Japan", "127.1 millions"); 
    Map<String,Double> newMap = new HashMap<>(); 
    for(String key : oldMap.keySet()){ 
     newMap.put(key, convert(oldMap.get(key))); 
    } 

    for(String key : newMap.keySet()){ 
     System.out.printf("%.0f millions\n" ,newMap.get(key)); 
    } 
} 

private static double convert(String str) {  
    String[] splitted = str.split(" "); 
    return Double.valueOf(splitted[0])*1000000; 
} 
} 
0

Ein bisschen Overkill, aber das sollte erweiterbar sein.

NB: Ich habe nur die Multiplikator-Suche abgedeckt.

/** 
* Possible units and their multipliers. 
*/ 
enum Unit { 
    Unit(1), 
    Hundred(100), 
    Thousand(1000), 
    Million(1000000), 
    Billion(1000000000), 
    Squillion(Integer.MAX_VALUE); 

    private final int multiplier; 

    Unit(int multiplier) { 
     this.multiplier = multiplier; 
    } 
} 

/** 
* Comparator that matches caseless and plurals 
* 
* NB: Not certain if this is consistent. 
*/ 
private static final Comparator<String> COMPARECASELESSANDPLURALS 
     = (String o1, String o2) -> { 
      // Allow case difference AND plurals. 
      o1 = o1.toLowerCase(); 
      o2 = o2.toLowerCase(); 
      int diff = o1.compareTo(o2); 
      if (diff != 0) { 
       // One character different in length? 
       if (Math.abs(o1.length() - o2.length()) == 1) { 
        // Which may be plural? 
        if (o1.length() > o2.length()) { 
         // o1 might be plural. 
         if (o1.endsWith("s")) { 
          diff = o1.substring(0, o1.length() - 1).compareTo(o2); 
         } 
        } else if (o2.endsWith("s")) { 
         // o2 might be plural. 
         diff = -o2.substring(0, o2.length() - 1).compareTo(o1); 
        } 
       } 
      } 
      return diff; 
     }; 

// Build my lookup. 
static final Map<String, Integer> MULTIPLIERS 
     = Arrays.stream(Unit.values()) 
     // Collect into a Map 
     .collect(Collectors.toMap(
       // From name of the enum. 
       u -> u.name(), 
       // To its multiplier. 
       u -> u.multiplier, 
       // Runtime exception in case of duplicates. 
       (k, v) -> { 
        throw new RuntimeException(String.format("Duplicate key %s", k)); 
       }, 
       // Use a TreeMap that ignores case and plural. 
       () -> new TreeMap(COMPARECASELESSANDPLURALS))); 

// Gives the multiplier for a word. 
public Optional<Integer> getMultiplier(String word) { 
    return Optional.ofNullable(MULTIPLIERS.get(word)); 
} 

public void test() { 
    String[] tests = {"Million", "Millions", "Thousand", "Aardvark", "billion", "billions", "squillion"}; 
    for (String s : tests) { 
     System.out.println("multiplier(" + s + ") = " + getMultiplier(s).orElse(1)); 
    } 
}