Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

New coder, doing third project, can not get app to run

I am a new coder, creating a higher or lower guessing game app in Android Studio using the Java language. My goal for this project is for the user to be able to input a number between 1 and 100, and for the app to be able to pick a random number each time the user plays the game, and for the app to be able to tell the user if the number they picked is the correct answer, or higher or lower. I am using Android studio using Java. I have all the code there, I just keep getting a couple error answers. For instance, on my "if (guess < theNumber) statements, the word "guess is in red" and says it cannot resolve symbol guess. And the statement "private Button btnGuess" the btnGuess is in grey and it says that it’s never used, but I have the id in the design tab of Android Studio and it’s id is labeled btnGuess and I thought that I wired it into the code, when I try to run the app through the emulator it just does not even run. I’ve tried googling around for the answer, but since I am new to coding I’m not sure that I’m asking the correct questions, I’ve included the code for reference, thank you very much in advance.

ckage com.example.billyhodgesguessinggame;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.billyhodgesguessinggame.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   private EditText txtGuess;
   private Button btnGuess;
   private TextView lblOutput;
   private int theNumber;
   public void checkGuess() {
       String guessText = txtGuess.getText().toString();
       String message = "";
       try {
           int Guess = Integer.parseInt(guessText);
           if (guess < theNumber)
               message = guess + "is too low. Try again.";
           else if (guess > theNumber)
               message = guess + "is too high. Try again.";
           else {
               message = guess +
                       "is correct. You win! Let's play again!";
               newGame();
           }
       } catch (Exception e) {
           message = "Enter a whole number between 1 and 100.";
       } finally {
           lblOutput.setText(message);
           txtGuess.requestFocus();
           txtGuess.selectAll();
       }

   }


   private AppBarConfiguration appBarConfiguration;

   public void newGame() {
       theNumber = (int)(Math.random() * 100 + 1);
   }
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       txtGuess = findViewById(R.id.txtGuess);
       Button btnGuess = findViewById(R.id.btnGuess);
       lblOutput = findViewById (R.id.lblOutput);
       newGame();
       btnGuess.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick (View v) {
               checkGuess();
           }
       });


       com.example.billyhodgesguessinggame.databinding.ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
       setContentView(binding.getRoot());

       setSupportActionBar(binding.toolbar);

       NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
       appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
       NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

       binding.fab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action", null).show();
           }
       });
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.menu_main, menu);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();

       //noinspection SimplifiableIfStatement
       if (id == R.id.action_settings) {
           return true;
       }

       return super.onOptionsItemSelected(item);
   }

   @Override
   public boolean onSupportNavigateUp() {
       NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
       return NavigationUI.navigateUp(navController, appBarConfiguration)
               || super.onSupportNavigateUp();
   }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. You use Guess and guess as variable names. Stick to the lowercase version and only use guess.

  2. In this line, remove Button at the beginning. Otherwise, you declare a new variable instead of reusing the existing one which you already declared at the top.

 Button btnGuess = findViewById(R.id.btnGuess);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading