CountryValue.java.html

  1  /**
  2     Describes a value that is associated with a country
  3  */
  4  public class CountryValue
  5  {
  6     private String country;
  7     private double value;
  8  
  9     /**
 10        Constructs a CountryValue from an input line.
 11        @param line a line containing a country name, followed by a value
 12     */
 13     public CountryValue(String line)
 14     {
 15        int i = 0; // Locate the start of the first digit
 16        while (!Character.isDigit(line.charAt(i))) { i++; }
 17        int j = i - 1; // Locate the end of the preceding word
 18        while (Character.isWhitespace(line.charAt(j))) { j--; }     
 19        country = line.substring(0, j + 1); // Extract the country name
 20        value = Double.parseDouble(line.substring(i).trim()); // Extract the value
 21     }
 22  
 23     /**
 24        Gets the country name.
 25        @return the country name
 26     */
 27     public String getCountry() { return country; }
 28  
 29     /**
 30        Gets the associated value.
 31        @return the value associated with the country
 32     */
 33     public double getValue() { return value; }
 34  }