I need to replace spaces commas $ % typed in the jtextfield on my taxcalculator code. But at the same time I need to fix it because when I try to run it again it does not because the validation adds $ to the field. This a simple form of validation such that if the user enters “
I need to replace spaces commas $ % typed in the jtextfield on my taxcalculator code. But at the same time I need to fix it because when I try to run it again it does not because the validation adds $ to the field. This a simple form of validation such that if the user enters “$1, 499. 97” it will be converted to “1499.97”.
我需要在我的taxcalculator代码的jtextfield中替换空格逗号$%。但同时我需要修复它,因为当我尝试再次运行它时它不会,因为验证会向该字段添加$。这是一种简单的验证形式,如果用户输入“$ 1,499.97”,它将被转换为“1499.97”。
import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
public class TaxCalculator7F extends JApplet {
JLabel subTotalJLabel, taxRateJLabel, totalTaxJLabel, totalSaleJLabel;
JTextField subTotalJTextField, taxRateJTextField, totalTaxJTextField, totalSaleJTextField;
JButton calculateTaxJButton;
public void init() {
setLayout(null); // allows explicit positioning of GUI components
Container myContent = getContentPane();
subTotalJLabel = new JLabel();
subTotalJLabel.setText("Sub Total: ");
subTotalJLabel.setBounds(10, 0, 100, 25);
myContent.add(subTotalJLabel);
subTotalJTextField = new JTextField();
subTotalJTextField.setText("0.00");
subTotalJTextField.setHorizontalAlignment(JTextField.RIGHT);
subTotalJTextField.setBounds(90, 0, 80, 25);
myContent.add(subTotalJTextField);
taxRateJLabel = new JLabel();
taxRateJLabel.setText("Tax Rate (%): ");
taxRateJLabel.setBounds(10, 40, 100, 25);
myContent.add(taxRateJLabel);
taxRateJTextField = new JTextField();
taxRateJTextField.setText("7");
taxRateJTextField.setHorizontalAlignment(JTextField.RIGHT);
taxRateJTextField.setBounds(90, 40, 80, 25);
myContent.add(taxRateJTextField);
totalTaxJLabel = new JLabel();
totalTaxJLabel.setText("Tax Due: ");
totalTaxJLabel.setBounds(10, 80, 100, 25);
myContent.add(totalTaxJLabel);
totalTaxJTextField = new JTextField();
totalTaxJTextField.setEditable(false);
totalTaxJTextField.setHorizontalAlignment(JTextField.RIGHT);
totalTaxJTextField.setBounds(90, 80, 80, 25);
myContent.add(totalTaxJTextField);
totalSaleJLabel = new JLabel();
totalSaleJLabel.setText("Total Sale: ");
totalSaleJLabel.setBounds(10, 120, 100, 25);
myContent.add(totalSaleJLabel);
totalSaleJTextField = new JTextField();
totalSaleJTextField.setEditable(false);
totalSaleJTextField.setHorizontalAlignment(JTextField.RIGHT);
totalSaleJTextField.setBounds(90, 120, 80, 25);
myContent.add(totalSaleJTextField);
calculateTaxJButton = new JButton();
calculateTaxJButton.setText("Calculate Tax");
calculateTaxJButton.setBounds(20, 160, 140, 30);
myContent.add(calculateTaxJButton);
calculateTaxJButton.addActionListener(
event -> {
// define variables
double subTotal, taxRate, taxDue, totalSale;
// get sales sub total and tax rate
subTotal = Double.parseDouble(subTotalJTextField.getText());
//subTotalJTextField.getText();
//Code to clear from field $ % ,
//subTotalJTextField.setText(String.valueOf(subTotal).replaceAll("(?<=\\d),(?=\\d)|\\$", ""));
taxRate = Double.parseDouble(taxRateJTextField.getText());
taxDue = subTotal * taxRate / 100;
totalSale = subTotal + taxDue;
DecimalFormat dollars = new DecimalFormat("$0.00");
DecimalFormat percent = new DecimalFormat("0.0%");
// test strings
subTotalJTextField.setText(dollars.format(subTotal));
taxRateJTextField.setText(percent.format(taxRate / 100.00));
totalTaxJTextField.setText(dollars.format(taxDue));
totalSaleJTextField.setText(dollars.format(totalSale));
}
);
}
}
3 个解决方案
#1
1
well you could replace the $ sign
那么你可以取代$符号
subTotal = Double.parseDouble(subTotalJTextField.getText().replace ("$", ""));
and the tax rate would be
税率将是
taxRate = Double.parseDouble(taxRateJTextField.getText().replace ("%", ""));
or as @nandha points out
或者@nandha指出
String money = "$1,234,000.67";
try {
NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse(money);
System.out.println(number.doubleValue());
} catch (ParseException e) {
e.printStackTrace();
}
#2
1
String subValue = subTotalJTextField.getText();
try {
NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse(subValue);
subTotal = number.doubleValue();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
subTotal = 0;
}
Try this. Refer this for more info
试试这个。有关详细信息,请参阅此
#3
1
I think below code suits well to your problem
我认为下面的代码很适合你的问题
Number amount = null, rate = null;
try {
amount = NumberFormat.getCurrencyInstance().parse(subTotalJTextField.getText());
rate = NumberFormat.getNumberInstance().parse(taxRateJTextField.getText());
} catch (Exception e) {
e.printStackTrace();
amount = 0.0;
rate = 0.0;
}
subTotal = Double.parseDouble(String.valueOf(amount));
taxRate = Double.parseDouble(String.valueOf(rate));
, 499. 97” it will be converted to “1499.97”. I need to replace spaces commas $ % typed in th