Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a java program to run within the Android Studio app

I am just a side programmer, working on little tiny projects for some extra coin. Nothing to big.

But I got this code with an if and else if statement that I am trying to run on the GUI on Android Studio, I want the code to display within the actual android app. I don't know where to put the code in or how to call it in the .java file, so that it can run in the android app on the Studio program. I was hoping maybe someone with more experience with Android Studio can help me figure out what my next steps are. I do apologize if I am vague with my problem, I am not quite great at explaining the issue.

public class Main {

    public static void main(String[] args) {

        String lit;

        Scanner scan = new Scanner(System.in);

        System.out.println("Set the thermostat to call for heating, does the pilot stay lit? y/n");
        lit = scan.nextLine();

        if (lit.equals("y")) {
            System.out.println("Great. So it works.");

        } else if (lit.equals("n")) {
            System.out.println("Check the thermocouple.");
        }
    }
}

This is the code I have written

This is where I am trying to figure out how to place in the .java file

like image 273
rodriguezj09 Avatar asked Apr 26 '26 15:04

rodriguezj09


2 Answers

Android apps work in a different way to 'normal' java programs, the concept of System.in doesn't really exist. The main function isn't something that you call manually. User input comes from having widgets on the screen like an EditText to read the data.

I'd start here to get a grasp on a basic app and then you should be able to see how you could modify your sample above to an app.

like image 106
Chris Avatar answered Apr 28 '26 03:04

Chris


I see you need to make an actual GUI app with this.

I would suggest you to see some tutorials on android studio first.

To build this app, first put in 2 Textview, and 2 buttons in the activity_main.

In the properties panel on the left, change the text of these objects so that your question is in one textView, the other is blank and the buttons are labelled Yes or No (or y and n, your choice).

Change the id of the 2 buttons so that one's id is yes and the other's is no. Set the id of the blank textView as result.

Now, in the main activity, put this in BEFORE @Override:

Button yes = (Button) findViewById(R.id.yes);
Button no = (Button) findViewById(R.id.no);
TextView result = (TextView) findViewById(R.id.result);

And put this AFTER setContentView (on the next line):

yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            result.setText("Whatever your message is for yes");
}

no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            result.setText("Whatever your message is for no");
}

I hope this is what you wanted. Once again, I would advise you to watch some tutorials.

like image 40
DeathVenom Avatar answered Apr 28 '26 04:04

DeathVenom