Function that saves any possibility of a root diagram in an array

I want to return any possibility of something like a root diagramm in an one dimensional array. It is not knwon how many branches the root (or tree) has, so I probably need to use something like recoursion. I basically want to use recoursion until it reaches one end of a branch and save that in an array and repeat that process until all possible ends are in the array and then I want to return that array. Is there a good solving for that problem, or do I need to make two functions, one returning one ending and one returning all these endigns in an array?
Thanks for helping me out,
bb :*

P.S.: I do it in Java

JSONObject return empty:false

After some researchs, I didn’t have found any solutions to this problem:
when I create a JSONObject (org.json) from a file, it return “empty”:false. Why does it return this and how can I fix it?

Java:

JSONObject config = new JSONObject(Files.readAllLines(Paths.get("config/maj.json")));

JSON:

{"FyloZ":"0"}

Files.readAllLines is working return the right value.

Thanks!

Boolean use in an if statement

public static int search(int[] a, int target)
{
    int i=0;
    boolean found = false;
    while((i<a.length) &&  ! found)
    {
        if (a[i] == target)
        {
        found = true;   
        }
        else i++;
    }
    if (found) return i;
    else return -1;
}

I dont understand the if statement part. So how i am reading it in my head is found is set to false. If not found…so if not false (since found = false), do whatever. So basically im reading it as a double negative and seeing if (true) dow whatever. But it doesnt make sense. I know its not an inifite loop and it runs fine but I dont get the logic, it must not be a double negative. Thanks!

EDIT:
So i get that we could just return i, much easier yes I agree. I just am having trouble with the logic of the boolean value being used in the loop with the not “!” symbol.

Basically if i wrote this i would say (ignoring everything else)

found = true //found is true to begin with
while (!found) //while not true
continue to next index //continue until ….actually i’m getting very confused now because to break the loop we would continue until found is false which logically is backwards

Android Dagger: I can’t provide an external class without @Inject constructor

I’m writing an Android app with use of Dagger and Kotlin.

I want to inject java.text.SimpleDateFormat class into my class DateHelper

@Singleton
class DateHelper @Inject constructor(var dateFormat: SimpleDateFormat) {
...
}

I have a module, which @Provides SimpleDateFormat.

@Module
class DateModule {
    @Singleton
    @Provides
    fun dateFormat(): SimpleDateFormat {
        return SimpleDateFormat("yyyy-MM-dd", Locale.US)
    }
}

and a Dagger component:

@Singleton
@Component(modules = arrayOf(MyAppModule::class, DateModule::class))
interface MyAppComponent {
  ...
}

Lastly my Application:

open class MyApplication : Application() {
    private lateinit var myAppComponent: MyAppComponent

    override fun onCreate() {
        super.onCreate()
        myAppComponent = createGraph()
    }

    open fun createGraph(): MyAppComponent {
        return DaggerMyAppComponent.builder().myAppModule(MyAppModule(this)).build()
    }

    fun getAppComponent(): MyAppComponent {
        return myAppComponent
    }

}

Now, I’m trying to @Inject DateHelper into my Fragment:

class CoffeeNoteFragment : Fragment(), DatePickerDialog.OnDateSetListener {
    @Inject lateinit var dateHelper: DateHelper
    ...
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        (activity.application as MyApplication).getAppComponent().inject(this)
    }
}

Unfortunately I’m getting compile-time error

error: [com.example.mateusz.coffeenotes.application.MyAppComponent.inject(com.example.mateusz.coffeenotes.CoffeeNoteFragment)] java.text.SimpleDateFormat cannot be provided without an @Inject constructor or from an @Provides-annotated method.
  java.text.SimpleDateFormat is injected at
      com.example.mateusz.coffeenotes.database.DateHelper.<init>(arg0)
  com.example.mateusz.coffeenotes.database.DateHelper is injected at
      com.example.mateusz.coffeenotes.CoffeeNoteFragment.dateHelper
  com.example.mateusz.coffeenotes.CoffeeNoteFragment is injected at
      com.example.mateusz.coffeenotes.application.MyAppComponent.inject(arg0)

I don’t understand why I’m getting this error, since I’m @Providing the SimpleDateFormat. Obviously I cannot add @Inject constructor, since I don’t own the SimpleDateFormat class.

Any help what I’m missing?

Internationalization in Java

I try to use I18N in my program but it doesn’t work properly. Console doesn’t show German symbol, just “?” instead of it.

Here is my code:

import java.util.Locale;
import java.util.ResourceBundle;

public class LocaleDemo {

public static void main(String[] args) {

            Locale l = new Locale("ge", "GE");
            ResourceBundle r = ResourceBundle.getBundle("Bundle_ge");
            displayValues(r);
        }

public static void displayValues(ResourceBundle bundle) {
    System.out.println("hello message:" + bundle.getString("wish"));
}
}

File Bundle_ge.properties has this text: wish=großer

And here is the result:

enter image description here

What is my mistake? Thank you in advance.

Background that triggers notification outside UI

I have a project I am working on where I am using JSON to pull some data from a website. I want to send a notification to the user outside the UI when a certain part of the data is too low.

I have looked around and the best thing I have found so far is notification but I dont know how to make that run in the background like a service?

thank you in advance

Populate a RadioGroup with RadioButtons from a string-array

I have a layout that includes an empty RadioGroup.

I’d like to populate this RadioGroup with the items from a string-array that I have defined in strings.xml.

The array in strings.xml looks like this:

<string-array name="currency_symbols">
    <item>$ - Dollar</item>
    <item>€ - Euro</item>
    <item>£ - Pound</item>
    <item>¥ - Yen</item>
    <item># - Other</item>
</string-array>

I am then trying to create a RadioButton and add it to the RagioGroup with teh following:

RadioGroup currencySettingRadioGroup = (RadioGroup) settings_dialog.findViewById(R.id.rg_currency_symbol);
currencySettingRadioGroup.removeAllViews();

RadioButton rb = new RadioButton(this);
String[] currency_symbols_options_array = getResources().getStringArray(R.array.currency_symbols);
for ( String this_currency_option: currency_symbols_options_array ) {
    rb.setText(this_currency_option);
    currencySettingRadioGroup.addView(rb);
}

The currencySettingRadioGroup.removeAllViews(); was added because I’m getting the following error, but it makes no difference:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

The line that is apparently causing the problem is the currencySettingRadioGroup.addView(rb); line…

How do I do this properly?

getting inputs one after the other in from one JTextField java

I’m new to Java, and I’m working on what is supposed to be a basic representation of a bookstore, where a user can add books, textbooks or workbooks to the bookstore(an arraylist). I have to use a GUI for this, and the only elements allowed are a JTextArea, and a JTextField. My problem is that in the actionlistener, the initial input in the textfield just becomes the input for the rest of the prompts that follow it, without allowing the user to put anything in. Is there a way to fix this? Below is a snippet of my code.

public static void main(String[] args){

    BookStore b = new BookStore();
    b.setVisible(true);

}
String input;
public BookStore() {

    super("Bookstore");
    setSize(800, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridLayout(1, 2));

    textField = new JTextField(25);
    panel = new JPanel();
    textField.addActionListener(new ActionListener(){
        @Override 
        public void actionPerformed(ActionEvent ae) {
            /*
                The majority of your code should be written here.
                Note that this scope has access to the fields textField
                and textArea.
            */
            //input = textField.getText();

            if(Integer.parseInt(textField.getText()) == 1) {
                bookSubMenu();
                textField.setText(null);
                input = textField.getText(); //this just uses the input already present in the JTextField; how can i fix that
                if(Integer.parseInt(input) == 1) {
                    textArea.append("Enter the title of the book: ");
                    tempTitle = textField.getText();//This also uses the input from the original getText() without allowing any input to be entered
                    textArea.append(tempTitle);
                    ret = addBook(BT_BOOK, tempTitle);
                }
                //getBookInfo();
                //textField.setText("");
            }
        }

How to execute a python script from a servlet?

I have written the following code for the servlet:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Summarize extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        String str = req.getParameter("txt");

        try {
            Process p = Runtime.getRuntime().exec("python test.py");
            p.waitFor();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            StringBuffer buf = new StringBuffer("");
            String line = null;
            System.out.println(p.toString());
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                buf.append(line);
            }
            System.out.println(buf.length());
            out.println("this is summarize class");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This code won’t return anything but If I write the same code in a simple java class the code works perfectly fine.

I have also checked the python script, it’s running perfectly fine.
Is there any way to fix this?

Code runs fine on one PC. But gives null pointer on another

So, I have a piece of code which works fine on a desktop but not on my laptop. I am using java’s RecursiveAction class to create a Flyweight to compute fibonacci number. The exact same code runs fine on one pc but gives a null pointer exception on another.

Also lets say I run it about 10 times on my pc and it will work 1 out of those 10 times.

Regards