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 1 year ago.
Improve this question
I am creating an Android Studio app in the Kotlin language and that utilizes the doAfterTextChanged function as a TextWatcher for an editText. However, when the user changes the text in the editText after it has already been changed, the app crashes. Is there any alternative (and simple) function I can use or something to add to my program. Here is an example of what I currently have:
editText.doAfterTextChanged() {
score = score + 15 * editText.text.toString().toInt()
totalscore.text = "Score: " + score.toString()
}
What would happens when you clear the text from the EditText, e.g. making it empty (text = "")?
score = score + 15 * editText.text.toString().toInt()
In that scenario, you are basically trying to convert "" to an integer, which does not work obviously. NOTE, you are not seeing this the first time, because the code only triggers after text changed.
Try this instead:
score += 15 * (editText.text.toString().toIntOrNull() ?: 0)
totalscore.text = "Score: $score"
Related
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 7 years ago.
Improve this question
I am beginner, I want to make one simple application that will count number of lines, words and characters from whatever we have entered in Editext field. i want result like:
EditText : My name is abc.
Button CLick---->
Result:
No. of Words : 4
No. of Characters : 14
No. of Lines : 1
First get the Edittext value.then do like as java word counter
EditText e1=(EditText) findViewById(R.id.youredittextid);
String mytext=e1.getText().toString();
Apply java code for mytext
lines in edittext can vary depending on device....
for words:
char ch[]= new char[s.length()]; //in string especially we have to mention the () after length
for(i=0;i<s.length();i++)
{
ch[i]= s.charAt(i);
if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
c++;
}
and for characters: s.length()
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
I am a beginner at programming Android, please help with this simple project.
I want an integer to show up in toast, which I write in textBox. I do not know what to put in the highlighted part here.
Sorry for the link, but I cannot post the picture
You need to use the getText() method to get the text the user entered.
Change the myEdit declaration to:
final EditText myEdit = ... ;
and then use:
Toast.makeText(MainActivity.this, myEdit.getText().toString(), ...
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 9 years ago.
Improve this question
I have a large file of about 10 MB, I want to search a specific string, and this specific string may be used a lot of times in 10 Mb text file. I need results where this specific string is used. I want to do search like Google. For example when i write a string then google comes with matching Patterns . Your suggestions will be appreciated.
file formate
he is going to school.
we should do best deeds.
we should work hard.
.
.
.
.
Always speak truth.
i have search edit field in my application.
user write "should" in search edit field.and press search button.
a list should be opened in which searched words come with it's complete line.
for example result should be
we should do best deeds.
we should work hard.
A simple way to search a file and get a match "with context" is to use grep. For example, to match every line with "hello", and print one line before and three lines after, you would do
grep -b1 -a3 'hello' myBigFile.txt
You can use grep -E to allow for a wide range of PCRE regex syntax.
Without more detail it would be hard to give you a better answer.
EDIT 2
Now that you have explained your problem more clearly, here is a possible approach:
InputStream fileIn;
BufferedReader bufRd;
String line, pattern;
pattern = "should"; // get the pattern from the user, do not hard code. Example only
fileIn = new FileInputStream("myBigTextfile.txt");
bufRd = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
while ((line = bufRd.readLine()) != null) {
if(line.contains(pattern)) {
System.out.println(line); // echo matching line to output
}
}
// Done with the file
br.close();
If you need to match with wildcards, then you might replace the line.contains with something that is a little more "hard core regex" - for example
matchPattern = Pattern.compile("/should.+not/");
(only need to do that once - after getting input, and before opening file) and change the condition to
if (matchPattern.matcher(line).find())
Note - code adapted from / inspired by https://stackoverflow.com/a/7413900/1967396 but not tested.
Note there are no for loops... maybe the boss will be happy now.
By the way - if you edit your original question with all the information you provided in the comments (both to this answer and to the original question) I think the question can be re-opened.
If you expect the user to do many searches it may be faster to read the entire file into memory once. But that's outside of the scope of your question, I think.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want to get user input of minutes/seconds for a timer
new CountDownTimer(30000, 1000) { }
The 30000 (30 second) value I want that from a user
t= (EditText)findViewById(R.id.time);
The value is stored in t
How do I achieve this?
new CountDownTimer(t, 1000) { }
This wont work...
You have to get the text from EditText from android convert it to long milliseconds.
t= (EditText)findViewById(R.id.time);
long userinput = Long.parseLong(t.getText().toString());
new CountDownTimer(userinput, 1000) { } ;
Though you have to do some input validation checking else you might get exception later on.
new CountDownTimer(t, 1000) { }
Here your t variable is EditText - its object and first parameter of CountDownTimer expects long so you need to get content of EditText and then parse it to long:
Long.parseLong(t.getText().toString());
Note: Also good practise is to validate input of User (input is not always correct you need to assume that User is "stupid" and can add some bullshit) if its correct number that can be parsed into long:
String input = t.getText().toString();
if (input.matches("\\d+")) {
// its valid number
Long.parseLong(t.getText().toString());
}
Or you can ensure it via XML when you'll specify inputType for EditText:
android:inputType="number"
t is EditText if you want get value in Long you must use:
Long.parseLong(t.getText().toString())
if you want any type else you must cast from t.getText.toString()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
in my application i have got 0.00 in edit text.next when i enter 2or3 or any digit its getting concat with 0.00 and getting 0.003.
if(item.equals("0.00")){
item=item+string;}
i changed the code.but this time too its getting an error
if(item.equals("0.00")){
int a=Integer.parseInt(item.toString());
int b=Integer.parseInt(string.toString());
int c=a+b;
item=String.valueOf(c);}
If you are using decimal places then you will want to use a data type that supports those, such as double. Try this:
double d = Double.parseDouble(item.toString());
NOTE: If both your variables are of string type, then you won't need to use toString()
You want O.OO but still using Integer. Try using FLOAT.
Also, FLOAT is NEVER a perfect value. So you might have .XX added.
You have an error, because "0.00" has decimal places, so it is parsed as a floating point number, not integer.
Parse it as a double:
String item = "0.00";
double result = Double.parseDouble(item) + 0.03; // result is 0.03