Regular Expression in numbers and string length Android [closed] - android

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I need to point out the users that a number can only start from 9 and and its length is to be exactly equal to 10 digits. How do I manipulate this with a regex ? Please don't mind the question, I am relatively new.

To validate that the input is exactly 10 digits, starting with 9, use:
/^9\d{9}$/
The ^9 part matches a starting 9, {d}9 matches 9 digits

Here you are. A simple solution (not with regex):
String number = "12345";
...
if (number.length() != 10 || number.startsWith("9") == false)
{
Toast.makeText(getApplicationContext(), "Please check your input.", Toast.LENGTH_SHORT).show();
}
Hope this helps. Write if you have any problems.

Related

Why I am not getting the correct output for array.set? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Firstly, I am storing the data in ArrayList.Now based on my index(current position) I want to replace that data with new data,but what I am getting is
old data,old data,old data,true,true,new data,new data....
Any suggestions
ArrayList<String> arr = new ArrayList<String>();
if (arr.isEmpty()) {
for(int i = 0;i<=mcq.size();i++) {
arr.add(s);
}
} else {
arr.set(currentPosition, String.valueOf(arr.add(s)));
}
"s" is a String value I am getting from somewhere else.
This looks really weird:
arr.set(currentPosition, String.valueOf(arr.add(s)));
ArrayList.set changes the elements stored at currentPosition. You want it to change it to the string value of what arr.add returns.
ArrayList.add returns a boolean, so there you get your true values from.
I think you want to do arr.set(currentPosition, s);

How to get input from user for timer [closed]

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()

How to sort a string in Android [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I have a string like "RSEBAK" and I'd like to sort that string to "ABEKRS"
Is it possible in android?
If it is, then please guide me through.
Your spec is a bit fuzzy, but I think you mean to sort the charters in a string? If so…
String s1 = "RSEBAK";
char[] arrC = s1.toCharArray();
Arrays.sort(arrC);
String strSorted = new String(arrC);
strSorted should have "ABEKRS"

adding string as integer [closed]

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

How to convert Timezones in Double? [closed]

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
Can anyone tell me how to get Timezones in double, actually I get timezone like this way UTC+5.30. But I want only 5.30 in double.
try
TimeZone tz = TimeZone.getDefault();
String gmt = TimeZone.getTimeZone(tz.getID()).getDisplayName(false,
TimeZone.SHORT);
String z1 = gmt.substring(4);
String z = z1.replaceAll(":", ".");
double zo = Double.parseDouble(z);
Log.d("double time", "" + zo);

Categories

Resources