I\'m working on this GUI program, and it\'s supposed take as input a number, a b
ID: 3694980 • Letter: I
Question
I'm working on this GUI program, and it's supposed take as input a number, a base number and a new base number all the user's choice. Then the user press the calculate button and the programs supposed to output the new number in the new base number. I have the program written and it's doing what I need it to do except displaying the new number in the new base in the designated text field correctly. It's displaying the new number backwards and I can't figure out how to reverse it. Here's my CalculateButtonHandler class, where all the work is:
private class CalculateButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent ae){
//number holds the string in nTF
//newNum stores the new number
String number = "", newNum = "";
//num is the number
//I is the index(the length of the string number)
//n is a variable to store calculation
//base stores the string in bTF
//new base stores the string in nbTF
//sum is the variable that stores the calculations for the //number in 10 base.
int num = 0, n = 0, I = 0, base = 0, newBase = 0;
double sum = 0.00;
number = nTF.getText();
base = Integer.pareseInt(bTF.getText());
newBase = Integer.pareseInt(nbTF.getText());
I = number.length();
num = Integer.parseInt(number);
for(int I = 0; i < I; i++){
n = num % 10;
sum = sum + n * Math.pow(base, i);
num = num / 10;
}
for(int i = 0; I < I - 1; i++){
double temp = sum % newBase;
number = "" + (int)(temp);
newNum = newNum.concat(number);
sum = sum / newBase;
}
nnTF.setText(newNum);
}
}
This isn't an assignment, this is just me doing programs out my textbook from college for my own pleasure and studying purposes. Can someone please help me with this.
Explanation / Answer
Here in the code, i have done some changes, changes are in bold letters.
private class CalculateButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent ae){
//number holds the string in nTF
//newNum stores the new number
String number = "", newNum = "";
//num is the number
//I is the index(the length of the string number)
//n is a variable to store calculation
//base stores the string in bTF
//new base stores the string in nbTF
//sum is the variable that stores the calculations for the //number in 10 base.
int num = 0, n = 0, I = 0, base = 0, newBase = 0;
double sum = 0.00;
number = nTF.getText();
base = Integer.pareseInt(bTF.getText());
newBase = Integer.pareseInt(nbTF.getText());
I = number.length();
num = Integer.parseInt(number);
for(int I = 0; i < I; i++){
n = num % 10;
sum = sum + n * Math.pow(base, i);
num = num / 10;
}
for(int i = 0; I < I - 1; i++){
double temp = sum % newBase;
number = "" + (int)(temp);
newNum = String.copy(number);
sum = sum / newBase;
}
nnTF.Text = newNum;
}
}
The above two changes are required in this case.