473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why this loop not go in else part of if condition?

I need to check that data in SQLite database is present of not? but when i Checking it loop is not go in else part of if condition? I need to know why? Is this right way to check values of Sqlite to your enterd value?

Expand|Select|Wrap|Line Numbers
  1. package com.user.registration;
  2.  
  3. import java.util.List;
  4.  
  5. import com.androidhive.androidsqlite.R;
  6.  
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15.  
  16.  
  17. public class Login extends Activity {
  18.     /** Called when the activity is first created. */
  19.     @Override
  20.     public void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.loginpage);
  23.       final EditText editText = (EditText)findViewById(R.id.editText2); 
  24.        final EditText editText2 = (EditText)findViewById(R.id.editText1); 
  25.        Button button = (Button)findViewById(R.id.button1);
  26.        button.setOnClickListener(new OnClickListener() {
  27.  
  28.         @Override
  29.         public void onClick(View arg0) {
  30.             // TODO Auto-generated method stub
  31.            String str1 = editText.getText().toString();
  32.             String str2 = editText2.getText().toString();    
  33.  
  34.         DatabaseHandler db = new DatabaseHandler(Login.this);
  35.        /**
  36.          * CRUD Operations
  37.          * */
  38.         // Inserting Contacts
  39.    //  Log.d("Insert: ", "Inserting .."); 
  40.         //db.getContact(3);
  41.      System.out.print("Value of str1="+ str1);
  42.         Log.d("Reading: ", "Reading all contacts..");
  43.         List<Contact> contacts = db.getAllContacts();       
  44.         System.out.print("Value of str1="+ str1);
  45.         for (Contact cn : contacts) {
  46.             String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
  47.                 // Writing Contacts to log
  48.         if(cn.getName().equals(str1) && cn.getPhoneNumber().equals(str2))
  49.         {
  50.             Toast.makeText(Login.this,"You login successfully",Toast.LENGTH_LONG).show();
  51.  
  52.         }
  53.  
  54.             else
  55.             {
  56.                 Toast.makeText(Login.this,"Login Failed",Toast.LENGTH_LONG).show();
  57.                 System.out.print("Value of str1="+ str1);
  58.             }
  59.             Log.d("Name: ", log);
  60.             System.out.print("Value of str1="+ str1);
  61.         }
  62.  
  63.         }
  64.        });
  65.  
  66.     }
  67. }
Feb 15 '14 #1
1 1059
Nepomuk
3,112 Expert 2GB
Hi usergood and welcome to bytes.com!

DatabaseHandler is not a standard class but searching for it I found this tutorial which I'm guessing you're following, right?

Generally speaking, I can't see anything wrong with your if-else-structure. My suggestion would be to put the following lines in front of the if and then check the output:
Expand|Select|Wrap|Line Numbers
  1. System.out.println("str1 = '" + str1 + "', cn.getName() = '" + cn.getName() + "'.");
  2. System.out.println("str2 = '" + str2 + "', cn.getPhoneNumber() = '" + cn.getPhoneNumber() + "'.");
or alternatively
Expand|Select|Wrap|Line Numbers
  1. Log.d("str1 = '" + str1 + "', cn.getName() = '" + cn.getName() + "'.");
  2. Log.d("str2 = '" + str2 + "', cn.getPhoneNumber() = '" + cn.getPhoneNumber() + "'.");
Chances are that they are equal even though you don't want them to be or that this is called at the wrong time.
Feb 17 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
7
by: reon | last post by:
In the below code.. else statements didnt work correctly... the program is to add the positive and negative numbers... if we enter alphabets it will show the wrong entry ,, but thats not...
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
5
by: kavithadevan | last post by:
Hi, Here is the script which i am working in this script i am trying to search some events for that i developed this .Its working but if there is no events means i want to display there is no...
7
by: Ixiaus | last post by:
I have just come across a site that discusses Python's 'for' and 'while' loops as having an (optional) 'else' structure. At first glance I interpreted it as being a bit like the 'default'...
23
by: bearophileHUGS | last post by:
So far in Python I've almost hated the 'else' of the 'for' loops: - I have problems to remember its meaning; - It gives me little problems when I later want to translate Python code to other...
19
by: creative1 | last post by:
hi I am in strange problem. I have javascript for for validation. If I have any entry invalid it prompts me to corrrect it. All checks I have works fine. However, form does not response when there...
1
by: kingka2 | last post by:
Hi all, I have created three radio buttons in my coldfusion page and working fine. But now i want to execute query, depending on the button selected. ex: <td><input type="radio" name="Env"...
2
by: alc21321 | last post by:
I am trying to make a view bot. Just for recreational/personal use. The problem is I am trying to make it so it will only open 20 windows at a time. It won't work for some reason. I also want it to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.