Can't cast from Edit Text to String [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
EditText entername = (EditText)findViewById(R.id.enter_name);
// this line convert it to String
String q =entername.getText().toString();
//why it is not working in this way ???
String d = (String) entername.getText();

The method getText() doesn't return a String, but an Editable.
Note that an Editable doesn't extend String, but it implements the CharSequence interface, so it can be used in many methods that accept CharSequence.

Because EditText#getText returns an Editable, not a String.

Related

Multiplying a string containing numbers with another string containing same? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have a string
String string = "1.18";
I want to multiply the above string with another string that is
String f1= "2.54";
What would I have to do to multiply these two values? I looked this up online and came across a function called Integer.ParseInt() that converts the string to it's numerical values, so i tried
System.out.println(Integer.parseInt(string) * Integer.parseInt(f1));
It doesn't work. I want to know what am I doing wrong. What's the proper way of doing it? I would find it very helpful if someone could help me understand, thanks!
Try Double.parseDouble(string)
Integer is not for decimal number. Use Double.parseDouble() or use BigDecimal type if you need exact result.

Remove single quotes in a string Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need some help to replace all the single quotes in a string.
This is my string: The State of the water = 'ICE'
I want to remove the single quotes around ICE.
str = str.replaceAll("\'","");
Use this
String str = "The State of the water = 'ICE'";
str = str.replaceAll("'","");

How would you display each element of an array that is returned from another method in Java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
For example, I have my main method as well as the method returnOdds(original), that returns the integer array "odd"
How would I print the elements of this array in the main method? With a for loop?
A for loop would work. If you don't care about the format, though, a simpler solution might be to use Arrays.toString, which will convert in the form "[elem1, elem2, ..., elemn]".
"With a for loop?"
answer : "yes"

how to change particular values in string array in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
String[] arraylist = {"0","0","0","0","0","0","0"};
but now I want to replace inside the array above to 1, to make it like this
String[] arraylist = {"0","0","0","1","0","0","0"};
Once try follow if you know position you want to change
arraylist[3]="1";
Hope this will helps you.

What's wrong with my assertEquals? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Still getting the red bar. What's wrong with the AssertEquals?
public void testFindEmployeeByID() {
StubEmployeeRepositoryImpl result = new StubEmployeeRepositoryImpl(dataSource);
List<Employee> emp = result.findEmployeesByName("John", "X");
assertEquals("John"+"X", result.findEmployeesByName("John", "X"));
}
Probably assertEquals doesn't know how to compare List and String...

Categories