Reputation: 31
I am still learning in making this calculator program, and much less in it i want to ask how to delete the last digit of a set of numbers
This is my current code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class frame {
int num1,num2,ans;
public static void main(String[] args) {
JFrame gui = new JFrame("Kalkulator");
final JTextField output = new JTextField(100);
JPanel panel = new JPanel();
JButton hapus = new JButton("<-");
JButton satu = new JButton("1");
JButton dua = new JButton("2");
JButton tiga = new JButton("3");
JButton empat = new JButton("4");
JButton lima = new JButton("5");
JButton enam = new JButton("6");
JButton tujuh = new JButton("7");
JButton delapan = new JButton("8");
JButton sembilan = new JButton("9");
JButton nol = new JButton("0");
JButton kali = new JButton("X");
JButton bagi = new JButton("/");
JButton kurang = new JButton("-");
JButton tambah = new JButton("+");
JButton samadengan = new JButton("=");
JButton titik = new JButton(".");
JButton clear = new JButton("C");
JLabel nama = new JLabel("Zulham azwar achmad (5302414040)");
nama.setHorizontalAlignment(SwingConstants.CENTER);
output.setBounds(10,11,290,61); //(jarak geser ke-kanan , jarak geser kebawah , panjang , tinggi )
clear.setBounds(250,121,50,50);
nama.setBounds(10,77,290,34);
hapus.setBounds(190,121,50,50);
satu.setBounds(10, 121, 50, 50);
dua.setBounds(70,121,50,50);
tiga.setBounds(130,121,50,50);
empat.setBounds(10,181,50,50);
lima.setBounds(70,181,50,50);
enam.setBounds(130,181,50,50);
tujuh.setBounds(10,241,50,50);
delapan.setBounds(70,241,50,50);
sembilan.setBounds(130,241,50,50);
nol.setBounds(10,301,110,50);
titik.setBounds(130,301,50,50);
kali.setBounds(190,181,50,50);
bagi.setBounds(190,241,50,50);
kurang.setBounds(250,181,50,50);
tambah.setBounds(250,241,50,50);
samadengan.setBounds(190,301,110,50);
panel.setLayout(null);
panel.add(output);
panel.add(hapus);
panel.add(satu);
panel.add(dua);
panel.add(tiga);
panel.add(empat);
panel.add(lima);
panel.add(enam);
panel.add(tujuh);
panel.add(delapan);
panel.add(sembilan);
panel.add(nol);
panel.add(titik);
panel.add(kali);
panel.add(bagi);
panel.add(tambah);
panel.add(kurang);
panel.add(samadengan);
panel.add(clear);
panel.add(nama);
gui.getContentPane().add(panel);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(317,390);
gui.setVisible(true);
gui.setResizable(false);
satu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"1");
}
});
dua.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"2");
}
});
tiga.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"3");
}
});
empat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"4");
}
});
lima.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"5");
}
});
enam.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"6");
}
});
tujuh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"7");
}
});
delapan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"8");
}
});
sembilan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"9");
}
});
nol.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(output.getText()+"0");
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(null);
}
});
hapus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setText(); <-- how to backspace ?
}
});
}
}
Upvotes: 0
Views: 409
Reputation: 9581
According to Docs Java Documentation
*Chars beginning at start
*str.substring(start, end)
*Up to but not including end
There is a more complex version of substring() that takes both start and end index numbers: substring(int start, int end) returns a string of the chars beginning at the start index number and running up to but not including the end index.
Examples:
String str = "Hello";
String a = str.substring(2, 4); // a is "ll" (not "llo")
String b = str.substring(0, 3); // b is "Hel"
String c = str.substring(4, 5); // c is "o" -- the last char
Take a look at ideone code live
hapus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = output.getText();
output.setText(0,text.length()-2));//will return all string but the last one
}
});
Upvotes: 0
Reputation: 4207
You can do something likewise,
hapus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(output.getText().length()>0){
output.setText(output.getText().substring(0,output.getText().length()-1));
}
}
});
Upvotes: 0
Reputation: 8387
You can use substring
method like this :
String strwithoutLastDigit = someString.substring(0,someString.length()-1)
Upvotes: 0
Reputation: 393811
You can use substring
:
hapus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = output.getText();
output.setText(text.substring(0,text.length()-1));
}
});
Upvotes: 2