Reputation: 67
This is my first time here, I'll try my best to follow all guidelines.
My Issue is that the following Java code, used in Android Studio, gets the errors "Expression Expected" and "; expected" in the line "private void berechnen() {" but a ; doesn't belong anywhere there and expression expected from what I have gathered from the millions of others who have asked this exact question comes when functions aren't properly closed.
Now i checked again and again but I simply cannot seem to find any missing brackets so the error comes from something else.
The question I have is does anyone know any other reason this can have and how to fix it? Many thanks in advance.
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.databinding.DataBindingUtil;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
private double zahl1 = Double.NaN;
private double zahl2;
private static final char ADDITION = '+';
private static final char SUBTRAKTION = '-';
private static final char MULTIPLIKATION = '*';
private static final char DIVISION = '/';
private char derzeitigesinput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView1 = (TextView) findViewById(R.id.textview1);
Button button1 = (Button) findViewById(R.id.zahl1);
private void berechnen() {
if(!Double.isNaN(zahl1)) {
zahl2 = Double.parseDouble(textView1.getText().toString());
textView1.setText(null);
if(derzeitigesinput == ADDITION)
zahl1 = this.zahl1 + zahl2;
else if(derzeitigesinput == SUBTRAKTION)
zahl1 = this.zahl1 - zahl2;
else if(derzeitigesinput == MULTIPLIKATION)
zahl1 = this.zahl1 * zahl2;
else if(derzeitigesinput == DIVISION)
zahl1 = this.zahl1 / zahl2;
}
else {
try {
zahl1 = Double.parseDouble(textView1.getText().toString());
}
catch (Exception e){}
}
}
}
}
Upvotes: 0
Views: 1163
Reputation: 1
uh.. i am also new here but i would like to suggest this,I think you have added unnecessary brackets in the end .also if you count the no.of brackets then they are "ODD" in no. hence the error says it expects a statement befor"}"and then a ";" try checking it once i think it will resolve your issue
Upvotes: 0
Reputation: 2408
You can't create a function inside another function. Your berechnen
is being defined inside onCreate()
. That won't work! Every function must be defined at the class level, Java doesn't have the concept of a function private to another function.
Upvotes: 2