Adam
Adam

Reputation: 2570

Unknown Error - Integer Parsing

My ultimate goal for this app is to make a sort of "tip book" for waiters/waitresses so they can record their tips they make each day. The app will display amount made, hours worked and hourly amount. It will also have an option to review all previous work days, showing daily, weekly and monthly totals.

I am trying to take the numbers input into a two EditText boxes in the main.xml file, id etTip and etHour, display them in two TextView, tvTip and tvHour, then divide the number in etTip by etHour to get the wage, which would then be displayed in tvWage after pressing the Submit button, bSubmit.

The tips and hours display themselves easily enough, no errors found when all the app is doing that, but when I attempt to use the Integer.parseInt(String string) command, I get an error message. If the parsing is done during the onCreate method, the error is immediate after opening the app. If the parsing is done after pressing the button, the app force closes quickly after pressing the button.

I've located the problem, but I just don't know how to go about fixing it.

Below is the main xml file:

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal">

<EditText
    android:id="@+id/etTip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal"
    android:layout_weight="50"
    android:layout_margin="8dp"
    android:hint="Tips">

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/etHour"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal"
    android:layout_weight="50"
    android:layout_margin="8dp"
    android:hint="Hours" >

    <requestFocus />
</EditText>

</LinearLayout>

<Button
    android:id="@+id/bSubmit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="@string/btSubmit"
    android:textSize="35sp"/>

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal">

<TextView
    android:id="@+id/tvTip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:layout_weight="50"
    android:text="Tips"
    android:gravity="center"/>

 <TextView
    android:id="@+id/tvHour"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:layout_weight="50"
    android:text="Hours"
    android:gravity="center"/>"

 </LinearLayout>

<TextView
    android:id="@+id/tvWage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="Wage"
    android:gravity="center"/>
</LinearLayout>

And the main Activity:

package com.smarticle.tipbook;

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

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

TextView textTip,textHour,textWage;
EditText editHour,editTip;
int tip,wage,hour;

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

    textTip = (TextView) findViewById(R.id.tvTip);
    textHour = (TextView) findViewById(R.id.tvHour);
    textWage = (TextView) findViewById(R.id.tvWage);
    editTip = (EditText) findViewById(R.id.etTip);
    editHour = (EditText) findViewById(R.id.etHour);
    tip = Integer.parseInt(textTip.getText().toString());
    hour = Integer.parseInt(textHour.getText().toString());
    Button bSubmit = (Button) findViewById(R.id.bSubmit);
    bSubmit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            textTip.setText(editTip.getText());
            textHour.setText(editHour.getText());
            wage = tip / hour;
            textWage.setText(wage);
        }
    });
}
}

Upvotes: 0

Views: 153

Answers (2)

KaHeL
KaHeL

Reputation: 4371

It's just because you parsed to int your edit text content at load which is why the first value is null which is why it can't be parsed into integer. That is why I just placed the parse part inside the onClick for the content of the editview to be converted after clicking. Also change your edit view to accept only numeric so that it won't fail in case a user input an invalid characters. :) There you go.

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

textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);

//int tip =  Integer.parseInt((String) textTip.getText());
//int hour = Integer.parseInt((String) textHour.getText());

Button bSubmit = (Button) findViewById(R.id.bSubmit);
bSubmit.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        textTip.setText(editTip.getText());
        textHour.setText(editHour.getText());
        wage = Integer.parseInt(textTip.getText().toString()) / Integer.parseInt(textHour.getText().toString());

            textWage.setText(String.valueOf(wage));
        }
    });
}
}

Upvotes: 1

Luis
Luis

Reputation: 3571

Use double instead:

double tip,wage,hour;

And,

tip = parseDouble(textTip.getText().toString());
hour = parseDouble(textHour.getText().toString());

Also, you should check that the input in EditText is actually a number before parsing. And you should enclose the parsing in try/catch block.

Upvotes: 1

Related Questions