This is my code for searching a 2 places that call the method of the polyline. When my editext Tabaco - Malinao it's calling the method properly same as Tabaco-Bacacay, Tabaco-Santo Domingo, Tabaco-Malilipot. But when my edittext is Malilipot-Santo Domingo, Santo Domingo-Bacacay it's always calling for the first condition (Tabaco-Malinao) not their own condition.
Could be the condition is wrong? or the logical operators that I'm using?
My understanding is that, it takes what comes first and not taking the equal sign correctly. This could be wrong.
String origin = etOrigin.getText().toString();
String destination = etDestination.getText().toString();
if (origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco")) {
ttom();
Toast.makeText(getApplicationContext(), "tabaco malinao", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Tabaco") == destination.equals("Bacacay") && origin.equals("Bacacay") == destination.equals("Tabaco")) {
ttob();
Toast.makeText(getApplicationContext(), "tabaco bacacay", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Tabaco") == destination.equals("Santo Domingo") && origin.equals("Santo Domingo") == destination.equals("Tabaco")) {
ttosto();
Toast.makeText(getApplicationContext(), "tabaco sto domingo", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Tabaco") == destination.equals("Malilipot") && origin.equals("Malilipot") == destination.equals("Tabaco")) {
ttomali();
Toast.makeText(getApplicationContext(), "tabaco malilipot", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Malilipot") == destination.equals("Santo Domingo") && origin.equals("Santo Domingo") == destination.equals("Malilipot")) {
malitosto();
Toast.makeText(getApplicationContext(), "malilipot sto domingo", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Malilipot") == destination.equals("Bacacay") && origin.equals("Bacacay") == destination.equals("Malilipot")) {
malitobac();
Toast.makeText(getApplicationContext(), "malilipot bacacay", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Santo Domingo") == destination.equals("Bacacay") && origin.equals("Bacacay") == destination.equals("Santo Domingo")) {
bactosto();
Toast.makeText(getApplicationContext(), "sto domingo bacacay", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Invalid input!", Toast.LENGTH_SHORT).show();
}
The logic is wrong, let's say that you have :
String origin = "Malilipot";
String destination = "Santo Domingo";
Then origin.equals("Tabaco") is false
and destination.equals("Malinao") is also false .
So origin.equals("Tabaco") == destination.equals("Malinao") yields false == false which is true, despite the fact that it absolutely doesn't match what you thought was your condition.
Refactor your code to only use && (AND) and || (OR) operators :
if (origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco"))
Becomes
if ((origin.equals("Tabaco") && destination.equals("Malinao")) || (origin.equals("Malinao") && destination.equals("Tabaco")))
Change the condition like below ,in your case when you enter Malilipot-Santo Domingo as per first condition
if (origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco"))
origin.equals("Tabaco") is false and destination.equals("Malinao") is also false then false == false it become true same for next condition
String origin = etOrigin.getText().toString();
String destination = etDestination.getText().toString();
if ((origin.equals("Tabaco") && destination.equals("Malinao")) ||( origin.equals("Malinao") == destination.equals("Tabaco"))) {
ttom();
Toast.makeText(getApplicationContext(), "tabaco malinao", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Tabaco") && destination.equals("Bacacay")) || ( origin.equals("Bacacay") && destination.equals("Tabaco"))) {
ttob();
Toast.makeText(getApplicationContext(), "tabaco bacacay", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Tabaco") && destination.equals("Santo Domingo")) ||( origin.equals("Santo Domingo") && destination.equals("Tabaco")) ){
ttosto();
Toast.makeText(getApplicationContext(), "tabaco sto domingo", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Tabaco") && destination.equals("Malilipot") )||( origin.equals("Malilipot") && destination.equals("Tabaco"))) {
ttomali();
Toast.makeText(getApplicationContext(), "tabaco malilipot", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Malilipot") && destination.equals("Santo Domingo") )||( origin.equals("Santo Domingo") && destination.equals("Malilipot"))){
malitosto();
Toast.makeText(getApplicationContext(), "malilipot sto domingo", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Malilipot") && destination.equals("Bacacay")) ||( origin.equals("Bacacay") && destination.equals("Malilipot"))) {
malitobac();
Toast.makeText(getApplicationContext(), "malilipot bacacay", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Santo Domingo") && destination.equals("Bacacay") )||( origin.equals("Bacacay") && destination.equals("Santo Domingo"))) {
bactosto();
Toast.makeText(getApplicationContext(), "sto domingo bacacay", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Invalid input!", Toast.LENGTH_SHORT).show();
}
here is the explanation why it's going in the first condition...
String origin = "Malilipot";
String destination = "Santo Domingo";
if(origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco"))
origin.equals("Tabaco") = false
destination.equals("Malinao") = false
origin.equals("Tabaco") == destination.equals("Malinao")
false == false
result will be = true
origin.equals("Malinao") = false
destination.equals("Tabaco") = false
origin.equals("Malinao") == destination.equals("Tabaco")
false == false
result will be = true
at your last condition will be if (true == true)
it will be : true
thats why its going in first condition
Related
I have a program that quotes up to 57 customizable products at one time. If there is a compatibility issue within one of the products the customer designs, an error pops up and a quote is not generated. I have it so if the generate button on line 57 is clicked, the program will check if lines 57, 56, 55... down to 1 are correct, then it will generate if they are. If line 56 is clicked, it checks 56 down to 1.
Is there a ways to loop is so that I only have to write it out once?
Here is my code:
if(e.getSource() == CA1.BTNgeneratequoteCA1)
{
if(CA1.generateCA1 == true)
{
try
{
FillAllKIS();
E2fill();
FillQuote();
}
catch(IOException y)
{
Logger.severe(y.getMessage());
}
RecordAllLines();
}
}
// if statments for lines 2 to 56 here
if(e.getSource() == CA57.BTNgeneratequoteCA1)
{
if(CA1.generateCA1 == true && CA2.generateCA1 == true && CA3.generateCA1 == true && CA4.generateCA1 == true && CA5.generateCA1 == true &&
CA6.generateCA1 == true && CA7.generateCA1 == true && CA8.generateCA1 == true && CA9.generateCA1 == true && CA10.generateCA1 == true &&
CA11.generateCA1 == true && CA12.generateCA1 == true && CA13.generateCA1 == true && CA14.generateCA1 == true && CA15.generateCA1 == true &&
CA16.generateCA1 == true && CA17.generateCA1 == true && CA18.generateCA1 == true && CA19.generateCA1 == true && CA20.generateCA1 == true &&
CA21.generateCA1 == true && CA22.generateCA1 == true && CA23.generateCA1 == true && CA24.generateCA1 == true && CA25.generateCA1 == true &&
CA26.generateCA1 == true && CA27.generateCA1 == true && CA28.generateCA1 == true && CA29.generateCA1 == true && CA30.generateCA1 == true &&
CA31.generateCA1 == true && CA32.generateCA1 == true && CA33.generateCA1 == true && CA34.generateCA1 == true && CA35.generateCA1 == true &&
CA36.generateCA1 == true && CA37.generateCA1 == true && CA38.generateCA1 == true && CA39.generateCA1 == true && CA40.generateCA1 == true &&
CA41.generateCA1 == true && CA42.generateCA1 == true && CA43.generateCA1 == true && CA44.generateCA1 == true && CA45.generateCA1 == true &&
CA46.generateCA1 == true && CA47.generateCA1 == true && CA48.generateCA1 == true && CA49.generateCA1 == true && CA50.generateCA1 == true &&
CA51.generateCA1 == true && CA52.generateCA1 == true && CA53.generateCA1 == true && CA54.generateCA1 == true && CA55.generateCA1 == true &&
CA56.generateCA1 == true && CA57.generateCA1 == true)
{
try
{
FillAllKIS();
E2fill();
FillQuote();
}
catch(IOException y)
{
Logger.severe(y.getMessage());
}
RecordAllLines();
}
I thought about doing it with a list filled with the instances of the classes but I'm struggling to think of how the logic would work for checking the boolean of each line, since there is another one to check with each added line item.
/* Create a list to hold CA1..CA57 */
List<MyClass> allCas = new ArrayList<>();
/* As you create CA1..CA57, whether in a loop or hardcoded, add them to your list, in order*/
...
allCas.add(CA1);
...
allCas.add(CA31);
...
allCas.add(CA57);
/* When button is clicked, find the related CA instance and check the preceding objects too. */
int selected = IntStream.range(0, allCas.size())
.filter(idx -> e.getSource() == allCas.get(idx))
.findFirst().getAsInt() + 1;
if (allCas.stream().limit(selected).allMatch(ca -> ca.generateCA1)) {
/* Fill quote and stuff. */
...
}
I'm making a checkwinner class that checks 4 arrays for a bingo win, and it is wayyyy too long. Any way i can shorten it? I know you can use a for loop, but I have no idea how.thank you in advance!
public static boolean checkWinner(String[][] card, String[][] card2, String[][] card3, String[][]card4) {
if ((card[0][0] == card[0][1] && card[0][1] == card[0][2] && card[0][2] == card[0][3] && card[0][3] == card[0][4]) || (card[1][0] == card[1][1] && card[1][1] == card[1][2] && card[1][2] == card[1][3] && card[1][3] == card[1][4])|| (card[2][0] == card[2][1] && card[2][1] == card[2][3] && card[2][3] == card[2][4]) || (card[3][0] == card[3][1] && card[3][1] == card[3][2] && card[3][2] == card[3][3] && card[3][3] == card[3][4])
|| (card[4][0] == card[4][1] && card[4][1] == card[4][2] && card[4][2] == card[4][3] && card[4][3] == card[4][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card[0][0] == card[1][0] && card[1][0] == card[2][0] && card[2][0] == card[3][0] && card[3][0] == card[4][0]) || (card[0][1] == card[1][1] && card[1][1] == card[2][1] && card[2][1] == card[3][1] && card[3][1] == card[4][1])
|| (card[0][2] == card[1][2] && card[1][2] == card[3][2] && card[3][2] == card[4][2]) || (card[0][3] == card[1][3] && card[1][3] == card[2][3] && card[2][3] == card[3][3] && card[3][3] == card[4][3])
|| (card[0][4] == card[1][4] && card[1][4] == card[2][4] && card[2][4] == card[3][4] && card[3][4] == card[4][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card[0][0] == card[1][1] && card[1][1] == card[3][3] && card[3][3]== card[4][4]) || (card[4][0] == card[3][1] && card[3][1] == card[1][3] && card[1][3]== card[0][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card2[0][0] == card2[0][1] && card2[0][1] == card2[0][2] && card2[0][2] == card2[0][3] && card2[0][3] == card2[0][4]) || (card2[1][0] == card2[1][1] && card2[1][1] == card2[1][2] && card2[1][2] == card2[1][3] && card2[1][3] == card2[1][4])
|| (card2[2][0] == card2[2][1] && card2[2][1] == card2[2][3] && card2[2][3] == card2[2][4]) || (card2[3][0] == card2[3][1] && card2[3][1] == card2[3][2] && card2[3][2] == card2[3][3] && card2[3][3] == card2[3][4])
|| (card2[4][0] == card2[4][1] && card2[4][1] == card2[4][2] && card2[4][2] == card2[4][3] && card2[4][3] == card2[4][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card2[0][0] == card2[1][0] && card2[1][0] == card2[2][0] && card2[2][0] == card2[3][0] && card2[3][0] == card2[4][0]) || (card2[0][1] == card2[1][1] && card2[1][1] == card2[2][1] && card2[2][1] == card2[3][1] && card2[3][1] == card2[4][1])
|| (card2[0][2] == card2[1][2] && card2[1][2] == card2[3][2] && card2[3][2] == card2[4][2]) || (card2[0][3] == card2[1][3] && card2[1][3] == card2[2][3] && card2[2][3] == card2[3][3] && card2[3][3] == card2[4][3])
|| (card2[0][4] == card2[1][4] && card2[1][4] == card2[2][4] && card2[2][4] == card2[3][4] && card2[3][4] == card2[4][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card2[0][0] == card2[1][1] && card2[1][1] == card2[3][3] && card2[3][3]== card2[4][4]) || (card2[4][0] == card2[3][1] && card2[3][1] == card2[1][3] && card2[1][3]== card2[0][4])) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
} else if ((card3[0][0] == card3[0][1] && card3[0][1] == card3[0][2] && card3[0][2] == card3[0][3] && card3[0][3] == card3[0][4]) || (card3[1][0] == card3[1][1] && card3[1][1] == card3[1][2] && card3[1][2] == card3[1][3] && card3[1][3] == card3[1][4])|| (card3[2][0] == card3[2][1] && card3[2][1] == card3[2][3] && card3[2][3] == card3[2][4])
|| (card3[3][0] == card3[3][1] && card3[3][1] == card3[3][2] && card3[3][2] == card3[3][3] && card3[3][3] == card3[3][4])
|| (card3[4][0] == card3[4][1] && card3[4][1] == card3[4][2] && card3[4][2] == card3[4][3] && card3[4][3] == card3[4][4])) {
System.out.println("CPU CALLS BINGO! YOU LOSE!");
displayBoard(card, card2, card3, card4);
System.exit(0);
return false;
}
}
return true;
}
I think what you have written in the checkWinner method is quite short, because using loops will give make your code more tedious to work with and it will be a lot longer. But, I have written down the CheckWinner method using loops. I haven't ran this code in IDE. So, there maybe some bugs. I hope is helped you.
NOTE:below code is only implementing for the card method only
public static boolean checkWinner(String[][] card, String[][] card2, String[][] card3, String[][]card4) {
boolean flag = true;
//repeat this nested-loop for card2 and card3 similarly, which I haven't showed in my code.
//The below nested loop are for the condition for the 'if' condition in your code.
for(int j=0; j<5; j++) {
boolean flag1 = true;
for (int i=0; i<4; i++) {
if (card[i][j] == card[i][j+1]) {
}
else {
flag1 = false;
break;
}
}
if (flag1) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
flag = false;
break;
}
}
if (flag) {
int i=0, j=0;
boolean flag2 = true;
while (i<5 && j<5) {
if (i!=2 && j!=2) {
if (card[i][j]==card[i+1][j+1]) {
` }
else flag2 = false;
break;
}
i++;
j++;
}
if (flag2) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
flag = false;
}
else {
i=4;
j=0;
flag2 = true;
while (i<5 && j<5) {
if (i!=2 && j!=2) {
if (card[i][j]==card[i+1][j+1]) {
` }
else {
flag2 = false;
break;
}
}
i--;
j++;
}
if (flag2) {
System.out.println("BINGO! Congratulations, you have won!");
displayBoard(card, card2, card3, card4);
System.exit(0);
flag = false;
}
}
}
return(flag);
}
I never used nested switch statement. Just wanted to know using nested switch statement is good in my code or not.
I got 3 integer variables. type and position values 0 or 1 on both and item with 0 to 6.
My code is too lengthy of this, anyway to shorten this:
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
type = spinner_type.getSelectedItemPosition();
position = spinner_pos.getSelectedItemPosition();
item = spinner.getSelectedItemPosition();
switch (adapterView.getId()) {
case R.id.cck_spinner_type:
switch (pos) {
case 0:
if(position == 0 && item == 0) {
sp_name = "cc_left";
} else if(position == 0 && item == 1){
sp_name = "cc_left1";
} else if(position == 0 && item == 2){
sp_name = "cc_left2";
}else if(position == 0 && item == 3){
sp_name = "cc_left3";
}else if(position == 0 && item == 4){
sp_name = "cc_left4";
}else if(position == 0 && item == 5){
sp_name = "cc_left5";
}else if(position == 0 && item == 6){
sp_name = "cc_left6";
}else if(position == 1 && item == 0) {
sp_name = "cc_right";
} else if(position == 1 && item == 1){
sp_name = "cc_right1";
} else if(position == 1 && item == 2){
sp_name = "cc_right2";
}else if(position == 1 && item == 3){
sp_name = "cc_right3";
}else if(position == 1 && item == 4){
sp_name = "cc_right4";
}else if(position == 1 && item == 5){
sp_name = "cc_right5";
}else if(position == 1 && item == 6){
sp_name = "cc_right6";
}
break;
case 1:
if(position == 0 && item == 0) {
sp_name = "ccL_left";
} else if(position == 0 && item == 1){
sp_name = "ccL_left1";
} else if(position == 0 && item == 2){
sp_name = "ccL_left2";
}else if(position == 0 && item == 3){
sp_name = "ccL_left3";
}else if(position == 0 && item == 4){
sp_name = "ccL_left4";
}else if(position == 0 && item == 5){
sp_name = "ccL_left5";
}else if(position == 0 && item == 6){
sp_name = "ccL_left6";
}else if(position == 1 && item == 0) {
sp_name = "ccL_right";
} else if(position == 1 && item == 1){
sp_name = "ccL_right1";
} else if(position == 1 && item == 2){
sp_name = "ccL_right2";
}else if(position == 1 && item == 3){
sp_name = "ccL_right3";
}else if(position == 1 && item == 4){
sp_name = "ccL_right4";
}else if(position == 1 && item == 5){
sp_name = "ccL_right5";
}else if(position == 1 && item == 6){
sp_name = "ccL_right6";
}
break;
}
break;
case R.id.cck_spinner_pos:
switch (pos) {
case 0:
if(type == 0 && item == 0) {
sp_name = "cc_left";
} else if(type == 0 && item == 1){
sp_name = "cc_left1";
} else if(type == 0 && item == 2){
sp_name = "cc_left2";
}else if(type == 0 && item == 3){
sp_name = "cc_left3";
}else if(type == 0 && item == 4){
sp_name = "cc_left4";
}else if(type == 0 && item == 5){
sp_name = "cc_left5";
}else if(type == 0 && item == 6){
sp_name = "cc_left6";
}else if(type == 1 && item == 0) {
sp_name = "ccL_left";
} else if(type == 1 && item == 1){
sp_name = "ccL_left1";
} else if(type == 1 && item == 2){
sp_name = "ccL_left2";
}else if(type == 1 && item == 3){
sp_name = "ccL_left3";
}else if(type == 1 && item == 4){
sp_name = "ccL_left4";
}else if(type == 1 && item == 5){
sp_name = "ccL_left5";
}else if(type == 1 && item == 6){
sp_name = "ccL_left6";
}
break;
case 1:
if(type == 0 && item == 0) {
sp_name = "cc_right";
} else if(type == 0 && item == 1){
sp_name = "cc_right1";
} else if(type == 0 && item == 2){
sp_name = "cc_right2";
}else if(type == 0 && item == 3){
sp_name = "cc_right3";
}else if(type == 0 && item == 4){
sp_name = "cc_right4";
}else if(type == 0 && item == 5){
sp_name = "cc_right5";
}else if(type == 0 && item == 6){
sp_name = "cc_right6";
}else if(type == 1 && item == 0) {
sp_name = "ccL_right";
} else if(type == 1 && item == 1){
sp_name = "ccL_right1";
} else if(type == 1 && item == 2){
sp_name = "ccL_right2";
}else if(type == 1 && item == 3){
sp_name = "ccL_right3";
}else if(type == 1 && item == 4){
sp_name = "ccL_right4";
}else if(type == 1 && item == 5){
sp_name = "ccL_right5";
}else if(type == 1 && item == 6){
sp_name = "ccL_right6";
}
break;
default:
Toast.makeText(getContext(),"ERR 208",Toast.LENGTH_SHORT).show();
}
break;
case R.id.cck_spinner:
switch (pos) {
case 0:
if (type == 0 && position == 0) {
sp_name = "cc_left";
} else if (type == 0 && position == 1) {
sp_name = "cc_right";
} else {
Toast.makeText(getContext(), "You can't change LABEL of this key", Toast.LENGTH_SHORT).show();
}
break;
case 1:
if (type == 0 && position == 0) {
sp_name = "cc_left1";
} else if (type == 0 && position == 1) {
sp_name = "cc_right1";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left1";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right1";
}
break;
case 2:
if (type == 0 && position == 0) {
sp_name = "cc_left2";
} else if (type == 0 && position == 1) {
sp_name = "cc_right2";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left2";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right2";
}
break;
case 3:
if (type == 0 && position == 0) {
sp_name = "cc_left3";
} else if (type == 0 && position == 1) {
sp_name = "cc_right3";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left3";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right3";
}
break;
case 4:
if (type == 0 && position == 0) {
sp_name = "cc_left4";
} else if (type == 0 && position == 1) {
sp_name = "cc_right4";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left4";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right4";
}
break;
case 5:
if (type == 0 && position == 0) {
sp_name = "cc_left5";
} else if (type == 0 && position == 1) {
sp_name = "cc_right5";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left5";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right5";
}
break;
case 6:
if (type == 0 && position == 0) {
sp_name = "cc_left6";
} else if (type == 0 && position == 1) {
sp_name = "cc_right6";
} else if (type == 1 && position == 0) {
sp_name = "ccL_left6";
} else if (type == 1 && position == 1) {
sp_name = "ccL_right6";
}
break;
default:
if (type == 0 && position == 0) {
sp_name = "cc_left";
} else if (type == 0 && position == 1) {
sp_name = "cc_right";
} else {
Toast.makeText(getContext(), "You can't change LABEL of this key", Toast.LENGTH_SHORT).show();
}
}
break;
}
pfKey4.setHint(sp.getString(sp_name, "not_found"));
}
So it seems sp_name simply changes based on those 3 variables and adapterView.getId() which you don't go into. From your code I believe if position is 0 then the name has left, or if 1 then right. The number at the end is item if greater than 0. And type of 0 means it starts with cc_ and type 1 means ccL_. I would make a short function to create sp_name.
String sp_name="";
// type
if(type==0)
sp_name="cc_";
else
sp_name="ccL_";
//position
if(pos==0)
sp_name+="left";
else
sp_name+="right";
//item
if(item>0)
sp_name+=(item+"");
return sp_name;
Like #deperm's answer but shorter.
return (type == 0 ? "cc_" : "ccL_") +
(pos == 0 ? "left" : "right") +
(item > 0 ? "" + item : "");
Unfortunately, I do not understand the problem with this part of my code.
I'm setting up a registration system.
It seems to be wrong when recording correct information.
if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty() &&
!(name.length() < 4) && !(password.length()<5) && email.lastIndexOf(".") - email.indexOf("#") > 2 &&
email.contains(".") && email.lastIndexOf("#") < email.lastIndexOf(".")
&& email.contains("#") ) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Something is Wrong! ", Toast.LENGTH_LONG)
.show();
}
please, help me to edit this. thank you
Use this to validate emails:
public final static boolean isValidEmail(CharSequence target) {
return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches()); }
It is possible that your email validation is wrong here.
Your final code snippet:
if (!name.isEmpty() && !name.length() < 4) && (!password.isEmpty() &&!(password.length()<5)) && isValidEmail(emailId)) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Something is Wrong! ", Toast.LENGTH_LONG)
.show();
}
instead of messing up your validation do a neat check. Either use this email validation
public static boolean isValidEmaillId(String email){
return Pattern.compile("^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))#"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$").matcher(email).matches();
}
Or this one:
public static boolean isValidEmail(CharSequence target) {
return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
and then do your validation like this:
if (!name.isEmpty() && !name.length() < 4) && (!password.isEmpty() &&!(password.length()<5)) && isValidEmail(emailId)) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Something is Wrong! ", Toast.LENGTH_LONG)
.show();
}
i know maybe this is a dumbest question and the question title ever but it's really seriously i have been stuck here for a long time.
Here is it i want to create a cryptography app which to encrypt a text and decrypt it again. I have doing all well and encryption section are done.
There are just two button on my app, which is encrypt and decrypt. When encrypt button pressed, it'l run a function that gonna produce an array named resultXOR. And when decrypt button pressed, i need the resultXOR array to doing the decryption progress. But here i don't know how to take it from encrypt function.
Here i attach my code also, on function toASCII i got that problem. There are the value on resultXOR and i need it for the decrypt section.
Please master help me.
Thanks.
NB. this the code so far i can create :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aesandroid);
final Button enkripButton = (Button) findViewById(R.id.enkrip_button);
final Button dekripButton = (Button) findViewById(R.id.dekrip_button);
final EditText inputKunci = (EditText) findViewById(R.id.input_kunci);
final EditText inputKata = (EditText) findViewById(R.id.input_kata);
final EditText teksEnkrip = (EditText) findViewById(R.id.teks_enkrip);
final EditText teksDekrip = (EditText) findViewById(R.id.teks_dekrip);
final TextView txtResultXOR = (TextView) findViewById(R.id.txtResultXOR);
// set Instance
// String [] kunciArrNumb=null;
// handling for enkripButton
enkripButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String ambilKunci = inputKunci.getText().toString();
String ambilKata = inputKata.getText().toString();
int jumKun = ambilKunci.length();
int jumKat = ambilKata.length();
String enKata = "";
//int jumKun=jumKat;
if(jumKun<jumKat){
inputKunci.setError("Karakter kata kunci tidak boleh kurang dari pesan ");
}else {
jumKun=jumKat;
String[] kunciArrNumb = toNumberKunci(ambilKunci, jumKun);
//kunciArrNumb
String[] kataArrNumb = toNumberKata(ambilKata, jumKat);
//kataArrNumb
ci(kunciArrNumb, kataArrNumb, jumKat, ambilKunci);
}
}
});
dekripButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String ambilKataHasil = teksEnkrip.getText().toString();
String ambilKunci = inputKunci.getText().toString();
int jumKun = ambilKunci.length();
int jumKat = ambilKataHasil.length();
ASCIIXOR(ambilKunci, ambilKataHasil, jumKat);
}
});
}
public void ci(String[] kunciArrNumb, String[] kataArrNumb,int jumKat, String ambilKunci){
int[] hasilCi= new int[jumKat];
String[] konversiCi= new String[jumKat];
for(int i=0;i<jumKat;i++){
hasilCi[i]=(Integer.parseInt(kunciArrNumb[i])+Integer.parseInt(kataArrNumb[i])) % 26;
Log.d("mod", String.valueOf(hasilCi[i]));
}
for(int i=0;i<jumKat;i++){
if(hasilCi[i] == 0){
konversiCi[i]="a";
}
else if(hasilCi[i] == 1){
konversiCi[i]="b";
}
else if(hasilCi[i] == 2 ){
konversiCi[i]="c";
}
else if(hasilCi[i] == 3){
konversiCi[i]="d";
}
else if(hasilCi[i] == 4){
konversiCi[i]="e";
}
else if(hasilCi[i] == 5 ){
konversiCi[i]="f";
}
else if(hasilCi[i] == 6){
konversiCi[i]="g";
}
else if(hasilCi[i] == 7 ){
konversiCi[i]="h";
}
else if(hasilCi[i] == 8){
konversiCi[i]="i";
}
else if(hasilCi[i] == 9 ){
konversiCi[i]="j";
}
else if(hasilCi[i] ==10 ){
konversiCi[i]="k";
}
else if(hasilCi[i] == 11){
konversiCi[i]="l";
}
else if(hasilCi[i] ==12 ){
konversiCi[i]="m";
}
else if(hasilCi[i] == 13){
konversiCi[i]="n";
}
else if(hasilCi[i] ==14 ){
konversiCi[i]="o";
}
else if(hasilCi[i] ==15 ){
konversiCi[i]="p";
}
else if(hasilCi[i] ==16 ){
konversiCi[i]="q";
}
else if(hasilCi[i] == 17){
konversiCi[i]="r";
}
else if(hasilCi[i] ==18 ){
konversiCi[i]="s";
}
else if(hasilCi[i] == 19 ){
konversiCi[i]="t";
}
else if(hasilCi[i] == 20 ){
konversiCi[i]="u";
}
else if(hasilCi[i] == 21){
konversiCi[i]="v";
}
else if(hasilCi[i] == 22){
konversiCi[i]="w";
}
else if(hasilCi[i] == 23){
konversiCi[i]="x";
}
else if(hasilCi[i] ==24 ){
konversiCi[i]="y";
}
else if(hasilCi[i] ==25 ){
konversiCi[i]="z";
}
}String leno = new String();
for(int i=0;i<jumKat;i++){
leno = leno +konversiCi[i];
}
Log.d("cuk", leno);
//teksEnkrip.setText(leno);
toASCII(leno, jumKat, ambilKunci);
}
public String[] toNumberKata(String ambilKata, int jumKat) {
String[] kunciArr = ambilKata.split("(?<=\\G.{1})");
//int jumKat = ambilKata.length();
int alpha = 26;
String[] kataArrNumb = new String[jumKat];
for (int i = 0; i < jumKat; i++) {
if (kunciArr[i].equals("A") || kunciArr[i].equals("a")) {
kataArrNumb[i] = "0";
} else if (kunciArr[i].equals("B") || kunciArr[i].equals("b")) {
kataArrNumb[i] = "1";
} else if (kunciArr[i].equals("C") || kunciArr[i].equals("c")) {
kataArrNumb[i] = "2";
} else if (kunciArr[i].equals("D") || kunciArr[i].equals("d")) {
kataArrNumb[i] = "3";
} else if (kunciArr[i].equals("E") || kunciArr[i].equals("e")) {
kataArrNumb[i] = "4";
} else if (kunciArr[i].equals("F") || kunciArr[i].equals("f")) {
kataArrNumb[i] = "5";
} else if (kunciArr[i].equals("G") || kunciArr[i].equals("g")) {
kataArrNumb[i] = "6";
} else if (kunciArr[i].equals("H") || kunciArr[i].equals("h")) {
kataArrNumb[i] = "7";
} else if (kunciArr[i].equals("I") || kunciArr[i].equals("i")) {
kataArrNumb[i] = "8";
} else if (kunciArr[i].equals("J") || kunciArr[i].equals("j")) {
kataArrNumb[i] = "9";
} else if (kunciArr[i].equals("K") || kunciArr[i].equals("k")) {
kataArrNumb[i] = "10";
} else if (kunciArr[i].equals("L") || kunciArr[i].equals("l")) {
kataArrNumb[i] = "11";
} else if (kunciArr[i].equals("M") || kunciArr[i].equals("m")) {
kataArrNumb[i] = "12";
} else if (kunciArr[i].equals("N") || kunciArr[i].equals("n")) {
kataArrNumb[i] = "13";
} else if (kunciArr[i].equals("O") || kunciArr[i].equals("o")) {
kataArrNumb[i] = "14";
} else if (kunciArr[i].equals("P") || kunciArr[i].equals("p")) {
kataArrNumb[i] = "15";
} else if (kunciArr[i].equals("Q") || kunciArr[i].equals("q")) {
kataArrNumb[i] = "16";
} else if (kunciArr[i].equals("R") || kunciArr[i].equals("r")) {
kataArrNumb[i] = "17";
} else if (kunciArr[i].equals("S") || kunciArr[i].equals("s")) {
kataArrNumb[i] = "18";
} else if (kunciArr[i].equals("T") || kunciArr[i].equals("t")) {
kataArrNumb[i] = "19";
} else if (kunciArr[i].equals("U") || kunciArr[i].equals("u")) {
kataArrNumb[i] = "20";
} else if (kunciArr[i].equals("V") || kunciArr[i].equals("v")) {
kataArrNumb[i] = "21";
} else if (kunciArr[i].equals("W") || kunciArr[i].equals("w")) {
kataArrNumb[i] = "22";
} else if (kunciArr[i].equals("X") || kunciArr[i].equals("x")) {
kataArrNumb[i] = "23";
} else if (kunciArr[i].equals("Y") || kunciArr[i].equals("y")) {
kataArrNumb[i] = "24";
} else if (kunciArr[i].equals("Z") || kunciArr[i].equals("z")) {
kataArrNumb[i] = "25";
} else {
kataArrNumb[i].valueOf(alpha + Integer.parseInt(kunciArr[i]));
}
}return kataArrNumb;
}
public String[] toNumberKunci(String ambilKunci, int jumKun){
String[] kunciArr = ambilKunci.split("(?<=\\G.{1})");
//int jumKun = ambilKunci.length();
int alpha=26;
String[] kunciArrNumb= new String[jumKun];
for(int i=0;i<jumKun;i++){
if(kunciArr[i].equals("A") || kunciArr[i].equals("a")){
kunciArrNumb[i]="0";
}
else if(kunciArr[i].equals("B") || kunciArr[i].equals("b")){
kunciArrNumb[i]="1";
}
else if(kunciArr[i].equals("C") || kunciArr[i].equals("c")){
kunciArrNumb[i]="2";
}
else if(kunciArr[i].equals("D") || kunciArr[i].equals("d")){
kunciArrNumb[i]="3";
}
else if(kunciArr[i].equals("E") || kunciArr[i].equals("e")){
kunciArrNumb[i]="4";
}
else if(kunciArr[i].equals("F") || kunciArr[i].equals("f")){
kunciArrNumb[i]="5";
}
else if(kunciArr[i].equals("G") || kunciArr[i].equals("g")){
kunciArrNumb[i]="6";
}
else if(kunciArr[i].equals("H") || kunciArr[i].equals("h")){
kunciArrNumb[i]="7";
}
else if(kunciArr[i].equals("I") || kunciArr[i].equals("i")){
kunciArrNumb[i]="8";
}
else if(kunciArr[i].equals("J") || kunciArr[i].equals("j")){
kunciArrNumb[i]="9";
}
else if(kunciArr[i].equals("K") || kunciArr[i].equals("k")){
kunciArrNumb[i]="10";
}
else if(kunciArr[i].equals("L") || kunciArr[i].equals("l")){
kunciArrNumb[i]="11";
}
else if(kunciArr[i].equals("M") || kunciArr[i].equals("m")){
kunciArrNumb[i]="12";
}
else if(kunciArr[i].equals("N") || kunciArr[i].equals("n")){
kunciArrNumb[i]="13";
}
else if(kunciArr[i].equals("O") || kunciArr[i].equals("o")){
kunciArrNumb[i]="14";
}
else if(kunciArr[i].equals("P") || kunciArr[i].equals("p")){
kunciArrNumb[i]="15";
}
else if(kunciArr[i].equals("Q") || kunciArr[i].equals("q")){
kunciArrNumb[i]="16";
}
else if(kunciArr[i].equals("R") || kunciArr[i].equals("r")){
kunciArrNumb[i]="17";
}
else if(kunciArr[i].equals("S") || kunciArr[i].equals("s")){
kunciArrNumb[i]="18";
}
else if(kunciArr[i].equals("T") || kunciArr[i].equals("t")){
kunciArrNumb[i]="19";
}
else if(kunciArr[i].equals("U") || kunciArr[i].equals("u")){
kunciArrNumb[i]="20";
}
else if(kunciArr[i].equals("V") || kunciArr[i].equals("v")){
kunciArrNumb[i]="21";
}
else if(kunciArr[i].equals("W") || kunciArr[i].equals("w")){
kunciArrNumb[i]="22";
}
else if(kunciArr[i].equals("X") || kunciArr[i].equals("x")){
kunciArrNumb[i]="23";
}
else if(kunciArr[i].equals("Y") || kunciArr[i].equals("y")){
kunciArrNumb[i]="24";
}
else if(kunciArr[i].equals("Z") || kunciArr[i].equals("z")){
kunciArrNumb[i]="25";
}
else{
kunciArrNumb[i]=Integer.toString(26 + Integer.parseInt(kunciArr[i]));
}
}
return kunciArrNumb;
}
public void toASCII(String leno, int jumKat, String ambilKunci){
final EditText teksEnkrip = (EditText) findViewById(R.id.teks_enkrip);
final TextView txtResultXOR = (TextView) findViewById(R.id.txtResultXOR);
byte[] b = leno.getBytes(StandardCharsets.US_ASCII);
int ascii [] = new int[jumKat];
for(int i = 0;i<jumKat;i++) {
ascii[i]=b[i];
}
String ascii2 [] = new String[jumKat];
for(int i = 0;i<jumKat;i++) {
ascii2[i]=String.valueOf(b[i]);
Log.d("ascii2",ascii2[i]);
}
String binKunci[] = toASCIIkunci(ambilKunci, jumKat);
int resultXOR[] = new int[jumKat];
for (int i=0;i<jumKat;i++){
resultXOR[i]=Integer.parseInt(ascii2[i])^Integer.parseInt(binKunci[i]);
Log.d("XOR",String.valueOf(resultXOR[i])); //this value that i need the resultXOR
}
String finalText = new String();
finalText=finalen(resultXOR, jumKat);
teksEnkrip.setText(finalText);
}
public String[] toASCIIkunci(String ambilKunci, int jumKat){
byte[] bKunci = ambilKunci.getBytes(StandardCharsets.US_ASCII);
String desKunci[] = new String[jumKat];
for(int i=0;i<jumKat;i++){
desKunci[i]=String.valueOf(bKunci[i]);
Log.d("desKun",desKunci[i]);
}
return desKunci;
/*String binKunci[]=new String[jumKat];
for(int i=0;i<jumKat;i++){
binKunci[i]= Integer.toBinaryString(bKunci[i]);
Log.d("kunciBin", String.valueOf(binKunci[i]));
}
return binKunci;*/
}
public String finalen(int[] resultXOR, int jumKat){
String[] finalText= new String[jumKat];
for (int i=0;i<jumKat;i++){
if(resultXOR[i] == 0){
finalText[i]="a";
}
else if(resultXOR[i] == 1){
finalText[i]="b";
}
else if(resultXOR[i] == 2 ){
finalText[i]="c";
}
else if(resultXOR[i] == 3){
finalText[i]="d";
}
else if(resultXOR[i] == 4){
finalText[i]="e";
}
else if(resultXOR[i] == 5 ){
finalText[i]="f";
}
else if(resultXOR[i] == 6){
finalText[i]="g";
}
else if(resultXOR[i] == 7 ){
finalText[i]="h";
}
else if(resultXOR[i] == 8){
finalText[i]="i";
}
else if(resultXOR[i] == 9 ){
finalText[i]="j";
}
else if(resultXOR[i] ==10 ){
finalText[i]="k";
}
else if(resultXOR[i] == 11){
finalText[i]="l";
}
else if(resultXOR[i] ==12 ){
finalText[i]="m";
}
else if(resultXOR[i] == 13){
finalText[i]="n";
}
else if(resultXOR[i] ==14 ){
finalText[i]="o";
}
else if(resultXOR[i] ==15 ){
finalText[i]="p";
}
else if(resultXOR[i] ==16 ){
finalText[i]="q";
}
else if(resultXOR[i] == 17){
finalText[i]="r";
}
else if(resultXOR[i] ==18 ){
finalText[i]="s";
}
else if(resultXOR[i] == 19 ){
finalText[i]="t";
}
else if(resultXOR[i] == 20 ){
finalText[i]="u";
}
else if(resultXOR[i] == 21){
finalText[i]="v";
}
else if(resultXOR[i] == 22){
finalText[i]="w";
}
else if(resultXOR[i] == 23){
finalText[i]="x";
}
else if(resultXOR[i] == 24){
finalText[i]="y";
}
else if(resultXOR[i] == 25){
finalText[i]="z";
}else if(resultXOR[i] == 26){
finalText[i]="1";
}else if(resultXOR[i] == 27){
finalText[i]="2";
}else if(resultXOR[i] == 28){
finalText[i]="3";
}else if(resultXOR[i] == 29){
finalText[i]="4";
}else if(resultXOR[i] == 30){
finalText[i]="5";
}else if(resultXOR[i] == 31){
finalText[i]="6";
}else if(resultXOR[i] == 32){
finalText[i]="7";
}else if(resultXOR[i] == 33){
finalText[i]="8";
}else if(resultXOR[i] == 34){
finalText[i]="9";
}
}
String finalString = new String();
for (int i=0;i<jumKat;i++){
finalString = finalString +finalText[i];
}
return finalString;
}
public String ASCIIXOR (String ambilKunci, String ambilKataHasil, int jumKat){
byte[] bKunci = ambilKunci.getBytes(StandardCharsets.US_ASCII);
String desKunci[] = new String[jumKat];
for(int i=0;i<jumKat;i++){
desKunci[i]=String.valueOf(bKunci[i]);
}
byte[] bHasil = ambilKataHasil.getBytes(StandardCharsets.US_ASCII);
String desHasil [] = new String[jumKat];
for(int i=0;i<jumKat;i++){
desHasil[i]=String.valueOf(bHasil[i]);
}
int resultXORDec[] = new int[jumKat];
for (int i=0;i<jumKat;i++){
resultXORDec[i]=Integer.parseInt(desHasil[i])^Integer.parseInt(desKunci[i]);
Log.d("XORDEC",String.valueOf(resultXORDec[i]));
}
String resultXORDecStr = new String();
resultXORDecStr = finalen(resultXORDec, jumKat);
//ambilKunci OK diambiil dari atas
} // handling for dekripButton
There are multiple ways to get data out of a function. You can create another function in your activity that takes an array and call that from the end of your button press. Such as call
public void encrytpedData(String[] encryption){
//You can now use your array here
}
You can also declare an Array outside of the onClick fucntion and then set that equal to the encrypted Array inside of the button onClick function. If you do this make sure you initialize it first or you could get a null pointer exception.