Search code examples
javaarraysandroid-studio-2.0

Is it possible to stream double values from an array using scanner?


I have a method that needs single values as the input, but I only have the information in an array. I have searched and found out that Scanner can stream values but only in string form. So I thought I should change the array into string and then convert it back into double before returning the value.

Here is what I had written.

    double E;
    public double StreamValue (double[]g2){
        Scanner s = new Scanner(Arrays.toString(g2)); //PROBLEM HERE
        while (s.hasNext()){
            E = Double.parseDouble(s.next());
        }
        return E;
    }

When I ran the code, it failed. My question is, is the idea right but I'm missing any part, or it's completely invalid?

Edited:

I need to feed the streaming values into a methode that will analyze each values and return an int.

This is kind of a short example of it:

 public int EventAnalyzer(double E) {
   if (E > c_max)
      a = 0;
   else
     a = 1;
   return a;
   }
The returned value will be used for this method:

private void isDetected (int a){
        CharSequence t0 = "No breathing";
        CharSequence t1 = "Normal breathing";
        TextView textView = (TextView)findViewById(R.id.apnea);

        if(a==0)
            textView.setText(t0);
        if(a==1)
            textView.setText(t1);
       
    }


Solution

  • @Alexander Anikin had answered the question in the comment. I'm working on the Arrays.stream but it requires API 24 while I have API 19. I'm looking for other way possible. Thanks everyone for the responses!