Cannot resolve substring() or indexOf() methods - android

I am new to android and i am trying to search for a certain character in an edit text and then extract all the characters before it in a new variable using the substring() and indexOf() methods , but android studio saying that it cannot solve either method . So please tell me what i am doing wrong with the code. Here is the declaration for the edit text :
EditText text;
text = (EditText) findViewById(R.id.text);
And the code for calling these methods :
if(text.getText().toString().contains("+")) {
String before = text.substring(text.indexOf("+") - 1);
}

You're calling substring() and indexOf() on your text variable, which is an EditText object, and they're string methods. Convert to string first and then use them, like so:
String textString = text.getText().toString();
if(textString.contains("+")) {
String before = textString.substring(textString.indexOf("+") - 1);
}

You need to call them in the same way you call contains(): text.getText().toString() because the two methods are part of the String class, not EditText.

You can try String before = text.split("+")[0]; This will return you everything before the first '+'

Related

What does getText().toString() return?

I am new to Android. Please help me to clear about the code below:
String num1 = etfirst.getText().toString();
String num2 = etsecond.getText().toString();
String keyword is used for characters, but here its used for receiving the numbers.
Please explain about the dot operator and getText().toString().
Thanks in advance.
Here you are getting text from etfirst and ersecomd and convert it into string using toString().
getText(): Return the text that is displaying.
toString(): Returns a string representation of the object.

Android comparing strings with == to each objects

I'm coming from C#, so typically I try to relate everything that i'm doing.
I cannot figure out why the below statement doesn't work. Basically String val = "admin". Then an I have an if statement, however the if statement is always false. I'm sure it's something simple.
Thanks!
EditText edt = (EditText) findViewById(R.id.email);
//String val = edt.getText().toString();
String val = "admin";
EditText edt2 = (EditText) findViewById(R.id.password);
String val2 = edt2.getText().toString();
if(val.toString() == "admin") {
String hero = val;
}
You should use
if (val.equals("admin")) {
String hero = val;
}
instead of using an equal sign. Using an equal sign in java is asking if they're the same object, which will be false even if the strings are the same.
Also, be careful with what you're doing inside of the if statement, because the variable "hero" won't be accessible outside of that block.
In Java you can't compare strings using ==
You need to change your if statment like this
if(val.equals("admin")){}
First of all you have never changed the value of String val to anything so there is no need to try convert it to a string in your if statement.
String val = "admin";
if (val == "admin") {
//code here
}else{
//code here
}
Hope this helps
In java, == operator check the address of each value, and equals() method check the value.
So If you want to compare the value of each string, you should use the equals() method.
Please search for the concept of 'call by reference' and 'call by value'.
And you already declare val to String, so it didn't need toString().
if(val.equals("admin")) {
String hero = val;
}
I'm surprised no one mentioned the difference between .matches() and .equals() depending on your needs, what you could also be looking for is .matches()
if(val.toString().matches("admin")) {
String hero = val;
}
Matches checks the match of a String to a regular expression pattern, not the same string.
For example:
"hello".equals(".*e.*"); // false
"hello".matches(".*e.*"); // true
use .equals() instead of ==.
for example:
if (val.equals("admin")) ...

Android - If doesn't work i am getting string from text box

if condition doesn't validate the value inside string mentioned below.
//getting input box value
newText = input.getText().toString();
//using if condition newText got value "a"
if (newText=="a")
{
//do something
}
but the above if condition doesn't work i check string got right value which is a.
thanks in advance
In java you should use equals method to compare the values of strings:
if ("a".equals(newText)) {
//do something
}
To compare the references you can use ==. For more information check this: https://stackoverflow.com/a/513839/3225458

Can't readout ArrayList in Android

I deliver a ArrayList to another method, where I just wanna readout a specific String of the list.
public void pruefeWerHat(ArrayList<Teilnehmer> test){
System.out.println(test);
I get this in LogCat
"1 PeterPan 0 0, 2 Hansi 0 0"
now I just want to use the name, but if I say (after sysout)
String name = test.get(1);
the problem he said to is, that he cannot convert from Teilnehmer to String. I also tested Teilnehmer.get(1) but it doesn't work neither.
When you do
System.out.println(test);
the toString() method is automatically used. This method is in the Object class, so all objects in java can call this method.
When you do
String name = test.get(1);
the toString() method is not called on it's own, you have to call it yourself. To do this, simply use
String name = test.get(1).toString();
Also, if you want to change what is printed, you can overwrite the toString() method in your class.
#Overwrite
public String toString() {
String stringToPrint = "This string will be printed";
return stringToPrint;
}
Now when you do
System.out.println(test);
instead of seeing "1 PeterPan 0 0, 2 Hansi 0 0" you will see "This string will be printed" (or whatever you choose to add in your toString() implementation.
When you print test toString function is called so use this in your code
String name = test.get(1).toString();
What are the members of Teilnehmer?
You need to use something like
string name = (Teilnehmer)test[1].Name
where Name is the field you are trying to extract
The get(int index) method available to ArrayList returns type E. In this instance, it returns type Teilnehmer, which is obviously not a String. You can try and cast Teilnehmer (although probably not desirable) to String or simply call the .toString() method (e.g; test.get(1).toString()) inherited from type Object. Or, if desired, calling a method that returns a String. test.get(1).getNameAsString();
The reason you are allowed to call the type in System.out.println(Teilnehmer) is that println makes a call to the object's string representation:
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}

NumberPicker.Formatter does not seem to be invoking in Android

I am trying to use a NumberPicker to display an array of Strings, but when I try and get the value back of the current String, the value is the integer index value of the String within the array which is the default response.
I have been trying to use NumberPicker.Formatter to get the actual String as a value back as opposed to the integer index value. I have implemented this, but it doesn't seem to be invoked. Could someone please tell me why this is and what I can do to fix it. Thanks in advance.
Here is the code:
final NumberPicker npUnits = (NumberPicker) numberPickerView.findViewById(R.id.numberPicker2);
npUnits.setMinValue(1);
npUnits.setDisplayedValues(tableUnitsArray);
npUnits.setMaxValue(tableUnitsArray.length);
npUnits.setFormatter(new NumberPicker.Formatter()
{
#Override
public String format(int value)
{
ArrayList<String> stringArrayList = (ArrayList<String>) Arrays.asList(tableUnitsArray);
String defaultUnits = stringArrayList.get(value);
System.out.println("Value formatted result: " + defaultUnits);
return defaultUnits;
}
});
npUnits.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
NumberPicker allows you to pick your values based on the min and max values you specify. It does not need you to call setDisplayedValues(). NumberPicker uses the String array you specify as a set of alternate values which causes your formatter to be ignored. Try removing the call to setDisplayedValues().
If your values for your NumberPicker are not so straight forward (i.e not from min to max incrementing by 1), then you can transform your values in your formatter to get the desired number.
I have found out the answer to my question. NumberPicker.Formatter was the wrong thing that I was doing as I didn't actually need to format anything. I just needed to get the current value from the String[] displayed values.
This is the code that worked for me:
List<String> stringArrayList = (List<String>) Arrays.asList(npUnits.getDisplayedValues());
String defaultUnits = stringArrayList.get(npUnits.getValue() - 1);
I found a solution for a few bugs in NumberPicker that works in APIs 18-26 without using reflection and without using setDisplayedValues() here.

Categories

Resources