Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3664002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:35:42+00:00 2026-05-19T01:35:42+00:00

I have a boolean method returning true or false to check whether or not

  • 0

I have a boolean method returning true or false to check whether or not data exists inside of strings. Everything works ok if the user enters all data or does not run through the dialogs…..BUT….if the user DOES NOT enter data in the “getItemsEditText” dialog popup AND still clicks “OK”, this boolean is resolving to true, even though “pricePerItemText” still has nothing stored. This is the boolean method:

public Boolean doesAllDataExistCheckBool ()
{

  if (pricePerItemText != "" && itemsPerDayText != "" && sleepTimeText != "" && 
     wakeTimeText != "")
    {

        SharedPreferences.Editor editor = mySharedPreferences.edit
       (); //opens shared preference editor
        editor.putBoolean("storedDoesAllDataExist", true);
        editor.commit();  //commit changes to mySharedPreferences
        //End storing shared preferences
        return true;
    }
    else
    {
        SharedPreferences.Editor editor = mySharedPreferences.edit
        (); //opens shared preference editor
        editor.putBoolean("storedDoesAllDataExist", false);
        editor.commit();  //commit changes to mySharedPreferences
        //End storing shared preferences

        return false;
    }
}

Here is where the boolean is being tested to see if true or false:

if (position == 4)
  {
    allDataExists = doesAllDataExistCheckBool ();  //checks if true or false

    if (serviceStarted == true)
     {
       Context context = getApplicationContext();
       String text = "Schedule is already running";
       int duration = Toast.LENGTH_SHORT;
       Toast toast = Toast.makeText(context, text, duration);
       toast.show();
     }
   if (serviceStarted == false && doesAllDataExistCheckBool () == true)
     {
     startScheduleService();
     }
   if (serviceStarted == false && doesAllDataExistCheckBool () == false)
     {
       Context context = getApplicationContext();
       String text = "Please enter all data before starting!";
       int duration = Toast.LENGTH_SHORT;
       Toast toast = Toast.makeText(context, text, duration);
       toast.show();
     }

}

Here is how the dialog with EditText and OK/Cancel buttons is written:

case ITEMS_PER_DAY :

LayoutInflater li = LayoutInflater.from(this);

final View itemsEntryView = li.inflate(R.layout.settings_dialog_input, (ViewGroup)
findViewById(R.id.layout_root));

final EditText getItemsEditText = (EditText)itemsEntryView.findViewById
(R.id.DialogEditText);


return new AlertDialog.Builder(SettingsActivity.this)

 .setTitle("This is the title")

 .setView(itemsEntryView)

 .setPositiveButton("Ok", new DialogInterface.OnClickListener()
 {
  public void onClick(DialogInterface dialog, int whichButton)
  {

    itemsPerDayText = getItemsEditText.getText().toString();  //gets input from 
    edittext and saves it to a string itemsPerDayText


    //Initialize shared preferences
    SharedPreferences.Editor editor = mySharedPreferences.edit(); //opens editor
   editor.putString("storedItemsPerDayText", itemsPerDayText); 
   editor.commit();  //commit changes to mySharedPreferences
   //End storing shared preferences

   }
 })
 .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
  public void onClick(DialogInterface dialog, int whichButton)
   {
    //user click cancel
   }
}).create();

Is there another way to do this? Why can the user still click “OK” if they did not enter anything at all? Any ideas? Thanks guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:35:42+00:00Added an answer on May 19, 2026 at 1:35 am

    You posted way too much code. But right away I noticed this

    pricePerItemText != ""
    

    Assuming pricePerItemText is a string, which we really have no idea since you didn’t include that, that’s not how you compare strings in java. It needs to be

    !pricePerItemText.equals("");
    

    Edit:

    In java, the == operator compares objects references, not values. So

    String mytext = "text";
    if (mytext == "text"){ print "True"}
    

    will never print true because the mytext variable is pointing to some memory location, which is most definitely not the same as where “text” points to.

    The fact that

    "text == "text"
    

    is true is a an artifact of Java keeping a string pool so it doesn’t have to reallocate new strings. This is a major cause of confusion.

    Here’s a random link which describes it probably better

    http://leepoint.net/notes-java/data/expressions/22compareobjects.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object with a method public boolean hasPermission(String role) { return permissions.contains(role);
I have a Method name update, this will be called depending on the boolean
I have this method, that can return three different response. At first, it was
I have made a custom input method for formtastic, it is designed to work
I'm writing a class in Java to represent a Graph data structure. This is
I have an unlockable mode in my game that is only available once the
I have a few basic questions about the Dispose Pattern in C#. In the
All, While this is similar to another post , that post (does not indicate
I've submitted my first iPhone App to the AppStore (on 10.09.10) and I have
I am using php/ajax to submit a form without page refresh. Here are my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.