Can anyone help me to do this

if a jTextField has text like this 1A2B89C0
array[0]=1;
array[1]=10;//A should convert to 10 ,B to 11,c to 12,........
array[2]=2;
array[3]=11;//B to 11
array[4]=8;
array[5]=9;
array[6]=12;//C to 12
array[7]=0;

actualy this part is needed for a program which is convert a number in a base to another base.The program witten is only capable to convert a number in this format(12346298).
and i cant insert a number which have a digit 10 and 10+.

Recommended Answers

All 8 Replies

What's wrong with the Integer class and its varying parseInt() methods?

What's wrong with the Integer class and its varying parseInt() methods?

Return value from parseInt() can't store in a integer type variable because it may include letters such as A,B.
Is threre is a method to read a character by chracter of a jTextField text.

I think masijade was talking about the method

Integer.parseInt(String s, int radix)

Check out the API for this static method which parses a String s to an Integer value in the base radix.

Thanks. I want to modify this code to,As a example when jTextFild number has text
1A2B(A represent 10, B 11, C 12, ........)
and jTextFild base1 has text 12 and jTextFild base2 has text 10 then the final output which is showed in a another jTextField should be 3203.
base1 has the base(radix) of the number and and base 2 has the base which the number needs to convert into.(I think calculations are correct in example.) Above example cant demonstrate in my code.
actualy i want to modify this code to, entere a number which have a number over 10.
I mean if the radix of the number is >10 that number may have a number 10 or over 10.
when this hapens code i have written is anable to deal with it.
I have used netBeans 5.5 to write this code.

public class convert1 extends javax.swing.JFrame {
    int b=10;
    
    /** Creates new form convert1 */
    public convert1() {
        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.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        number = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        base1 = new javax.swing.JTextField();
        base2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        result = new javax.swing.JLabel();
        result1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("entere a number");

        jLabel2.setText(" Convert a number of one  base to another base");

        jLabel3.setText("entere the base of the number");

        jLabel4.setText("entere the base which the number wants to convert");

        jLabel5.setText("Result");

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .addContainerGap()
                                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 137, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(number, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(57, 57, 57)
                                .add(jLabel2))
                            .add(layout.createSequentialGroup()
                                .addContainerGap()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                        .add(jLabel4)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 43, Short.MAX_VALUE)
                                        .add(base2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(layout.createSequentialGroup()
                                        .add(jLabel3)
                                        .add(48, 48, 48)
                                        .add(base1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 78, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                            .add(layout.createSequentialGroup()
                                .addContainerGap(328, Short.MAX_VALUE)
                                .add(jButton1)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(result, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(jLabel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(42, 42, 42)
                        .add(result1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 316, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .add(58, 58, 58)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel1)
                            .add(number, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(20, 20, 20)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel3)
                            .add(base1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(22, 22, 22)
                        .add(jLabel4))
                    .add(base2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(19, 19, 19)
                .add(jButton1)
                .add(25, 25, 25)
                .add(result, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(result1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(66, 66, 66))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  char array[]=new char[200];
  int n=0;
    char array1='A';
  for(;n<200;n++){
   array[n]=array1;
   array1++;
    
  }
        int tenTh[]=new int[1000];
        for(int i=0;i<100;i++){
    tenTh[i]=0;
        }
      int bas22[]=new int[100];
        for(int i=0;i<100;i++){
    bas22[i]=0;
}
long num=Integer.parseInt(number.getText().toString());
long bas1=Integer.parseInt(base1.getText().toString());
long bas2=Integer.parseInt(base2.getText().toString());
System.out.println("num "+num);
int g=0;
int a=10;
long ten=0;
long ten2=num;
int b=1;
if(bas1!=10){
    for(;;g++){
double ten1=((double)ten2)/10;

if(ten1==0&ten2==0)break;
ten1+=0.01;
 ten2=ten2/10;
ten1=ten1-(double)ten2;
ten1+=0.01;
System.out.println(ten1);
tenTh[g]=(int)(ten1*10);
System.out.println("tenTh "+tenTh[g]);
//System.out.println(b);
b++;
    }
    System.out.println("g "+g);
    g--;
    long p=1;
    p=bas1;
    for(;g>1;g--){
        
    for(int w=1;w<g;w++){
        p*=bas1;
     System.out.println("1.ten is "+ten);
     System.out.println("p is "+p);
        }
    ten+=tenTh[g]*p;
    //ten+=tenTh[g];
    System.out.println("ten is "+ten);
    p=bas1;
}
    ten+=tenTh[0];
    ten+=tenTh[1]*bas1;
}
else{
    ten=num;
}
int k=0;
System.out.println("result"+ten);
for(k=0;ten>=1;k++){
    bas22[k]=(int)(ten%bas2);
    ten/=bas2;
    }
String fin=" ";
//bas22[k]=1;

for(;k>=0;k--){
    if(bas22[k]>=10){
        n=bas22[k]-10;
        fin+=""+array[n];
        result1.setText(fin);
    }
    else{
        fin+=""+bas22[k]; 
   result1.setText(fin);
    }
   
}

    

 
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new convert1().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField base1;
    private javax.swing.JTextField base2;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField number;
    private javax.swing.JLabel result;
    private javax.swing.JLabel result1;
    // End of variables declaration//GEN-END:variables
    
}

Please use a few imports. Reading that code with all those explicit calls is painful, and annoying, and I, for one, am definately not going to "fight through it". Use some imports and then repost your code.

And, I will ask again, what is wrong with Integer and its parseInt(string, radix) method? And, to go along with that, Integer and its toString(int, radix) method.

Before you tell i didnt know about this method.
I have tried the parseInt(String,radix) method but it only convert a String which have 1s and 0s to the radix.If the string dont have 1s & 0s it returns a error message.
This code could run on netBeans without imports.
Try to run the code in netBeans 5.5 if you can
and you will get a idea about my problem.

Can you pleace understand my problem.

Integer.parseInt("10101", 2);

Does what you said.

Integer.parseInt("ABCDEF", 16);

Parses a hexidecimal number. The second number is the radix (as the argument says). If you use 2, then yes, its binary. If you use 16, then its hex. The radix is "base" you're talking about. Have you tried it in that manner? Seemingly not, or you would not have made that last post. Just because, in whatever example you saw, only 2 for binary was used, does not mean that that is the only "base" that the method understands.

Edit: It does not simply convert a binary String representation of an integer to a String representation of an integer in another base. That is not how it works. It takes whatever String you give it and parses that String into an Integer using the radix you give as the "base" for the String representation in the first argument.

thanks a lot masijade.Now i understand it.I will try this method.Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.