[英]Count Vowels in 2 strings with Android Studio on button click
我目前正在與android studio進行作業,需要做一些事情。
單擊按鈕時,它需要在1個字符串中顯示Hello,在第二個字符串中顯示World。
它需要比較兩個不同的字符串並顯示它們是否相等或不同
我所有這些都工作正常,最后一部分我遇到了麻煩。
我目前有4個文本視圖設置和3個按鈕。
1個文本視圖以顯示Hello(1)文本視圖以顯示World(世界)1文本視圖以顯示上述字符串是否匹配
最后一個我要努力解決的問題是一個文本視圖,以顯示前兩個字符串中包含的元音數量。
我也有3個按鈕,第一個按鈕顯示前2個文本視圖
第二個按鈕是用來比較前兩個字符串的按鈕
最后一個按鈕是顯示元音的數量
這是我為最終按鈕和最終文本視圖設置的當前代碼,我搞砸了很多事情,只是沒有運氣。 這將加載,但是按下按鈕不會對單擊產生任何作用。
目前沒有錯誤消息,它什么也不顯示。 我需要能夠比較字符串1和字符串2(分別是txt和txt2),然后顯示那些以“ Hello World”結尾的字符串中的元音數量。
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(view.getId()==R.id.button2)
{
String file_name=txt4.getText().toString()+".txt";
StringBuffer sBuffer = new StringBuffer();
int count=0;
FileInputStream fis;
try {
fis = openFileInput(file_name);
DataInputStream dataIO = new DataInputStream(fis);
String strLine = null;
while ((strLine = dataIO.readLine()) != null)
{
sBuffer.append(strLine +"\n");
}
dataIO.close();
fis.close();
for(int i=0;i<sBuffer.length();i++)
{
switch(sBuffer.charAt(i))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
count++;
break;
}
}
Toast.makeText(getApplicationContext(),"Total Vovels Are : "+count,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Full code:
package com.example.project3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
TextView txt;
TextView txt2;
TextView txt3;
TextView txt4;
Button btn;
Button btn2;
Button btn3;
String file_name;
String strFileContents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txtHello);
txt2=(TextView) findViewById(R.id.txtWorld);
txt3=(TextView) findViewById(R.id.txtCompare);
txt4=(TextView) findViewById(R.id.txtOccur);
btn=(Button) findViewById(R.id.button3);
btn2=(Button) findViewById(R.id.buttonCompare);
btn3=(Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
txt.setText(getResources().getString(R.string.txt_hello));
txt2.setText(getResources().getString(R.string.txt_world));
}
});
btn2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (txt.equals(txt2))
txt3.setText(getResources().getString(R.string.txt_equal));
else
txt3.setText(getResources().getString(R.string.txt_not));
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(view.getId()==R.id.button2)
{
String file_name=txt4.getText().toString()+".txt";
StringBuffer sBuffer = new StringBuffer();
int count=0;
FileInputStream fis;
try {
fis = openFileInput(file_name);
DataInputStream dataIO = new DataInputStream(fis);
String strLine = null;
while ((strLine = dataIO.readLine()) != null)
{
sBuffer.append(strLine +"\n");
}
dataIO.close();
fis.close();
for(int i=0;i<sBuffer.length();i++)
{
switch(sBuffer.charAt(i))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
count++;
break;
}
}
Toast.makeText(getApplicationContext(),"Total Vovels Are : "+count,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}
該注釋器是正確的,由於if語句,您的代碼未執行。
onClick(View view)
方法中的參數始終是被單擊的視圖,因此在您的情況下為btn3
。 這是為了給程序員提供對單擊視圖的引用。
從您的方法中刪除if
塊(或將條件更改為view.getId()==R.id.btn3
如注釋所示,但這毫無意義),它應該可以工作。 在這種簡單情況下,您不需要使用獲取的視圖引用,因為您已經知道它將是btn3
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.