I making a program where you can put in a persons name and the points he scores. Pretty simple. I'm trying to make a part of the program where it will make a sheet showing how much points someone is behind someone. Like if theres two people Bill and Mike and Bill has 330 points and Mike has 300. I want the program to do 330 - 300 which would equal 30 and the program would say: 2nd Place Mike with 300 points. 30 points behind Bill. But the way the program uses JButtons and you click the JButton and the JButton text becomes the points you typed in. So I was just going to subtract the JButton values but JButton contains Strings. This is what I have tried
public class event1 implements ActionListener {
public void actionPerformed(ActionEvent b){
String userText = userInput.getText();
buttons[1].setText(userText);
addPoint[1] = true;
System.out.println("addPoint[1] is " + addPoint[1]);
//This is how I'm trying to do...
if(addPoint[0] == true) {
String takingAway = buttons[0].getText();
String value = takingAway - userText;
//I've tried int instead of String but that just broke everything
}
}
}
So is their anyway I can take the JButton text and convert it into a int, like theres functions .toString() but I need in a int. Any advice?
You could do it like this:
Ideally you would first want to check if the String
contains digits only
final String s = 012345;
if(s.matches("[0-9]+")) {
System.out.println("String is a number");
} else {
System.out.println("String is not a number.");
}