Home > Blockchain >  "Expected an Identifier" after else if statements in Calculator App
"Expected an Identifier" after else if statements in Calculator App

Time:01-07

I am new to coding in general, so apologies for the incorrect terminology.

I am following a YouTube video for making a calculator app on flutter.

There is a section where for function buttonPressed, if clear was pressed, it would reset the variables, else if any operand was pressed, does stuff, else if pressed decimal, print out text, etc etc. Two of the else if's reads an error "Expected an Identifier"

I looked it up, and all I got was to use a ternary operator, but I do not how to do that, or if that is the correct answer, if anyone can help me out.

Below is a URL link to the YouTube video and the code I have set up.

Video Here: https://www.youtube.com/watch?v=eVG5DkPF5x8&ab_channel=SamarthAgarwal

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Calculator'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String output = '0';
  String _output = '0';
  double num1 = 0.0;
  double num2 = 0.0;
  String operand = '';

  buttonPressed(String buttonText) {
    if (buttonText == 'CLEAR') {
      String _output = '0';
      double num1 = 0.0;
      double num2 = 0.0;
      String operand = '';
    }else if (buttonText == ' ' || buttonText == '-' || buttonText == '/' ||buttonText == '*' ||){ **EXPECTED AN IDENTIFIER.**
      num1 = double.parse(output);
      operand = buttonText;
      _output = '0';
    } else if (_output.contains('.')){
      print('Already contains decimals');
      return;
    } else {
      _output = _output   buttonText;
    } else if (buttonText == '='){ **EXPECTED AN IDENTIFIER.**
      num2 = double.parse(output);
      if(operand == ' '){
        _output = (num1   num2).toString();
      }
      if(operand == '-'){
        _output = (num1 - num2).toString();
      }
      if(operand == '*'){
        _output = (num1 * num2).toString();
      }
      if(operand == '/'){
        _output = (num1 / num2).toString();
      }

      num1 = 0.0;
      num2 = 0.0;
      operand = '';

    } else{
      _output = _output   buttonText;
    }
    print(_output);

    setState(() {
      output = double.parse(_output).toStringAsFixed(2);
    },);
  }

  Widget buildButton(String buttonText) {
    return Expanded(
      child: OutlinedButton(
        onPressed: () => buttonPressed(buttonText),
        style: OutlinedButton.styleFrom(
          padding: const EdgeInsets.all(20.0),
        ),
        child: Text(
          buttonText,
          style: const TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      // ignore: avoid_unnecessary_containers
      body: Container(
        child: Column(
          children: <Widget>[
            Container(
              alignment: Alignment.centerRight,
              padding: const EdgeInsets.symmetric(
                vertical: 24.0,
                horizontal: 12.0,
              ),
              child: Text(
                output,
                style: const TextStyle(
                  fontSize: 48.0,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            const Expanded(
              child: Divider(),
            ),
            Column(
              children: [
                Row(
                  children: [
                    buildButton('7'),
                    buildButton('8'),
                    buildButton('9'),
                    buildButton('/'),
                  ],
                ),
                Row(
                  children: [
                    buildButton('4'),
                    buildButton('5'),
                    buildButton('6'),
                    buildButton('*'),
                  ],
                ),
                Row(
                  children: [
                    buildButton('1'),
                    buildButton('2'),
                    buildButton('3'),
                    buildButton('-'),
                  ],
                ),
                Row(
                  children: [
                    buildButton('.'),
                    buildButton('0'),
                    buildButton('00'),
                    buildButton(' '),
                  ],
                ),
                Row(
                  children: [
                    buildButton('CLEAR'),
                    buildButton('='),
                  ],
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

I have tried using ternary operator, but I do not have the knowledge to use it properly.

CodePudding user response:

There is a syntactical error on this line:

    }else if (buttonText == ' ' || buttonText == '-' || buttonText == '/' ||buttonText == '*' ||){ **EXPECTED AN IDENTIFIER.**

The logical OR operator (||) requires both a left and a right operand. Thus, having a || without an expression on the right-hand side is not syntactically valid. Remove the last || on that line, by changing it to be:

    }else if (buttonText == ' ' || buttonText == '-' || buttonText == '/' ||buttonText == '*') {

Additionally, if / else if / else statements must have at most one else clause, and if present, it must be the last clause.

Your code has duplicate else clauses:

else {
      _output = _output   buttonText;
    }

Remove the first one, leaving the one at the end, and the error will be resolved.

CodePudding user response:

Thanks to @Chuck Batson ,I changed my else clause to be the last one, resolving my error.

  • Related