-6

I wrote some code using Android Studio IDE that should ask for a couple of numbers and give us an answer for the ratio between them.

I receive the error saying: error class interface or enum expected.
I read that it occurs when I open too many curve brackets without closing them or vice-versa, but I have just 2 open brackets and then 2 closed ones so it seems balanced.

Do not take me wrong, but I read there are no android programming languages, you just use Java, right?

The code is as follow:

Import java.util.Scanner;

Class BIL{
//I am defining a class, a function;

  Public static void main(string args[]){

      Scanner userInput = new Scanner(System.in);


      Double MH, c, Md;
      //I define variable with double-precision 64-bit floating point;

      System.out.println(Enter the load, please:);
      //It prints the argument passed, into the System.out which is the standard output. In this particular case it prints it and goes to a new line;
      MH = userInput.nextDouble();
      //The call to .nextDouble() waits for a value to be input. Once input, the value will be stored in the assigned variable MH20;
      System.out.println(Enter capacity, please:);
      c = userInput.nextDouble();
      Md = MH/c;
      System.out.println(The mass needed is:);
      System.out.println(Md);
      //it prints out the result;
   }
}
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
Mario
  • 3
  • 3
  • ciao Mario, welcome! have a look around in the website because there are already interesting posts: https://stackoverflow.com/questions/3949980/what-programming-languages-can-one-use-to-develop-android-applications – Antonino Jun 12 '18 at 13:19
  • 3
    Android apps are written in the Java language (or e.g. Kotlin), but need to use the features of the Android framework. The standard `main()` method isn't one of them. So, you'll need to study the Android basics to start writing Android apps, even if you are fluent in Java. – Markus Kauppinen Jun 12 '18 at 13:20
  • `I read there are no android programming languages, you just use Java, right?` Not necessarily. You can choose between Java, Kotlin, Native (C++) and even basic (basic4android). – Phantômaxx Jun 12 '18 at 13:20
  • Some people also use C#, Javascript, Python, or Dart (Flutter)... This code won't run on Android though, anyway, as mentioned – OneCricketeer Jun 12 '18 at 13:22
  • `Class` and `Public` need to be lowercase to run as regular Java. – Andy Turner Jun 12 '18 at 13:26
  • 1
    And `import` is lower case and `String` is uppercase – GBlodgett Jun 12 '18 at 13:29
  • 1
    Oh and you need quotation marks in your printlines – GBlodgett Jun 12 '18 at 13:30
  • 2
    please pass through some basic Java and Android training lessons before you continue. – Vladyslav Matviienko Jun 12 '18 at 13:32
  • @KlingKlang `Double` is valid. I strongly doubt that `Class` can be used in that context, even on Android. – Andy Turner Jun 12 '18 at 13:32
  • @KlingKlang that is the type `java.lang.Class`, not the keyword `class`. You can't use the former there. – Andy Turner Jun 12 '18 at 13:54
  • Thanks to all of your advice! I will study a bit more tutorial, especially in Android specific tutorials. – Mario Jun 13 '18 at 13:31
  • Guys do you have some basic tutorials to link me so I can start from scratch (with a bit of programming knowledge, C++, Java little bit) like very simple ones that will take me after that to build an app as simple as this one please? – Mario Jun 13 '18 at 14:54

1 Answers1

0

I think you want an Android app able to do this. First of all you have Android Studio installed so in Android Studio click on File -> New -> New Project... and let's create the project as shown here. Your project location will be different and it's ok but if you don't want to have troubles later please leave the same package name

project_setup1

then leave the default in the next screen and finally choose the Empty Activity:

project_setup2

then Next and in the next screen leave the default settings and click Finish

Now in order to build this app we have to modify the files that I'm gonna show you

The first file is essentially the layout of your app. To be fully precise hardcoding 16dp as I did here is not the best practice but for the moment it will be ok

In the left menu go to app/res/layout and double click activity_main.xml

You can see both the graphics and a code depending on if you are respectively clicking on Design or Text tab. You need to select this last one and copy and paste this code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context="com.stackoverflow.mario.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="97dp"
        android:text="Ratio"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="@dimen/abc_action_bar_default_height_material" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="43dp"
        android:text="Number One"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/txtNumber1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/textView1"
        android:ems="2"
        android:singleLine="true"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="47dp"
        android:text="Number Two"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignRight="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="46dp"
        android:onClick="onClick"
        android:text="Divide" />

    <EditText
        android:id="@+id/txtNumber2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnAdd"
        android:layout_alignLeft="@+id/txtNumber1"
        android:ems="2"
        android:singleLine="true"
        android:inputType="numberDecimal" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/txtNumber2"
        android:layout_alignTop="@+id/btnAdd"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

The second file is essentially the engine of your app. Go to app/java/com/stackoverflow/mario and click on MainActivity[.java]. Copy and paste the following code:

package com.stackoverflow.mario;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText firstNumber;
    EditText secondNumber;
    TextView addResult;
    Button btnAdd;

    double num1,num2,ratio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        firstNumber = (EditText)findViewById(R.id.txtNumber1);
        secondNumber = (EditText)findViewById(R.id.txtNumber2);
        addResult = (TextView)findViewById(R.id.txtResult);
        btnAdd = (Button)findViewById(R.id.btnAdd);

        btnAdd.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                num1 = Double.parseDouble(firstNumber.getText().toString());
                num2 = Double.parseDouble(secondNumber.getText().toString());
                ratio = num1 / num2;
                addResult.setText(Double.toString(ratio));
            }
        });
    }

}

Now if you build [in Android Studio: Build -> Rebuild Project] and run [in Android Studio: Run -> Run App] this app you will see a screen like this:

app_screen

You can run the app creating a virtual device [the previous screenshot is from the Android Emulator and you can create one virtual device clicking the button shown in this screenshot]

android_device_choice

or if you have your phone you can enable the USB debugging, connect the USB to the PC, select your device from the window showing the devices and then clicking OK

credits for a similar sample to the author of this post

Dharman
  • 30,962
  • 25
  • 85
  • 135
Antonino
  • 3,178
  • 3
  • 24
  • 39
  • Dear Antonino CIAO!!! I copy-pasted the 2 files and I received and error like "error package R does not exist". Sorry just wanted to try your code, I was curious. I will follow the advice to study a basic android tutorial. I just studied the basic of Java (even if it might not seem so) – Mario Jun 12 '18 at 14:48
  • I extended the tutorial guiding you through the setting of the project. I hope it can help you correctly deploying the app on your mobile. I also suggested you an article at the very end of my answer. Please follow my post carefully and let me know if you still run into troubles. The app works, as I showed you with the screenshot so hopefully you will get there too! – Antonino Jun 12 '18 at 14:52
  • Antonino really thanks for your help! I am not able to run with a virtual device, it seems there is something I could change in the BIOS but I am worried to mess up my computer touching the BIOS :( I tried to connect through USB my S6 but Android Studio cannot see it :( – Mario Jun 13 '18 at 13:28
  • "Error while waiting for device: Could not start AVD". I know this is due to the BIOS. But if I connect my Samsung the software will not show it in the connected devices. I already searched and put Developer Options (thanks to the 7 times clicks :) )and I put debugging through USB but nothing. – Mario Jun 13 '18 at 13:42
  • ehi @Mario do not touch the BIOS and do me a favor: pull down the notifications curtain. You should see something [in my S7 is at the very end] where it says "USB for charging". Tap it and choose "USB for file transfer". You should then get a pop up asking you if you authorize the debug from your laptop and of course click yes. Restart Android Studio [and the system just in case] to see if now you see your phone when you ask to run the app – Antonino Jun 13 '18 at 13:57
  • In USB Configuration I have: Charging, MTP, PTP, RNDIS (USB Ethernet), MIDI. In MTB will not work. If I move to PTP, the phone automatically goes back to MTP – Mario Jun 13 '18 at 14:12
  • I also did what you said and it is already on file transfer but through MTP – Mario Jun 13 '18 at 14:24
  • if you see your phone in the filesystem of your laptop [you should because your MTP connection is active] you can also do this: in Android Studio click on Build -> Build APK then "locate" and copy that file from the PC to a folder of your mobile, e.g. Downloads. Go on your mobile, search for the download folder, double click the apk file and give all the permissions to install. You should be able to start the app – Antonino Jun 13 '18 at 14:43
  • Dear Antonino, sorry for the delay but I was busy at work :( I am receiving now the following error "Unexpected metadata type found" I do not know what is going on here.... – Mario Jun 20 '18 at 15:12
  • I was trying all this on another computer (until our discussion about the BIOS and following advice..) and now I am doing on my personal machine...and I got that error... After I solve this I will certainly follow all your advice about connecting my S6 and doing what you thought me. I am appreciating a lot your help on this Antonino. – Mario Jun 20 '18 at 15:24
  • I am stuck :( Did not get why in the other computer was working..I cannot try your advice if I cannot solve that error issue. Hope you will be soon online. Molte grazie :) – Mario Jun 25 '18 at 09:58
  • ciao Mario, the problem that you are facing is something that is not connected to the original problem. It's a problem in the configuration of your machine, nothing to do with Android. Please take the Android project, copy it on any laptop with Android Studio correctly installed, select the right options on your phone, build and install the apk as explained and give a try to check if it works. If the app installs and works please mark the answer as the right one. Thanks and have a good day! – Antonino Jun 25 '18 at 14:24
  • sorry @Antonino do not leave me alone :( So are you saying in the computer where I really need to run this app I installed Android Studio incorrectly? What could I do now please? Reinstall everything from zero? – Mario Jun 25 '18 at 14:47
  • @Mario as you said "in the other computer was working" and that's why I'm telling you that probably you have a problem on your machine. The correctness of the solution doesn't depend on the conditions of your computer. It is another issue, not the issue you asked for. We started checking this 2 weeks ago, did you work on this to get it solved during these days? it doesn't really look like a coding problem, are you able to run other Android Studio projects? good luck and have a nice day – Antonino Jun 25 '18 at 23:39
  • OK @Antonino I will create another post for the separate issue I am having. Thing is that I was using a computer that I cannot access now for 1 month and my personal computer is having this issue that means I cannot try your solutions that I believe will be super correct if I do not solve this issue first if you know what I mean. I did not want to be disrespectful :) – Mario Jun 26 '18 at 08:22
  • WOW it is finally working I made it! So what I could do now to thank you?? – Mario Jul 09 '18 at 09:23
  • ciao Mario, I'm happy you finally did it! if it works and you are satisfied you can simply mark my answer as the right one. Have a good day – Antonino Jul 09 '18 at 12:44