Android Studio count not outputting number correctly - android

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

Related

How can Separate a number from a text in Android?

this is my code :
String addchar = "";
String tempchar;
int len = strline.length();
char[] chars = strline.toCharArray();
int amount = 0;
for (int i = 0; i < len; i++) {
tempchar = String.valueOf(chars[i]);
if (tempchar == "0" || tempchar == "1" || tempchar == "2" || tempchar == "3" || tempchar == "4" || tempchar == "5" || tempchar == "6" || tempchar == "7" || tempchar == "8" || tempchar == "9") {
addchar=tempchar+addchar;
}
}
amount=Integer.parseInt(addchar);
but when run this code , I see that amount is empty!
I want to extract , the number that there is in strline
Slightly less elegant than Nilesh's answer, but as an alternative:
StringBuilder sb = new StringBuilder();
for (char c : strline.toCharArray()) {
if ((sb.length() == 0 && "-".equals(c)) || Character.isDigit(c)) sb.append(c);
}
int amount = Integer.parseInt(sb.toString());
Edit Amended to allow for initial negative sign
try this use Matcher
Matcher matcher = Pattern.compile("\\d+").matcher("a22dsdddd212");
while(matcher.find()) {
Log.e("number :-> ",matcher.group()+"");
}
If you want all numeric char, you can use replaceALL in this way:
String numericString = strline.replaceAll("[^0-9]", "");
then use ParseInt.
Hope it helps.

Empty Stack Exception in Rpn conversion

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() != '(' )

Compare list of numbers to see if they match in Android

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;
}

after validation how to direct it to another page?

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?

Arrays not getting values

I have been working on this yahtzee project and have run into a problem. The dice_number array doesn't seem to be getting the randomly generated values. The oneScore TextView always displays "--". I'm posting my code. Thanks in advance for any help given. Also if you need to see anymore of the code let me know.
switch (v.getId()) {
case R.id.rollBtn:
for(i = 0; i < 5; i++){
randnum = random.nextInt(5);
if(i == 0){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 1){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 2){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 3){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 4){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 5){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
}
break;
case R.id.onesBtn:
for (i = 0; i < 5; i++)
{
if (dice_number[i] == 1) {
dice_count[0] += 1;
oneScore.setText(Integer.toString(dice_count[0]));
}
else
oneScore.setText("--");
}
That second for loop re-sets the text for each number. So if you have an array
dice_number = {3, 2, 1, 1, 4}
The TextView will be set to --, then --, then 1 (if dice_count[0] was 0), then 2, then --, so all you will see is the result from dice_number[4], which is the last --.
You need to either construct a string or use multiple TextViews to see the whole array, not just the last element.
Also, the if statements in the first for loop aren't doing anything as far as I can tell.
Not an answer, just a suggested refactoring:
case R.id.rollBtn:
for(i = 0; i < 5; i++){
randnum = random.nextInt(5);
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
break;
in fact that piece of code that says if(i == 5) {..} will never be reached because the value of i is always 0,1,2,3, or 4.

Categories

Resources