Calcul TVA en java

Résolu/Fermé
westerndigit Messages postés 134 Date d'inscription mercredi 16 février 2011 Statut Membre Dernière intervention 18 avril 2012 - 18 janv. 2012 à 09:42
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 18 janv. 2012 à 12:42
Bonjour,

J'ai un bouton sur lequel je clique et celui ci doit m'afficher son résultat comment faire java m'indique une erreur pour le signe "*"

je veux faire 100 * 1.196 = 119.60

Simple oui mais ça ne marche pas.

merci d'avance pour la réponse

voici un extrait de mon code :

private void HTbtnActionPerformed(java.awt.event.ActionEvent evt) {
String nombre = NombreTfd.getText();
String taux = TvaTfd.getText();
TotalLbl = NombreTfd*TvaTfd;





A voir également:

4 réponses

choubaka Messages postés 39375 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 14 avril 2024 2 100
18 janv. 2012 à 09:55
Bonjour

Tu fais des opération mathématiques sur des chaînes de caractères, tu dois d'abord les passer en int ou autres float
0
choubaka Messages postés 39375 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 14 avril 2024 2 100
18 janv. 2012 à 11:05
ce ne sont même pas des chaînes de caractères

NombreTfd et TvaTfd, ces deux élément ont tout l'air d'être des éléments GUI (d'affichage)
0
Pignic13 Messages postés 6 Date d'inscription mercredi 18 janvier 2012 Statut Membre Dernière intervention 26 avril 2012
18 janv. 2012 à 10:57
Quel est le type de NombreTfd et TvaTfd ?
Je te conseille de les mettre en float ou double.

(A noter que, suivant la norme Java, seule les noms de classes doivent commencer par des majuscules.)

Quand tu dit "Ça marche pas", tu veux dire quoi, "Ça compile pas" ou "Ça pète au runtime" ou "Ça me donne pas le bon résultat"... Dans tous les cas, fournir un message d'erreur serai utile pour t'aider.
A+
0
westerndigit Messages postés 134 Date d'inscription mercredi 16 février 2011 Statut Membre Dernière intervention 18 avril 2012
18 janv. 2012 à 11:41
voici le code complet


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* Main.java
*
* Created on 18 janv. 2012, 05:32:28
*/
package Index;

/**
*
* @author
*/
public class Main extends javax.swing.JFrame {

/** Creates new form Main */
public Main() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
NombreTfd = new javax.swing.JTextField();
TvaTfd = new javax.swing.JTextField();
HTbtn = new javax.swing.JButton();
TTCBtn = new javax.swing.JButton();
TotalLbl = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(0, 153, 153));

NombreTfd.setHorizontalAlignment(javax.swing.JTextField.CENTER);

HTbtn.setBackground(new java.awt.Color(0, 153, 255));
HTbtn.setText("HT");
HTbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
HTbtnActionPerformed(evt);
}
});

TTCBtn.setBackground(new java.awt.Color(0, 153, 255));
TTCBtn.setText("TTC");
TTCBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TTCBtnActionPerformed(evt);
}
});

TotalLbl.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
TotalLbl.setText("0,00");

jLabel3.setText("Entrer un nombre");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(149, Short.MAX_VALUE)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(TvaTfd, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TotalLbl, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(HTbtn)
.addGap(18, 18, 18)
.addComponent(TTCBtn))
.addComponent(NombreTfd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
.addGap(43, 43, 43))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(63, 63, 63)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(NombreTfd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(38, 38, 38)
.addComponent(TvaTfd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(HTbtn)
.addComponent(TTCBtn))
.addGap(37, 37, 37)
.addComponent(TotalLbl)
.addContainerGap(67, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

private void HTbtnActionPerformed(java.awt.event.ActionEvent evt) {
String nombre = NombreTfd.getText();
String taux = TvaTfd.getText();
TotalLbl = (NombreTfd*TvaTfd);

}

private void TTCBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton HTbtn;
private javax.swing.JTextField NombreTfd;
private javax.swing.JButton TTCBtn;
private javax.swing.JLabel TotalLbl;
private javax.swing.JTextField TvaTfd;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
0
choubaka Messages postés 39375 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 14 avril 2024 2 100
Modifié par choubaka le 18/01/2012 à 12:42
C'est pas au top, mais c'est en gros un truc du genre...

private void HTbtnActionPerformed(java.awt.event.ActionEvent evt) {
Float nombre = Float.valueOf(NombreTfd.getText());
Float taux = Float.valueOf(TvaTfd.getText());
float nombreValue = nombre.floatValue();
float tauxValue = taux.floatValue();
float result = nombreValue*tauxValue;
TotalLbl.setText("Calcul TVA:"+result);

il faut calculer avec des valeurs qui sont calculables ...

entre nous, le code généré automatiquement .. Beurk!

Chouba, Modérateur CCM
Bibapeloula
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
18 janv. 2012 à 12:42
Vu ton code, les remarques de Choubaka sur les types de tes objets sont évidentes, tu ne peux pas faire n'importe quelles opérations sur n'importe quel type d'objet !
Multiplier deux cases ça n'a aucun sens, multiplier les contenus des cases non plus, seul la multiplication des valeurs de ces cases a un sens. Mais le résultat n'est pas une case pour autant, et il faut faire les opérations inverses pour passer de la valeur résultat à la case.

Version courte :
TotalLbl.setText(String.valueOf(Double.parseDouble(NombreTfd.getText())*Double.parseDouble(TvaTfd.getText())));

Version longue :
String nombre = NombreTfd.getText();
Double nb = Double.parseDouble(nombre);

String taux = TvaTfd.getText();
Double tx = Double.parseDouble(taux);

Double pd = nb*tx;
String produit = String.valueOf(pd);

TotalLbl.setText(produit);
0