This is a problem I've been trying to fix for 3 hours now.
How the app works:
I've got a list of numbers in a txt file (in assets). I'm reading that txt file line by line and I set the text of a TextView to the current line. Now I have to type the same number in an EditText. But if I compare the EditText and the current line, the app says they are not the same, although they are.
I've got a file in assets with different numbers, like this:
3
9
8
14
[finish]
The [finish] tag will be used later so I can finish the application, but is pretty unnecessary now.
I've defined the following strings and views:
public String curval;
public EditText etds;
public TextView curvalte;
A part of my onCreate() method:
etds = (EditText)findViewById(R.id.editText1);
buttonds = (Button)findViewById(R.id.button1);
curvalte = (TextView)findViewById(R.id.textView2);
try {
inds = this.getAssets().open(assetname);
} catch (IOException e) {
e.printStackTrace();
}
readerds = new BufferedReader(new InputStreamReader(inds));
Now I'm reading from the file and setting it as text of curvalte:
curval = readerds.readLine();
curvalte.setText(curval);
And it works just fine.
But then you have to type the same number that is now shown in curvalte in the EditText.
If I try to use equals() on the text in EditText with curval, it always says that EditText.getText().toString doesn't equal curval:
if(etds.getText().toString().equals(curval.toString()){
// The code here
}else{
// This is what I get
Toast.makeText(getBaseContext(), "Wrong answer!", Toast.LENGTH_LONG).show();
}
Even if I enter the correct answer, I get that toast saying it's wrong.
Removing the .toString() didn't fix it and I've got no idea what I could try.
Maybe it's because of the string that actually contains a number?
Try printing both of the values out and looking at them. You may not even be getting the right values from the file or perhaps your getting some weird formatting characters like "\n"
Try this with .trim(), sometimes that is the issue
if(etds.getText().toString().trim().equals(curval.toString().trim()){
// The code here
}else{
// This is what I get
Toast.makeText(getBaseContext(), "Wrong answer!", Toast.LENGTH_LONG).show();
}
Alright, I found the problem. It was the formatting of the assets file!
Cp1252 encoding fixed it.
Maybe you have spaces in the string or the difference between Uper and LowerCase. Try this version:
if(etds.getText().toString().trim().toLowerCase().equals(curval.toString().trim().toLowerCase())){
Log.i("Yes", "equal");
} else {
Log.i("NO", "not equal");
}
Related
I am pulling data from a Website to my application. I want the TextView to display the result I want from the website immediately as the user launches the app. However html codes make the result look weird some times and I am trying to correct it. I have the codes that will do what I am trying to do. I just can't figure out how to get it to do everything automatically at app launch. It needs to pull the code from the website and if it receives any special symbols within the string I want it to correct it as soon as the app launches. Here is an example...
TextView tv = (TextView) findViewbyId(R.id.my_textview_result);
tv.setText(resultFromWebsite);
The result it pulled: You u0026 Me Forever!
The result I want: You & Me Forever! My app should correct that.
Here is my correction code...
public void symbolTextFilter(TextView myTv) {
String getData = tv.getText().toString();
if (getData.contains("u0026") {
String replace = getData.replace("u0026", "&");
myTv.setText(replace);
}
Now on my onCreate Method
TextView tv = (TextView) findViewbyId(R.id.my_textview_result);
tv.setText(resultFromWebsite);
symbolTextFilter(tv);
It will not make that correction. It will if I put the symbolTextFilter(tv) in a onClickListener button though. I don't want to assign the correction in a button. I want it automatically. My guess is, everything that I have in the onCreate is happening too fast for corrections to be made. How do I fix that? Thanks in advance!
Try this:
tv.setText(symbolTextFilter(resultFromWebsite))
You should use the method symbolTextFilter to handle the string only:
public void symbolTextFilter(String input) {
if (input.contains("u0026") {
return input.replace("u0026", "&");
} else {
return input
}
Nevermind, I got it! I'm not sure where the "\" came from because it wasn't in the original string that it pulled before the correction. I fixed it with this...
public String symbolTextFilter(String input) {
if (input.contains("u0026") {
return input.replace("\\" + "u0026", "&");
} else {
return input
}
i'm using autocompletebubbletext library (https://github.com/FrederickRider/AutoCompleteBubbleText) which display the list of items to chose from in a list and allow in same time to chose the items from the editetxt..
My problem is as follow:
after the user choses a number of items(=Multiple inputs) .. i want to display a text as an output when clicked on a button (depending on the items chosen of course) as explained in this picture: (https://i.imgur.com/QQuzFvl.png)..
but i got stucked in getting the string of itemsChosen from the edittext
FIRST: i am not sure which return value to use!!
SECOND: i assumed i should use "checkedIds" and I've tried A lot of solution in internet , i've been trying different ideas all day, from what i have tried: ( Ps: i used a toast to see if the methods did work)
edittext.getText().toString() > nothing appears in Toast
i have tried to turn the setHash to String[]: then turning the String[] to one string like:
content=editText.getCheckeditems();//getcheckeditems returns checkedIds which is = new HashSet<String>()
String[] BLANA= content.toArray(new String[content.size()])
data= TextUtils.join(",",BLANA);
it didnt work, in Toast i got"[]"
For the MainActivity.Java (i have the same as here):
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/samplelist/src/main/java/com/mycardboarddreams/autocompletebubbletext/samplelist/SampleActivity.java
For MultiSelectEditText.java (i Have same as here) :
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/library/src/main/java/com/mycardboarddreams/autocompletebubbletext/MultiSelectEditText.java
WHAT is the solution? (to get a string so i can use it later)
PS: if there is another way(another library or methode) to get what i want to achieve in the first place , i would love to try it..
EDIT: THIS IS A CODE THAT LOOKS PROMISING BUT DIDN'T WORK!
in MultiSelectEditText.java
public String datachosen(){
String [] blan= checkedIds.toArray(new String[0]);
StringBuilder builder = new StringBuilder();
for (String string : blan) {
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(string);
}
String DATATORETURN = builder.toString();
return DATATORETURN;
}
in MAINACTIVTY.JAVA
MultiSelectEditText editText = (MultiSelectEditText)findViewById(R.id.auto_text_complete);
content=editText.datachosen();
Toast.makeText(DecisionTree.this, content,
Toast.LENGTH_LONG).show(); // TOAST INCLUDED IN A BUTTON OF COURSE
OUTPUT: TOAST SHOWS NOTHING!
Solved it ..
i intialize the edit text before on create ..and defin it later after onCreate()..
and got string with the normal edittext.getText().toString(); method!
Simple but was hard to detect the problem!
I have button for open other app by write name of package in edittext i want if user not write any text in edittext and click button for open app get error toast like you should name of package first .. how do like this
?
If I understand you correctly, you need to see if the user typed something into an EditText, and if they didn't, show a toast.
// Storing EditText value in package
String package = editText.getText().getString().trim();
// calculating how many character are there in string.
int checkEmpty = package.length();
if (checkEmpty == 0) {
Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
}
The better way to check empty in android you can write this code.
if(TextUtils.isEmpty(editText.getText().toString())){
Toast.makeText(getApplicationContext(), "This is empty.", Toast.LENGTH_LONG).show();
}
I have some EditText in my Activity. The user has to fill them out and confirm his input. Some of the EditText are only for numbers. To make sure that there are only valid values i'm trying to cast the input to an Integer using Integer.parseInt( . . . ). The problem is no matter what the string is the NumberFormatException wont be thrown. I debuged the problem and the corosponding line is executed every time but without throwing an exception.
Here is the Code of my method:
private boolean formNotEmpty()
{
boolean returnValue = true;
for(int i = 0; i < _editText.size();i++){
if(_editText.get(i).getText().toString().trim().length() == 0)
{
returnValue = false;
_toastMessage = "Es müssen alle Felder ausgefüllt werden.";
break;
}else if(_editText.get(i).getHint().equals(getResources().getString(R.string.rentactivity_hint_userPLZ))
||_editText.get(i).getHint().equals(getResources().getString(R.string.rentactivity_hint_userTelefon)))
{
try{
Integer.parseInt(_editText.get(i).getText().toString().trim());
}catch(NumberFormatException e)
{
_toastMessage += "In "+_editText.get(i).getHint().toString() +" dürfen nur Zahlen stehen. \n";
}
}
}
return returnValue;
}
I've forgotten to set returnValue in the catch block to false. The _toastMessage gets a new string if the method returns true. This is the reason i assumed that the exception is never thrown. I'm realy sorry for the trouble i caused.
Thank you very much
If you are counting on the code to throw you an exception, and you want to use this as a means to validate the data to accept only numbers, then this is not the right approach to this. Exception handling should'nt explicitly be used as an if-else like you suggest. A simpler, and perhaps more correct way to accomplish what you want would be :
Declare in the layout xml
android:inputType="number"
for that particular edittext you want to confine entries only to numbers.
When you do this, you only get option to type in numbers, when you try to fill that editText, as a user. After adding this in the xml, you would not need to depend on the Integer.parseInt() API to throw exception, for your validation.
Also you can use the attr of EditText:
android:digits="0123456789."
http://developer.android.com/reference/android/widget/TextView.html#attr_android:digits
After that user can write only numbers.
I'm writing an app that takes in an input from the AddBook class, allows it to be displayed in a List, and then allows the user to Search for their book. To this end, I'm creating a temporary EditText, binding it to the box where the user actually enters their search value, then (after ensuring that it is not empty) I compare what they've entered for the ISBN number with the ISBN numbers of each entry in the arrayList of <Book> custom objects, the list being named books.
Problem is, when I try to parse the EditText into an Int, it doesn't seem to work. I first tried using toString() on the EditText, then using Integer.parseInt() on the result of that method, but it hasn't worked out, as the conversion is seemingly unsuccessful;
All of the resources are in place and the code compiles properly, so those aren't the problems here.
EditText myEdTx = (EditText) findViewById(R.id.bookName);
if(myEdTx.getText().equals("")){Toast toast = Toast.makeText(this, "Please enter something for us to work with!", Toast.LENGTH_SHORT);
toast.show();}
else{
//resume here
for(int i=0; i<books.size(); i++)
{
Book tBook = new Book(i, null, null); tBook = books.get(i); String s=myEdTx.toString();
int tInt = Integer.parseInt(s);`
To get the string representation of an EditText's content, use:
String s = myEdTx.getText().toString();
Using toString() directly on the EditText gives you something like:
android.widget.EditText{40d31450 VFED..CL ......I. 0,0-0,0}
...which is clearly not what you want here.
You assume the user inputs a number into the text field, but that is unsafe, as you only get a string text (which theoretically can contain non-numbers as well). When I remember correctly, you can adjust a text field in android where a user only can input numbers, which should suit you more.
NumberFormatException occurs when Integer.parse() is unable to parse a String as integer, so, its better to Handle this exception.
String s = myEdTx.getText().toString();
try {
int tInt = Integer.parseInt(s);
} catch( NumberFormatException ex ) {
//do something if s is not a number, maybe defining a default value.
int tInt = 0;
}
So the current String here you are trying to parse is with white space in the line
and integer class unable to parse that white space. So use following code.
String s=myEdTx.getText().toString();
int tInt = Integer.parseInt(s.trim());
String s = myEdtx.getText().toString().trim();
int iInt = Integer.parseInt(s);