-3

I need help with the java. As I have such a character in a line 1.11 € 1.10 € 1.02 € 0.66 € 0.50 and I need to add these characters to separate variables like double i = 1.11 double a = 1.10 and so on.

try {
    final Document document = Jsoup.connect("http://www.jozita.lt/").get();
    Elements ele = document.select("div#fuel_price_table_silale_1");
    final String title = ele.select("td.price").text();          
    System.out.println(title);
} catch (IOException ex) {
    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
foreach
  • 13
  • 2
  • 1
    You can use `String.split()` and `String.trim()`. Also, do you know howmany prices you have? If you don't, use a `List` instead of several variables. – Arnaud Denoyelle Jan 17 '18 at 18:54
  • 2
    You can't just dump your requirements and expect us to write code for you. What have you tried? Where *specifically* are you stuck? – shmosel Jan 17 '18 at 18:55
  • I'm trying to get data from the internet and use them – foreach Jan 17 '18 at 19:01

3 Answers3

2

There are a number of parts to this problem.

  1. Fetch the data
  2. Parse the individual items from the data
  3. Convert the items into useable numbers

You have already done the first part using JSoup.

Secondly, use String.split() to parse the values from the JSoup response.

String[] values = title.split("€");

Finally, converting the values to doubles is a little more challenging, as the number format (with a comma) is not parsed by Double.valueOf(). Instead, we need to use a NumberFormat, and parse the strings using that.

NumberFormat format = NumberFormat.getInstance(ITALY); // Italy chosen as it uses the comma format
Number number = format.parse(value.trim());
Double d = number.doubleValue();

So putting it all together gives:

String[] values = title.split("€");

List<Double> doubles = new ArrayList<>();

NumberFormat format = NumberFormat.getInstance(ITALY);

for (String s : values) {
    Number number = format.parse(s.trim());
    doubles.add(number.doubleValue());
}

System.out.println(doubles);

With the output being [1.11, 1.1, 1.02, 0.66, 0.5].

starf
  • 1,063
  • 11
  • 15
1

You can have a List of the values in the expression using a combination of String.split and Stream utilities:

List<Double> values = Arrays.stream(str.split("€")) // get a String[] and then a Stream<String>
        .map(Double::valueOf)                       // cast every value to double
        .collect(Collectors.toList());              // get a list of the double values

EDIT

As indicated by @AjahnCharles we could have used map(Double::parseDouble), but it returns a primitive double, hence there would be some overhead to get a Stream<Double>. Read this to see the difference.

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
  • I've removed my answer since I think yours is the optimal solution. I think you should mention why you use `Double::valueOf` (and not `Double::parseDouble`) to go directly to the boxed `Stream` rather than a primitive `DoubleStream`. +1 from me :) – charles-allen Jan 17 '18 at 19:23
0

I can help you out with the splitting and conversion part, I wouldn't suggest it is a good idea to create a new variable for every item/value. It's better if it is saved in a list.

 String data = "1.11 € 1.10 € 1.02 € 0.66 € 0.50";
 List<> dataList  = new ArrayList<>();

 //Splitting based on key
 dataList = data.split("€");

 for(int i=0;i<dataList.size();i++){
     //Data is stored in a list in string format.
     double d  = Double.parseDouble( dataList[i]);
     System.out.println(d);
 }

Note:: please ignore any formatting mistakes or typos as I have typed using phone

FAIZAN
  • 271
  • 1
  • 6
  • 16