I am trying to write a very simple Android app.
It takes in 2 floating point numbers, does a calculation,
When I run my app, it crashes.
However, if I enter integers instead of floating point numbers, my
program doesn't crash and does the correct calculation.
How to solve this problem? thx a lot
private Button.OnClickListener btnTranListener =new Button.OnClickListener(){
public void onClick(View v){
int amount=Integer.parseInt(editfc.getText().toString());
double $=1.24*(double)amount;
$ = (double) (((int)($ * 1000)) / 1000.0);
outelc124.setText("ELC(1.24)= " + $ + " /pc");
Replace Integer.parseInt() with Double.parseDouble() and you will be able to handle numbers with decimals or integers.
You can also catch NumberformatException to keep it from crashing if they enter other bogus input.
Related
I'm trying to restrict what my EditText takes as input. I want it to take a maximum of 21 million (20,999,999.99999999). Is it possible to achieve> if, then how do I go about it? I've tried using InputFilter with a pattern [0-9]{0,9}+((\.[0-9]{0,7})?)||(\.)?. Show me how to do it.
Well, I read Francesco's answer and your comment also. If you need to check the decimal point, you can try this below.
EditText someText = findViewById(R.id.someValueInputHi);
String paw = someText.getText().toString().replaceAll("\\s+", ""); //rem unusual spaces
Double someInput = Double.parseDouble(paw);
if (someInput < 5.3465726543d | someInput > 3.7834657836d) {
//Ok now
} else {
//Oops, no man
}
You only take the value between 5.3465726543 and 3.7834657836.
Why double?
As the name implies, a double has 2x the precision of float. In general, a double has 15 decimal digits of precision, while float has 7.
EDIT:
Francesco's answer is now deleted.
I'm using Alamkanak's Week View Library. It's great but I'm having trouble with a specific feature I want to add to my app. It needs to look like this:
But this calendar starts showing time at 01:00 and goes all the way to 00:00 the following day. I'm looking to only show hours that are important to me: from 09:00 to 20:00.
I've also searched into this a lot, even found the same topic here, but still no solution.
Anyway, any help would be greatly appreciated. Also, if you can recommend another library to use for showing such events (I didn't quite like Google Calendar), it can also be of high value. Thanks in advance guys.
It turned out to be quite simple actually, (In my bellow explanation I've gone for the 07:00 - 19:00 hours)
Firstly copy the WeekView class and create a new WeekView class in your project.enter link description here
Secondly, assuming you've got the in your XML, you need to change that to
to reflect the newly created class and that fact that we want to use that instead of the default one.
Then in your new WeekView class you're going to need
import com.alamkanak.weekview.DateTimeInterpreter;
import com.alamkanak.weekview.MonthLoader;
import com.alamkanak.weekview.WeekViewEvent;
import com.alamkanak.weekview.WeekViewLoader;
After that, in the private void initTextTimeWidth() method, you'll need to update the for loop to
for (int i = 0; i < 12; i++) {
Then in the private void drawTimeColumnAndAxes(Canvas canvas) method you need to alter couple of things:
for (int i = 0; i < 12; i++) {
...
String time = getDateTimeInterpreter().interpretTime(i+7); //This will set the correct display hour digits
In the very next method drawHeaderRowAndEvents, update:
mEffectiveMinHourHeight= Math.max(mMinHourHeight, (int) ((getHeight() - mHeaderTextHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom) / 12));
There's another for loop that needs updating
for (int hourNumber = 0; hourNumber < 12; hourNumber++) {
And lastly
float top = mHourHeight * 12 * mEventRects.get(i).top / 1440 + mCurrentOrigin.y + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical;
bottom = mHourHeight * 12 * bottom / 1440 + mCurrentOrigin.y + mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 - mEventMarginVertical;
Basically go trough all the 24s in the class and update those to whatever you like, in my case it's 12 as I'm going for a 12 hour time-span to show and add howevery many hours you want to, coresponding to your first starting hour in String time = getDateTimeInterpreter().interpretTime(i+7)
Here's a paste bin to visualise all the crap from above, Left is the original class, right mine
I'm using the same library, and I've tried to make something similar once. What I can suggest is set the goToHour(double); to your first event's start time, than scale the hour's height with something like this to match your last event's end time:
int height = weekView.getMeasuredHeight();
int textSize = weekView.getTextSize();
int padding = weekView.getHeaderRowPadding();
height = height - textSize - (2 * padding);
weekView.setHourHeight(height / 24);
Edit: I've used this code to see whole 0-24 on the weekview. I think tweaking this could get you what you want.
In my game, a body is randomly relocated on the screen after the user does something. However, if the object is relocated on top of another body, then both are pushed slightly (to make room!). I would like to check the location of the randomly generated coordinates first, so that the relocation only takes place if the position is free (within a certain diameter anyway).
Something like.. location.hasBody(). There surely must be a function for this that I haven't found. Thanks!
There is no way to query a world with a point and get the body, but what you can do is query the world with a small box:
// Make a small box.
b2AABB aabb;
b2Vec2 d;
d.Set(0.001f, 0.001f);
aabb.lowerBound = p - d;
aabb.upperBound = p + d;
// Query the world for overlapping shapes.
QueryCallback callback(p);
m_world->QueryAABB(&callback, aabb);
if (callback.m_fixture)
{
//it had found a fixture at that position
}
Solution originally posted here: Cocos2d-iphone forum
Not sure if box2d includes a 'clean' way to do it. I'd just manually iterate over all bodies in the world just before adding a new one, and manually check if their positions + radio/size overlap with the new body shape.
try
b2Vec2 vec = body->GetPosition(); // in meters
or
CGPoint pos = ccp(body->GetPosition().x * PTM_RATIO, body->GetPosition().y * PTM_RATIO); // in pixels
I am pretty new to all this Flash CS6 Action script 3.0 stuff and I was hoping to find out some different ways to apply a rare drop chance on an movie clip array for AS3. I have a random chance code that works pretty well for enemies, as they fall more often, however I'd like hearts to fall rarely for my player to catch and gain a life.
Here is the code I have so far, it drops far too many hearts. I've tried fiddling with the numbers but I only seem to make it worse. Any suggestions?
function makeHeart():void
{
var chance:Number = Math.floor(Math.random() * 60);
if (chance <= 1 + level)
{
var tempHeart:MovieClip;
tempHeart = new Heart();
tempHeart.speed = 3;
tempHeart.x = Math.round(Math.random() * 800);
tempHeart.cacheAsBitmapMatrix = tempHeart.transform.concatenatedMatrix;
tempHeart.cacheAsBitmap = true;
trace("tempHeart");
addChild(tempHeart);
hearts.push(tempHeart);
}
}
Well, this is either too simple of a question, or I just didn't understand it.
If I did however understand it correctly, here's the way out:
Let's say you want to have a 1% chance of a falling heart. Since you're using a Number class for your chance variable, and Math.random() returns a Number as well, you don't need any transformations.
Math.random() returns a Number (float) between 0 and 1, not including 1
so your code for 1% could look something like this:
var chance:Number = Math.random();
if (chance <= 0.01)
{
//enter code here
}
And yes, since you call less unneeded functions, it works faster.
Math.random() gives a very precise number, far beyond 1/100, so it's possible to make a much less number for chance possibility, here's one value returned from Math.random():
Math.random(); // 0.9044877095147967
Hi I am working on an android calculator apps and the now working on the manipuations. I have defined for the following:
ArrayList<Float> inputnum = new ArrayList<Float>();
float inputnum1;
float inputnum2;
and then for the operations,
case MULTIPLY:
inputnum1 = inputnum.get(0);
inputnum2 = inputnum.get(1);
inputnum.add(inputnum1 * inputnum2);
Display.setText(String.format("%.9f", inputnum.get(0)));
similar for the division one.
The muliply function and divide function works well for integers (eg 5* 4 output 20.00000000)
however, when it deals with figures with decimal places, eg 5.3 * 4, it output as 21.12000089, which is incorrect.
what is the problem?
also, how to set output to Display to remove unnecessary zero? eg
when 5*4 it only show 20 instead of 20.000000 as final answer?
when 5.3*4 = 21.12 instead of 21.12000000 as final answer?
Thanks a lot!
Just to change all the related float to double will then avoid presenting the rounding error.
If wanted to present 9 decimal places by filling up zero after the dot, eg 7.56 become 7.560000000, can use the below coding.
Display.setText(String.format("%.9f", inputnum.get(0)));