0

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;}
GSerg
  • 76,472
  • 17
  • 159
  • 346

1 Answers1

0

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.

Ashutosh Meher
  • 1,811
  • 2
  • 7
  • 16
  • Hi and thanks, after converting the strings to int in the syntax that you have sugered me, I receive these new errors: Process: com.example.pavneet_singh.roomdemo, PID: 4039 java.lang.NumberFormatException: For input string: "" at java.lang.Integer.parseInt(Integer.java:627) at java.lang.Integer.valueOf(Integer.java:801) etc. etc. –  May 18 '20 at 08:29
  • Is seems the string you are getting is blank so it cant be converted into a number. You could wrap this statement in a try-catch block and take appropriate action if the received string cannot be converted a number. – Ashutosh Meher May 18 '20 at 08:34
  • For a blank, you may want to initialize it to a default value, but you also need to think about other cases where the value is a random string which can't be converted to an integer. – Ashutosh Meher May 18 '20 at 08:35
  • Yes, the string was empty simply because I hadn't written anything, but now I get this, What is it about? java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.support.design.widget.TextInputEditText.getText()' on a null object reference One last thing, the try-catch block you were telling me where to write it? Always onClick? Thanks –  May 18 '20 at 08:42
  • Try catch is a general way to handle exceptions in Java. For best practices and design patterns that are more android specific, you should tag the question with `android` to get attention from the android community. I am not really familiar with android to suggest this. – Ashutosh Meher May 18 '20 at 08:50