I WANT TO DIRECT IT TO ANOTHER CLASS
if (rbSingle.isChecked() || rbMarried.isChecked()) {
if (sqlName.getText().toString().length() == 0 && sqlcycle.getText().toString().length() == 0
&& sqlperiod.getText().toString().length() == 0 && sqlAge.getText().toString().length() == 0 && tvper.getText().toString().length() == 0) {
sqlName.setError("Please Input Name");
sqlcycle.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
sqlAge.setError("Put your Birthday");
tvper.setError("Put your first day of last period");
} else if (sqlName.getText().toString().length() == 0 && sqlcycle.getText().toString().length() == 0) {
sqlName.setError("Please Input your Cycle Length");
sqlcycle.setError("Please Input your Period");
} else if (sqlName.getText().toString().length() == 0 && sqlperiod.getText().toString().length() == 0) {
sqlName.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
} else if (sqlcycle.getText().toString().length() == 0 && sqlperiod.getText().toString().length() == 0) {
sqlcycle.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
} else if (sqlName.getText().toString().length() == 0) {
sqlName.setError("Please Input your Cycle Length");
} else if (sqlcycle.getText().toString().length() == 0) {
sqlcycle.setError("Please Input your Cycle Length");
} else if (sqlperiod.getText().toString().length() == 0) {
sqlperiod.setError("Please Input your Cycle Length");
}
} else if(sqlName.getText().toString().length() == 0
&& sqlcycle.getText().toString().length() == 0 && sqlperiod.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(), "Please select your Gender", Toast.LENGTH_SHORT).show();
sqlName.setError("Please Input Name");
sqlcycle.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
}else{
Intent main = new Intent(this, CalendarMain.class);
startActivity(main);
finish();
}
problem: why it wont direct to another page? after the codition i want it to direct to CalendarMain class. can you specify what's the problem in my code?
Related
I made a code to show the amount of times a vowel is used. A, E, I, O, U. The code is counting but it is not conunting correctly.
#Override
public void onClick(View v) {
int letter1 = 0;
int letter2 = 0;
String enter1 = str1.getText().toString();
String enter2 = str2.getText().toString();
for (int i = 0; i < str1.length(); i++) {
if (enter1.charAt(i) == 'a' || enter1.charAt(i) == 'e' || enter1.charAt(i) == 'i' || enter1.charAt(i) == 'o' ||
enter1.charAt(i) == 'u')
{
letter1++;
}
for(int j = 0; j < str2.length(); j ++){
if(enter2.charAt(j) == 'a' || enter2.charAt(j) == 'e' || enter2.charAt(j) == 'i' || enter2.charAt(j) == 'o' ||
enter2.charAt(j) == 'u')
{
letter2++;
}
}
display3.setText("The amount of vowels are :"+ letter1 + " & " + letter2);
}
}
});
My output for Hello World is 2 and 5. 2 for Hello is correct but 5 for World is not. What am I missing?
If we reformat the code slightly, the problem is more apparent:
for (int i = 0; i < str1.length(); i++) {
if (enter1.charAt(i) == 'a' || enter1.charAt(i) == 'e' || enter1.charAt(i) == 'i' || enter1.charAt(i) == 'o' ||
enter1.charAt(i) == 'u') {
letter1++;
}
for (int j = 0; j < str2.length(); j++) {
if (enter2.charAt(j) == 'a' || enter2.charAt(j) == 'e' || enter2.charAt(j) == 'i' || enter2.charAt(j) == 'o' ||
enter2.charAt(j) == 'u') {
letter2++;
}
}
display3.setText("The amount of vowels are :" + letter1 + " & " + letter2);
}
As you can see, the second for loop is inside the first one. That means the second number will be N times larger than it should be, where N is the length of the first word. This matches what's happening: there is only 1 vowel in "world" but there are 5 letters in "hello".
The display3.setText(...) part will also be executed N times, but since it overwrites the previous value you won't be able to notice that, and you'll just see the final value.
To fix, just close the first for loop earlier:
for (int i = 0; i < str1.length(); i++) {
if (enter1.charAt(i) == 'a' || enter1.charAt(i) == 'e' || enter1.charAt(i) == 'i' || enter1.charAt(i) == 'o' ||
enter1.charAt(i) == 'u') {
letter1++;
}
}
for (int j = 0; j < str2.length(); j++) {
if (enter2.charAt(j) == 'a' || enter2.charAt(j) == 'e' || enter2.charAt(j) == 'i' || enter2.charAt(j) == 'o' ||
enter2.charAt(j) == 'u') {
letter2++;
}
}
display3.setText("The amount of vowels are :" + letter1 + " & " + letter2);
Something problem with the brackets
#Override
public void onClick(View v) {
for(Loop){
if(condition){
}
}//first loop
for(Loop){
if(condition){
}
}//second loop
//display here
});//end for onClick
How can I use if statement of a spinner when the selected value = 0?
if( wilayat.setSelection(0) || city.setSelection(0) ||
station.setSelection(0) || distnation.setSelection(0)) {
message = "Select the 4 fields, please..";
Toast.makeText(BusArrivalTime.this, message, Toast.LENGTH_SHORT).show();
}
Try this.
if (wilayat.getSelectedItemPosition() == 0 || city.getSelectedItemPosition() == 0 || station.getSelectedItemPosition() == 0 || distnation.getSelectedItemPosition() == 0) {
message = "Select the 4 fields, please..";
Toast.makeText(BusArrivalTime.this, message, Toast.LENGTH_SHORT).show();
}
Just use getSelectedItemPosition() method.
You make sure that wilayat、city and so on were Spinner.
Add judgement
if (wilayat != null || city != null || station != null || distnation != null) {
// do something
}
I am writing a code in android for rpn calculations and it crashes when i have operators inside brackets. Eg(89+7) crashes but (8)+7 outputs 15.
This is the method i use for infix to postfix conversion:
private static List<String> convert(String input) {
int p = 0;
String pfb = "";
Stack<Character> chara = new Stack<>();
List<String> pfa = new ArrayList<>();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
if (pfb.length() > 0) {
pfa.add(pfb);
}
pfb = "";
if (chara.empty()) {
chara.push(ch);
} else {
Character chTop = (Character) chara.peek();
if (chTop == '*' || chTop == '/')
p = 1;
else if (chTop == '+' || chTop == '-')
p = 0;
if (p == 1) {
if (ch == '+' || ch == '-') {
pfa.add(String.valueOf(chara.pop()));
i--;
Log.d("pfa", "" + input.length() + "" + i);
} else { // Same
pfa.add(String.valueOf(chara.pop()));
i--;
}
} else {
if (ch == '+' || ch == '-' && chara.peek() != '(') {
pfa.add(String.valueOf(chara.pop()));
chara.push(ch);
} else
chara.push(ch);
}
}
} else if (ch == '(') {
chara.push(ch);
// Log.d("St",""+chara.peek());
} else if (ch == ')') {
`//Code crashes here` while (chara.peek() != '(' && !chara.empty()) {
pfa.add(String.valueOf(chara.pop()));
}
if (!chara.empty() && chara.peek() != '(')
return null; // invalid expression
else
chara.pop();
} else {
pfb += ch;
}
// Log.d("pfa",""+pfa+"");
}
return pfa;
}
You code crashes because first you peek the stack element and then checking the empty condition.
it should be like
while ( !chara.empty() && chara.peek() != '(' )
getch(); command is used in c++ to pause the program, What should i use in Android Studio to pause the program?
for (int i = 0; i < 2; i++){
if ((b[0].getText().toString() == c && b[1].getText().toString() == c && b[2].getText().toString() == c) || (b[3].getText().toString() == c && b[4].getText().toString() == c && b[5].getText().toString() == c) || (b[6].getText().toString() == c && b[7].getText().toString() == c && b[8].getText().toString() == c) || (b[0].getText().toString() == c && b[3].getText().toString() == c && b[6].getText().toString() == c) || (b[1].getText().toString() == c && b[4].getText().toString() == c && b[7].getText().toString() == c) || (b[2].getText().toString() == c && b[5].getText().toString() == c && b[8].getText().toString() == c) || (b[0].getText().toString() == c && b[4].getText().toString() == c && b[8].getText().toString() == c) || (b[2].getText().toString() == c && b[4].getText().toString() == c && b[6].getText().toString() == c)){
if (c == "X"){
paper.setText("You Won the Game.");
}
else{
paper.setText("Computer Won the Game.");
}
}
I want to pause the program where i am displaying the result of the game using paper.setText(); command.
If you are inside of an Activity simply call this.finish();
Edit: I'm not sure exactly what you're trying to do, but it's typically not good practice to close the program without the user consenting to do so. After you have shown your text it may be better to show a dialog such as:
"Would you like to play again?"
[Yes] [Close Game]
You can accomplish that with the following code:
new AlertDialog.Builder(this)
.setTitle("Play again?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// start game over
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyActivity.this.finish();
//use the class name of you activity + this
//to access your Activity's finish rather than try and perform finish on an onClick
}
})
.setIcon(android.R.drawable.myicon)
.show();
I have a list of numbers, in that list I want to check if any of the numbers match, and if so return 'true'. Essentially, what I want to check is if the numbers match, then do not save. I've gotten a seemingly very inefficient method to check. Any help would be appreciated, thanks!
if (mFav1Compare == mNumber1Compare || mFav1Compare == mNumber2Compare || mFav1Compare == mNumber3Compare || mFav1Compare == mNumber4Compare || mFav1Compare == mNumber5Compare) {
return true;
}
if (mFav2Compare == mNumber1Compare || mFav2Compare == mNumber2Compare || mFav2Compare == mNumber3Compare || mFav2Compare == mNumber4Compare || mFav2Compare == mNumber5Compare) {
return true;
}
if (mFav3Compare == mNumber1Compare || mFav3Compare == mNumber2Compare || mFav3Compare == mNumber3Compare || mFav3Compare == mNumber4Compare || mFav3Compare == mNumber5Compare) {
return true;
}
if (mFav4Compare == mNumber1Compare || mFav4Compare == mNumber2Compare || mFav4Compare == mNumber3Compare || mFav4Compare == mNumber4Compare || mFav4Compare == mNumber5Compare) {
return true;
}
if (mFav5Compare == mNumber1Compare || mFav5Compare == mNumber2Compare || mFav5Compare == mNumber3Compare || mFav5Compare == mNumber4Compare || mFav5Compare == mNumber5Compare) {
return true;
}
If you have two lists and you want to know if the second list contains at least one element of the first list you could try
public <T> boolean listMatches(List<T> list1, List<T> list2) {
for (T element : list1) {
if (list2.contains(element)) {
return true;
}
}
return false;
}
Do you thought this?
ArrayList<Integer>listNumbers = new ArrayList<>();
listNumbers.add(99);
listNumbers.add(99);
listNumbers.add(11);
listNumbers.add(10);
for(int i = 0; i < listNumbers.size(); i++){
if(!listCheck(listNumbers.get(i))){
//save something
}
}
and this method
private boolean listCheck(int number){
for(int i = 0; i < listNumbers.size(); i++){
if(number == listNumbers.get(i)){
return true;
}
}
return false;
}