String to int conversion

Asked

Viewed 97 times

1

my xml `

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="0dp"
    tools:ignore="MissingConstraints">

</android.support.design.widget.AppBarLayout>


<android.support.v7.widget.CardView
    android:id="@+id/cardfejaum"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:layout_marginTop="20dp"
    android:elevation="50dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/appBarLayout">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <ImageView
        android:id="@+id/fejaum"
        android:layout_width="386dp"
        android:layout_height="200dp"
        android:background="@drawable/fejaum"
        android:padding="4dp"
        android:src="@color/transparencia"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="200dp" />




    <android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:textColorHint="#fff"
        tools:layout_editor_absoluteX="168dp"
        tools:layout_editor_absoluteY="205dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/edt"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:backgroundTint="#fff"
            android:hint="Comprado:"
            android:inputType="number" />

    </android.support.design.widget.TextInputLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text="No estoque"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Feijão"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff"
        android:textSize="32sp" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="110dp"
        android:layout_height="15dp"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="158dp"
        android:layout_weight="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:max="4"
        android:progressDrawable="@drawable/custom_seek_bar"
        android:progress="6"/>
    <TextView
        android:id="@+id/tvseek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="280dp"
        android:layout_marginTop="150dp"
        android:text="0"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="150dp"
        android:text="0"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ok"
        android:textColor="#fff"
        android:layout_marginTop="110dp"
        android:layout_marginLeft="200dp"
        android:background="@null"/>

</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:id="@+id/cardarroz"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:elevation="50dp"
    app:layout_constraintEnd_toEndOf="@id/cardfejaum"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@id/cardfejaum"
    app:layout_constraintTop_toBottomOf="@+id/cardfejaum">

    <ImageView
        android:id="@+id/arroz"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/arrozbranco"
        android:padding="4dp"
        android:src="@color/transparencia" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="130dp"
        android:textColorHint="#fff"
        tools:layout_editor_absoluteX="168dp"
        tools:layout_editor_absoluteY="205dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/edtarroz"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:backgroundTint="#fff"
            android:hint="Usado:"
            android:inputType="number" />

    </android.support.design.widget.TextInputLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text="Restante"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff" />

    <TextView
        android:id="@+id/tvarroz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="150dp"
        android:text="0"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Arroz"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff"
        android:textSize="32sp" />

</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:id="@+id/cardaçucar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="208dp"
    android:elevation="50dp"
    app:layout_constraintEnd_toEndOf="@id/cardfejaum"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@id/cardfejaum"
    app:layout_constraintTop_toBottomOf="@+id/cardfejaum">

    <ImageView
        android:id="@+id/açucar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/acucarrefinado"
        android:padding="4dp"
        android:src="@color/transparencia" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="130dp"
        android:textColorHint="#fff"
        tools:layout_editor_absoluteX="168dp"
        tools:layout_editor_absoluteY="205dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/edtaçucar"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:backgroundTint="#fff"
            android:hint="Usado:"
            android:inputType="number" />

    </android.support.design.widget.TextInputLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text="Restante"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff" />

    <TextView
        android:id="@+id/tvaçucar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="150dp"
        android:text="0"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Açucar Refinado"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#fff"
        android:textSize="32sp" />

</android.support.v7.widget.CardView>

My java code

`package org.lucas.aplicativo;

import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class Principal extends AppCompatActivity {
    CardView cardfejaum, cardarroz, cardaçucar;
    TextInputEditText edtarroz, edtaçucar, edt;
    TextView tv , tvarroz, tvaçucar, tvseek;
    SeekBar seekBar;
    Button btn;
    int progresso;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        cardfejaum = findViewById(R.id.cardfejaum);
        cardarroz = findViewById(R.id.cardarroz);
        cardaçucar = findViewById(R.id.cardaçucar);
        edt = findViewById(R.id.edt);
        edtarroz = findViewById(R.id.edtarroz);
        edtaçucar = findViewById(R.id.edtaçucar);
        tv = findViewById(R.id.tv);
        tvarroz = findViewById(R.id.tvarroz);
        tvaçucar = findViewById(R.id.tvaçucar);
        seekBar = findViewById(R.id.seekBar);
        tvseek = findViewById(R.id.tvseek);
        btn = findViewById(R.id.btn);
        seekBar.setProgress(0);
        seekBar.setMax(15);
        calcular();
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int prog, boolean b) {
                progresso = prog;
                tvseek.setText(" "+prog);
                calcular();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                tvseek.setText(" "+progresso);
                calcular();
            }
        });

        tv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        }

    public void calcular() {
    int sub = Integer.parseInt(edt.getText().toString());
    int sub2 = Integer.parseInt(tvseek.getText().toString());
    int calc = sub - sub2;
    tv.setText(String.valueOf(calc));
}
}`

2 answers

1

When you use Integer.parseInt may occur a NumberFormatException. How are you putting right the Integer.parseInt(tvseek.getText().toString()); this can give a problem if the text of the text field is empty or with a letter for example.

0

You can use 2 forms as needed:

Integer.parseInt(valorString): returns a Integer.

or

Integer.valueOf(valorString): returns a primitive (int)


As prmottajr said, you may have trouble with a String emptiness.

An example of how to treat:

if (valorString.length() > 0) 
    Integer.parseInt(valorString);

Useful links:

Integer

Difference between int and Integer in Java

Browser other questions tagged

You are not signed in. Login or sign up in order to post.