Help me making this simple calculation in my extension

hello @Mohamed_Tamer, @Srrazmi, @hammerhai, @golumaths100, @Peter now i have completely changed my topic now i want to do a simple calculation like-
return x(variable) + 273.15

Help me Please!

Be patient and someone will come along.

2 Likes

public static void main(String[] args)

{

// initializing ArrayList

List<Integer> numbers = Arrays.asList( 1 , 2 , 3 ,

4 , 5 , 6 , 7 , 8 );

// For Loop for iterating ArrayList

for ( int i = 0 ; i < numbers.size(); i++)

System.out.print(numbers.get(i) + " " );

}

I am not a java expert but as far as I know, this is how your given blocks can be written in Java.

3 Likes

thanks @golumaths100 i will try this

1 Like

Three of those lines they gave you are incorrect.

oh can someone please tell the correct version please

Is there any extension developer that can help me

Manners got you somewhere! Thank you for using them :slight_smile:

FileName.java

package io.kodular.example;

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.runtime.util.YailList;

import java.lang.NumberFormatException;
import java.util.Arrays;
import java.util.List;

@DesignerComponent(
  description = "Description of your extension",
  category = ComponentCategory.EXTENSION,
  iconName = "images/extension.png",
  nonVisible = true,
  version = 1,
  versionName = "0.1.0"
)
@SimpleObject(external = true)
public class FileName extends AndroidNonvisibleComponent {
  public FileName(ComponentContainer container) {
    super(container.$form());
  }

  @SimpleFunction
  public int LoopOverList(YailList list) {
    List listOfNumbers = Arrays.asList(list.toArray());
    int finalResult = 0;

    for (Object itemInList : listOfNumbers) {
      try {
        finalResult += Integer.parseInt(itemInList);
      } catch(NumberFormatException nfe) {
        // Not a number
        nfe.printStackTrace();
      }
    }

    return finalResult;
  }
}

Sorry it took a few minutes, I wasn’t using my IDE :sweat_smile:

1 Like

thank you i will try this also @hammerhai

1 Like

do we need to write
package io.kodular.example;
also
i dont know because i am new

1 Like

Are you using the Appybuilder IDE?

yes i am using appybuilder

Ok, then you will use this…

import com.google.appinventor.components.runtime.util.YailList;
import java.lang.NumberFormatException;
import java.util.Arrays;
import java.util.List;

...

// Place this inside of your extension class
@SimpleFunction
public int LoopOverList(YailList list) {
  List listOfNumbers = Arrays.asList(list.toArray());
  int finalResult = 0;

  for (Object itemInList : listOfNumbers) {
    try {
      finalResult += Integer.parseInt(itemInList);
    } catch(NumberFormatException nfe) {
      // Not a number
      nfe.printStackTrace();
    }
  }

  return finalResult;
}

The 3 dots ... should not be included after the import statements. Just include the import statements by the rest of where they are (at the top, but below the generated classes). Just follow what Help me making this extension shows and you’ll figure it out!

If you’re unsure of the placement, I’ve given an example in a file!

TestJava.java (1.8 KB)

2 Likes

How Object Convert Into String ???

I just simply wrote the java code of the following procedure because I don’t have knowledge about extensions development. :sweat_smile: :sweat_smile:

2 Likes

@luv.ak.tech wanted items that were everything, BUT integers to be skipped over. Strings are also extended from the Object class which make them classified as an Object.

try {
  finalResult += Integer.parseInt(itemInList);
} ...

Tests to see if the Object is an integer, if it is then it will add to the existing variable. Let me give a better example.

Let’s say finalResult = 0? If I do finalResult += aNumberFromTheList then it will add it to the finalResult instead of me needing to do finalResult = finalResult + aNumberFromTheList. My solution is shorter than doing finalResult = finalResult + aNumberFromTheList.

2 Likes

Ya. I understood it. I misread @Srrazmi comment, that’s why I replied that. I deleted that reply instantly. :zipper_mouth_face:

1 Like

But Code Editor Showing Error That

[javac] %SERVER_WORKSPACE%/appinventor/components/src/io/kodular/foreach/foreach.java:33: error: incompatible types: Object cannot be converted to String

1 Like