adding string as integer [closed] - android

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

Related

Regular expression for song name in android [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 6 years ago.
Improve this question
In my application I want to display the song's name. Now it is showing the entire song path, like this
String song_name = /storage/Music/Lean on.mp3
but I want to display only the song's name, that means
String song_name = Lean on.mp3
Can any one help me with the regular expressions used for extracting the name of song from the file path?
You can use subString function with combination of LastInedexOf function.
please find below example
String path=":/storage/Music/Lean on.mp3";
String filename=path.substring(path.lastIndexOf("/")+1);

Convert a string into mathematical function of x :f(x)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Hello I'm a beginner in Android programming.
I'm working on a graphic calculator but i still have the problem of converting the function to math
For example :
y=cos(x^2)-ln(x)
should look like
y=Math.cos(x*x) - Math.log(x)
And than we plot it.
Thank You
I assume you want to convert a String to a float or double?
String input = "3.14";
float x = Float.parseFloat(input);
This code converts the String "3.14" into a float. You can use this float in the function you described in the question.
EDIT:
If you want to convert the full function String into Java code, you should limit the input to buttons. For example, add a button named "Cos", and remember in the code the user wants to use the Cosinus function. Then the user enters a value, and presses "Enter". The code knows it should use the cosinus function, and x is the value entered in an EditText. That value should be parsed using Float.parseFloat().

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

Regular Expression in numbers and string length 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 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.

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