Search code examples
androidintgetstring

How to get a String value from an EditText Int?


Writing this:

note.setOraIn(txtOraIn.getText().toString());

I get this error:

error: incompatible types: String cannot be converted to int

note.setOraIn(txtOraIn.getText().toString());

Indeed txtOraIn is an Integer.

What is the correct syntax to show txtOraIn?

Alternatively, is it possible to perform mathematical operations with the values of the Strings? For example I need to get the result of txtOraFin - txtOraIn, which at the moment are Strings.

This is part of the Notes class:

@ColumnInfo(name = "note_content")
// column name will be "note_content" instead of "content" in table
private String data;
private String oraIn;
private String oraFin;
private String luogo;
private String km;
private String ore;

public Note(String data, String oraIn, String oraFin, String luogo, String km, String ore) {
    this.data = data;
    this.oraIn = oraIn;
    this.oraFin = oraFin;
    this.luogo = luogo;
    this.km = km;
    this.ore = ore;
}

@Ignore
public Note() {
}

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

public long getNote_id() {
    return note_id;
}

public void setNote_id(long note_id) {
    this.note_id = note_id;
}

public String getOraIn() {return oraIn;}

public void setOraIn(String oraIn) {this.oraIn = oraIn;}

public String getOraFin() {return oraFin;}

public void setOraFin(String oraFin) {this.oraFin = oraFin;}

public String getLuogo() {
    return luogo;
}

public void setLuogo(String luogo) {
    this.luogo = luogo;
}

public String getKm() {return km;}
public void setKm(String km) {this.km = km;}

public String getOre() {return ore;}
public void setOre(String ore) {this.ore = ore;}

Solution

  • Not sure if this is related to Corda in anyways as its tagged corda.

    However, I will try to answer this as it seems to be a generic Java question. To convert a String to an Integer you could use Integer.valueOf(<String>).

    So for the above, it becomes note.setOraIn(Integer.valueOf(txtOraIn.getText().toString())).

    Also, mathematic operations on String is not possible (in plain Java), unless you write some code to read the values and do the calculations in the background.