Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7972099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:46:42+00:00 2026-06-04T07:46:42+00:00

I’m writing a small code to convert one amount of currency to another. The

  • 0

I’m writing a small code to convert one amount of currency to another.
The app accepts two values through EditText (configured to accept decimal numbers)- 1) Home or away currency (If one is filled, the field of the other is left blank) 2) The exchange rate.

Here is the code:

package kk.currency;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class CurrencyActivity extends Activity {
    /** Called when the activity is first created. */

    EditText inpval1;
    EditText inpval2;
    EditText rate;

    int v1 = 0;
    int v2 = 0;
    int r = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void btnclick(View view)  {
        try {
            inpval1 = (EditText)findViewById(R.id.away);
            v1 = Integer.parseInt(inpval1.getText().toString());

            inpval2 = (EditText)findViewById(R.id.home);
            v2 = Integer.parseInt(inpval2.getText().toString());

            rate = (EditText)findViewById(R.id.rate);
            r = Integer.parseInt(rate.getText().toString()); 
        }
        catch (Exception e) {
            Toast.makeText(this, "Empty field", Toast.LENGTH_SHORT).show();
        }
        finally {
            v1 = v2 * r;

            EditText destination = (EditText)findViewById(R.id.away);
            destination.setText(Integer.toString(v1));
        }
    }   
}

The problem is that upon clicking the button associated with “btnclick” the field which is left blank (and where the answer is supposed to be displayed) displays 0, no matter what input. But when clicked again the correct answer is displayed.

And if I put any value in the field which is supposed to be left blank, the correct value is displayed in the first go.

What could possibly be the problem?!

On removing ‘finally’ and placing it’s content into ‘try’ I don’t even get any response upon clicking of the button.

Also, the LogCat is is completely empty during the execution of the app.

Here is the layout of the app as someone had requested.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dip"
    android:layout_marginLeft="5dip"
    android:text="@string/away"
    android:textStyle="bold"
    android:textSize="20dip"
    android:textColor="#4199ae" />

<EditText
    android:id="@+id/away"
    android:digits="0123456789."
    android:inputType="number"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:hint="@string/c1" />

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dip"
    android:layout_marginLeft="5dip"
    android:text="@string/conv"
    android:textStyle="bold"
    android:textSize="20dip"
    android:textColor="#4199ae" />

<EditText
    android:id="@+id/rate"
    android:digits="0123456789."
    android:inputType="number"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:hint="@string/rate" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dip"
    android:layout_marginLeft="5dip"
    android:text="@string/home"
    android:textStyle="bold"
    android:textSize="20dip"
    android:textColor="#4199ae" />

<EditText
    android:id="@+id/home"
    android:digits="0123456789."
    android:inputType="number"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:hint="@string/c2" />

<Button
    android:id="@+id/but"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dip"
    android:text="@string/buttxt"
    android:onClick="btnclick" />

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:46:43+00:00Added an answer on June 4, 2026 at 7:46 am

    My reply from Reddit:

    Your issue is because you’re not handling the various possible inputs a user could have.
    On the first go, on this line

    v1 = Integer.parseInt(inpval1.getText().toString());

    The value of

    inpval1.getText().toString()

    returns “”, which cannot be converted to an integer, therefore it is causing an exception. You should be seeing the “Empty field” Toast appearing.
    So after that, it gets to the finally step, but because v2 and r and still 0, then it puts 0 in v1, and puts 0 in the away EditText.

    Next time you run, it gets 0 from the Away EditText, and works properly.
    Do you know how to do debugging? If you’re using Eclipse, doubleclick the line you want to pause at, at the very left, in the grey/blue bar, this will put a little breakpoint icon, and the program will stop there.

    So, if you either remove the getting of the value of the EditText at the beginning, it should work. Also, you want to look at implementing some of the other suggestions here, because you are doing some redundant work by getting the pointers to the control views every time you click the button.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.