string concatenation in android studio java - string-concatenation

I am trying to display invalid user credentials in textview by concatenating the invalid fields.So that i get a textview like invalid name,username,contact...at a time.
I want the invalid fields to display at a time.
I have declared a string and tried to concatenate but the result is textview displays all the fields even if its valid one.
boolean flag = true;
String s1,s2,s3,s4,s5;
boolean checkDataEntered() {
if (isEmpty(Name)) {
Name.setBackgroundResource(R.drawable.error);
s1="Full name";
flag = false;
}
if (isEmpty(Username)) {
Username.setBackgroundResource(R.drawable.error);
s2="Username";
flag = false;
}
if (isEmail(EmailId)) {
EmailId.setBackgroundResource(R.drawable.error);
s3="Email";
flag = false;
}
if (flag) {
return true;
} else {
textView7.setText("Please fill"+s1+ s2+ s3) ;
return false;
}
}

This is because you are concatenating all the fields in your else statement.
textView7.setText("Please fill"+s1+ s2+ s3);
But your requirement is to concatenate it only if it is empty. So you should try like,
boolean flag = true;
String error = "";
boolean checkDataEntered() {
if (isEmpty(Name)) {
Name.setBackgroundResource(R.drawable.error);
error += "Full name";
flag = false;
}
if (isEmpty(Username)) {
Username.setBackgroundResource(R.drawable.error);
error += ",Username";
flag = false;
}
if (isEmail(EmailId)) {
EmailId.setBackgroundResource(R.drawable.error);
error += ",Email";
flag = false;
}
if (flag) {
return true;
} else {
textView7.setText("Please fill "+error) ;
return false;
}
}

Related

Flutter error: The body might complete normally

I'm trying to run this function in Flutter and I'm getting the error: The body might complete normally, causing 'null' to be returned, but the return type, 'String', is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.
But if I add return ''; in the end it does not return any value, any solution?
String getWord() {
wordCounter += 1;
var rand = Random();
int wordLength = _words.length;
int randNumber = rand.nextInt(wordLength);
bool notUnique = true;
if (wordCounter - 1 == _words.length) {
notUnique = false;
return '';
}
while (notUnique) {
if (!_usedNumbers.contains(randNumber)) {
notUnique = false;
_usedNumbers.add(randNumber);
return _words[randNumber];
} else {
randNumber = rand.nextInt(wordLength);
}
}
}
The body might complete normally, causing 'null' to be returned, but the return type, 'String', is a potentially non-nullable type. Try adding either a return or a throw statement at the end. can be resolved like the below code.
Try to return only once at the end
String getWord() {
String returnString = "";
wordCounter += 1;
var rand = Random();
int wordLength = _words.length;
int randNumber = rand.nextInt(wordLength);
bool notUnique = true;
if (wordCounter - 1 == _words.length) {
notUnique = false;
}
while (notUnique) {
if (!_usedNumbers.contains(randNumber)) {
notUnique = false;
_usedNumbers.add(randNumber);
returnString = _words[randNumber];
} else {
randNumber = rand.nextInt(wordLength);
}
}
return returnString;
}

my code not complete for where contact number

How to fix code my code flutter and use plugin
filterContacts() {
setState(() {
List<Contact> _contacts = [];
_contacts.addAll(contacts);
if (searchController.text.isNotEmpty) {
_contacts.retainWhere(
(contact) {
String searchTerm = searchController.text.toLowerCase().trim();
String searchTermFlatten = flattenPhoneNumber(searchTerm);
String contactName = contact.displayName.toString().toLowerCase();
bool nameMatches = contactName.contains(searchTerm);
if (nameMatches == true) {
return true;
}
if (searchTermFlatten.isEmpty) {
return false;
}
var phone = contact.phones.firstWhere((phn) {
String phnFlattened = flattenPhoneNumber(phn);
return phnFlattened.contains(searchTermFlatten);
}, orElse: () => null);
return phone != null;
},
);
contactsFiltered = _contacts;
}
});
}
Flutter code How to fix code my code flutter and use plugin contacts_service,
this image is about a problem
contact.phones can be null, in this you need to check its value 1st then proceed,
you can use contact.phones?.firstWhere to handle this situation or
If you're sure it will have value, you can also do contact.phones!.firstWhere but I don't recommend this. You don't need to use orElse you want to pass null,
Item? phone = contact.phones?.firstWhere((phn) {
String phnFlattened = flattenPhoneNumber(phn);
return phnFlattened.contains(searchTermFlatten);
}, );
You can learn more about ?. !...
[how to fix now]
error code not complete
filterContacts() {
setState(() {
List<Contact> _contacts = [];
_contacts.addAll(contacts);
if (searchController.text.isNotEmpty) {
_contacts.retainWhere(
(contact) {
String searchTerm = searchController.text.toLowerCase().trim();
String searchTermFlatten = flattenPhoneNumber(searchTerm);
String contactName = contact.displayName.toString().toLowerCase();
bool nameMatches = contactName.contains(searchTerm);
if (nameMatches == true) {
return true;
}
if (searchTermFlatten.isEmpty) {
return false;
}
Item? phone = contact.phones?.firstWhere((phn) {
String phnFlattened = flattenPhoneNumber(phn);
return phnFlattened.contains(searchTermFlatten);
}, );
return phone != null;
},
);
contactsFiltered = _contacts;
}
});
}

Need to edit the code "user info" for sdk ZKTeco device fb100

I have this code working 100% but with one device zkt fb100,This code is for set user name ,card number , with id to the device to register it:
public int sta_SetUserInfo(ListBox lblOutputInfo, TextBox txtUserID, TextBox txtName, ComboBox cbPrivilege, TextBox txtCardnumber, TextBox txtPassword)
{
if (GetConnectState() == false)
{
lblOutputInfo.Items.Add("*Please connect first!");
return -1024;
}
if (txtUserID.Text.Trim() == "" || cbPrivilege.Text.Trim() == "")
{
lblOutputInfo.Items.Add("*Please input data first!");
return -1023;
}
int iPrivilege = cbPrivilege.SelectedIndex;
bool bFlag = false;
if (iPrivilege == 5)
{
lblOutputInfo.Items.Add("*User Defined Role is Error! Please Register again!");
return -1023;
}
/*
if(iPrivilege == 4)
{
axCZKEM1.IsUserDefRoleEnable(iMachineNumber, 4, out bFlag);
if (bFlag == false)
{
lblOutputInfo.Items.Add("*User Defined Role is unable!");
return -1023;
}
}
*/
//lblOutputInfo.Items.Add("[func IsUserDefRoleEnable]Temporarily unsupported");
int iPIN2Width = 0;
int iIsABCPinEnable = 0;
int iT9FunOn = 0;
string strTemp = "";
axCZKEM1.GetSysOption(GetMachineNumber(), "~PIN2Width", out strTemp);
iPIN2Width = Convert.ToInt32(strTemp);
axCZKEM1.GetSysOption(GetMachineNumber(), "~IsABCPinEnable", out strTemp);
iIsABCPinEnable = Convert.ToInt32(strTemp);
axCZKEM1.GetSysOption(GetMachineNumber(), "~T9FunOn", out strTemp);
iT9FunOn = Convert.ToInt32(strTemp);
/*
axCZKEM1.GetDeviceInfo(iMachineNumber, 76, ref iPIN2Width);
axCZKEM1.GetDeviceInfo(iMachineNumber, 77, ref iIsABCPinEnable);
axCZKEM1.GetDeviceInfo(iMachineNumber, 78, ref iT9FunOn);
*/
if (txtUserID.Text.Length > iPIN2Width)
{
lblOutputInfo.Items.Add("*User ID error! The max length is " + iPIN2Width.ToString());
return -1022;
}
if (iIsABCPinEnable == 0 || iT9FunOn == 0)
{
if (txtUserID.Text.Substring(0,1) == "0")
{
lblOutputInfo.Items.Add("*User ID error! The first letter can not be as 0");
return -1022;
}
foreach (char tempchar in txtUserID.Text.ToCharArray())
{
if (!(char.IsDigit(tempchar)))
{
lblOutputInfo.Items.Add("*User ID error! User ID only support digital");
return -1022;
}
}
}
int idwErrorCode = 0;
string sdwEnrollNumber = txtUserID.Text.Trim();
string sName = txtName.Text.Trim();
string sCardnumber = txtCardnumber.Text.Trim();
string sPassword = txtPassword.Text.Trim();
bool bEnabled = true;
/*if (iPrivilege == 4)
{
bEnabled = false;
iPrivilege = 0;
}
else
{
bEnabled = true;
}*/
axCZKEM1.EnableDevice(iMachineNumber, false);
axCZKEM1.SetStrCardNumber(sCardnumber);//Before you using function SetUserInfo,set the card number to make sure you can upload it to the device
if (axCZKEM1.SSR_SetUserInfo(iMachineNumber, sdwEnrollNumber, sName, sPassword, iPrivilege, bEnabled))//upload the user's information(card number included)
{
lblOutputInfo.Items.Add("Set user information successfully");
}
else
{
axCZKEM1.GetLastError(ref idwErrorCode);
lblOutputInfo.Items.Add("*Operation failed,ErrorCode=" + idwErrorCode.ToString());
}
axCZKEM1.RefreshData(iMachineNumber);//the data in the device should be refreshed
axCZKEM1.EnableDevice(iMachineNumber, true);
return 1;
}
But if I have two device what should I change here ? .
I tried to change iMachineNumber to make first device is 1 and second device is 2 but not working too !
Finally I found the solution : in the button connect to device need to insert the above code in loop for one device and then next device and so on .

Can somebody shorten this code?

I just finished creating a program (I am a beginner at this programming stuff) Now I might be doing this the total wrong way or my logic might not be the greatest at programming but any help would be amazing I will post my code so far below
This code is used when a button is clicked, the button will send a text then the textbox will get the text.
if (txt1.Text == "")
{
txt1.Text = "J";
btn1.Visible = false;
}
else if (txt1.Text != "")
{
if (txt2.Text == "")
{
txt2.Text = "J";
btn1.Visible = false;
}
else if (txt2.Text != "")
{
if (txt3.Text == "")
{
txt3.Text = "J";
btn1.Visible = false;
}
else if (txt3.Text != "")
{
if (txt4.Text == "")
{
txt4.Text = "J";
btn1.Visible = false;
}
else if (txt4.Text != "")
{
if (txt5.Text == "")
{
txt5.Text = "J";
btn1.Visible = false;
}
else if (txt5.Text != "")
{
if (txt6.Text == "")
{
txt6.Text = "J";
btn1.Visible = false;
}
else if (txt6.Text != "")
{
if (txt7.Text == "")
{
txt7.Text = "J";
btn1.Visible = false;
}
else if (txt7.Text != "")
{
if (txt8.Text == "")
{
txt8.Text = "J";
btn1.Visible = false;
}
else if (txt8.Text != "")
{
}
}
}
}
}
}
}
}
You need to get all of these text cases into an array for the following loop to work (I have called the array 'txt' here). Based on what you have written this loop should do the same thing as your code but I'm not sure if that's what you really want to do. Your code is setting a single text box to "J" and then hiding your button only if every preceding text field is not an empty string (This will include any of the fields set to null, for example). The conditional then exits.
`for (int i = 0; i < txt.Length; i++) {
if(txt[i] != "") {
continue;
}
else if(txt[i] == "") {
txt[i] = "J";
btn1.Visible = false;
break;
}
}
Note: I don't know whether this works for C# 3 or not (it should). Try it.
First, you should put all of your text fields into an array:
TextField[] textFields = { txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8, };
Then, loop through the text fields to find a text field that has no text in it:
foreach (TextField tf in textFields) {
if (tf.Text == "") {
}
}
After we find it, we want to set its text to "J" and make btn1 invisible. Since we already found the text field, we don't need to continue the loop anymore, so we break:
tf.Text = "J";
btn1.Visible = false;
break;
If this doesn't work in C# 3, just update to C# 5 or 6 alright?

Selecting a value from a database an check if it is true or false

This code always returns true can some please help me out
public boolean plselct(String sqd)
{
try{
String player ;
pm = conn.prepareStatement("SELECT playertype FROM playerdetails where idplayeratrr = ?");
pm.setString(1,sqd );
h= pm.executeQuery();
player = h.getString("playertype");
if ("Goal keeper".equals(player))
{ return true; }
else
return false;
}catch (Exception e)
{}
return false;
}
SelectQu p = new SelectQu();
p.plselct("77");
if(true)
{ System.out.println("Yes"); }
else
{ System.out.println("No"); }
You are checking for if(true)? Did you mean this instead:
SelectQu p = new SelectQu();
if(p.plselct("77"))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}