Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What causes error "'void' type not allowed here" and how do I fix it?

I found error at line LN.Totalbayar= Integer.parseInt(txtTotalBayar.setText(sum+""));
Previously I’ve tried converting from string to integer. please help

private void btnProsesActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:        
     ClassProgramTiket LN = new ClassProgramTiket();
     String hrTiket = txtHargaTiket.getText();
    String jmlTiket = txtJumlahTiket.getText();
    int harga = Integer.valueOf(hrTiket);
    int jumlah = Integer.valueOf(jmlTiket);
    int sum = harga * jumlah;    
    LN.Nama=txtNama.getText();
    LN.KodeKereta= (String)cbokodeKereta.getSelectedItem();
    LN.NamaKereta=(String)jenisKereta.getSelectedItem();
    LN.Jurusan= (String)jurusan.getSelectedItem();
    LN.JenisTiket= (String)jenisTiket.getSelectedItem();
    LN.HargaTiket= Integer.parseInt(txtHargaTiket.getText());
    LN.JumlahTiket= Integer.parseInt(txtJumlahTiket.getText());      
    LN.Totalbayar= Integer.parseInt(txtTotalBayar.setText(sum+"")); <- Error 'void' 
type not allowed here
    Tiket.addElement(LN);
     }                
}

//========== class ClassProgramTiket ====  
public class ClassProgramTiket {
public String Nama;
public String KodeKereta;
public String NamaKereta;
public String Jurusan;
public String JenisTiket;
public int HargaTiket;
public int JumlahTiket;
public int Totalbayar;

@Override
public String toString(){     
    return Nama +"-" + KodeKereta +"-" + NamaKereta + "-" + Jurusan +"-" + JenisTiket 
+ "-" + HargaTiket+"-" + JumlahTiket + "-" + Totalbayar;
}
 }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

LN.Totalbayar= Integer.parseInt(txtTotalBayar.setText(sum+""));

The return type of setText is void. Integer.parseInt expects a String. Setters don’t usually return anything. Their purpose is to change a value, not retrieve it.

I’m not sure why you are trying to change txtTotalBayar while also parsing its value, so it is hard for me to say what the right thing to do is, but perhaps you want something like:

txtTotalBayar.setText(sum+"");
LN.Totalbayar= Integer.parseInt(txtTotalBayar.getText());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading