# Recursion

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-23-2011, 02:07 AM
KA5poker
Recursion
Hello i'm new to this forum, so i will introduce me a bit at first. My name is Kassandra and i live in the netherlands but im originally Danish. I learning to program in Java, I am now busy with programming with recursion. The goal is this: I want an applet with 4 boxes. in the first you put a number. in the second you put the Numeral system of the first number. and in the third you put the numeral system which you want to convert the number (you did put in the first box ) in. The calculation has to be on a recursive way. the applet what i made is this code:

Code:

```import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.JTextField; import java.awt.Rectangle; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.Font; public class Applet1 extends Applet {     public Applet1() {     }     public void init() {         try {             jbInit();         } catch (Exception e) {             e.printStackTrace();         }     }     private void jbInit() throws Exception {         jTFGetal.setBounds(new Rectangle(30, 50, 300, 19));         this.setLayout(null);         jTFTT1.setBounds(new Rectangle(30, 93, 35, 19));         jTFTT2.setBounds(new Rectangle(30, 136, 35, 19));         jTFUitvoer.setBounds(new Rectangle(30, 236, 300, 19));         jLbl.setText("the number you want to convert:");         jLbl.setBounds(new Rectangle(30, 34, 173, 14));         jLbl2.setText("numeral system of the number:");         jLbl2.setBounds(new Rectangle(30, 77, 176, 14));         jLlb3.setText("numeral system you want to change it in:");         jLlb3.setBounds(new Rectangle(30, 123, 230, 14));         jLabel1.setText("answer:");         jLabel1.setBounds(new Rectangle(30, 221, 94, 14));         jBtnOK.setBounds(new Rectangle(30, 188, 77, 25));         jBtnOK.setText("Run");         jBtnOK.addActionListener(new Applet1_jBtnOK_actionAdapter(this));         this.add(jTFTT1);         this.add(jTFTT2);         this.add(jTFGetal);         this.add(jLbl);         this.add(jLbl2);         this.add(jLlb3);         this.add(jTFAnswer);         this.add(jLabel1);         this.add(jBtnOK);     }     JTextField jTFGetal = new JTextField();     JTextField jTFTT1 = new JTextField();     JTextField jTFTT2 = new JTextField();     JTextField jTFAnswer = new JTextField();     JLabel jLbl = new JLabel();     JLabel jLbl2 = new JLabel();     JLabel jLlb3 = new JLabel();     JLabel jLabel1 = new JLabel();     JButton jBtnOK = new JButton();```
But how do you convert the number in a recursive way?? to convert an int to char and backwards i have made this code:
Code:

```    private int charToInt(char c) {             if (c >'9') {                     c &= ~('A' ^ 'a');             }             return (c>'9')? c-'A'+10 : c-'0'; // A-Z = 10-36 : 0-9 = 0-9     }     private char intToChar(int i) {                                            if (i >= 0 && i <= 9)                     return (char) (i + '0');             if (i >= 10 && i <= 35)                     return (char) (i - 10 + 'A');             else                                                                                            return 0;     }```
I hope there are some people who can help me with my problem.

thanks alot.

greetings Kas
• 12-23-2011, 02:42 AM
Norm
Re: Recursion
Quote:

to convert an int to char
What is the valid range of values in the int variable? Can it be > 9? If so, you will need an array of char to hold the converted value.

What are the valid char values that you want to convert to an int? '0' to '9'?
Since char values can be treated as numeric types, you can subtract '0' from the char to get the int value.
• 12-23-2011, 01:42 PM
KA5poker
Re: Recursion
the range is bigger then 0 - 9. its possible to put a number like B5 in it.

I do substract the 0 from it right?
Code:

`return (c>'9')? c-'A'+10 : c-'0'; // A-Z = 10-36 : 0-9 = 0-9`
or isnt this what you mean?
• 12-23-2011, 02:19 PM
Norm
Re: Recursion
Quote:

a number like B5
What kind of number is that? What would be its int value?
• 12-23-2011, 03:23 PM
KA5poker
Re: Recursion
B5 with numeral system 16( hex) should give.. 181 ( if you want numeralsystem 10 ( decimal) )

B is int 66?? or am i wrong?
• 12-23-2011, 03:29 PM
Norm
Re: Recursion
You need to define what the input is going to be first.
For example: what base(s) for the input: decimal, binary, hexadecimal, octal, etc
And is there a limit on the number of characters that can be input?
Quote:

B is int 66
Are you referring to the ASCII value of the char 'B'?
• 12-23-2011, 03:55 PM
KA5poker
Re: Recursion
yes but thats what it has to do. put the numeral system in the box that defines if it is a decimal, binary etc. but i dont know how i make that.

Quote:

Are you referring to the ASCII value of the char 'B'?
yes, isn't that what the int is? the ascii value of the character?
• 12-23-2011, 05:15 PM
Norm
Re: Recursion
Quote:

isn't that what the int is? the ascii value of the character?
It all depends on what you are doing.
If the String is supposed to contain hexadecimal digits, then 'b' or 'B' is 11.
• 01-03-2012, 02:48 PM
KA5poker
Re: Recursion
okay i've changed my program and made a recursive way to calculate the answer. But now i cant get the answer on my panel
what do i have to put in at line: 46. GetalUit.setText(??);

Code:

```package week4; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Invoerpaneel extends JPanel {         private JTextField Stelsel1, Stelsel2, GetalIn, GetalUit;         public Invoerpaneel(){                 setLayout( new GridLayout(5, 2));                 Stelsel1 = new JTextField (10);                 Stelsel2 = new JTextField (10);                 GetalIn = new JTextField (10);                 GetalUit = new JTextField (10);                 GetalUit.setEditable(false);                 GetalUit.setBackground( Color.green);                                 add(new JLabel("Stelsel invoer"));                 add(Stelsel1);                                 add(new JLabel("Stelsel uitvoer"));                 add(Stelsel2);                                 add(new JLabel("Getal"));                 add(GetalIn);                                 add(new JLabel("Uitkomst"));                 add(GetalUit);                                 JButton berekenknop = new JButton("Bereken");                 berekenknop.addActionListener(new KnopHandler());                 add( new JLabel()); // leeg vak                 add(berekenknop);         }                 private class KnopHandler implements ActionListener{                 public void actionPerformed(ActionEvent e) {                         GetalUit.setText(uitkomst);                 class Omrekening {                         public int recuForw(int inInt, String inStr) {                                                         int length = inStr.length();                                 char c = inStr.charAt(0);                                 int temp = charToInt(c);                                                                 if (length > 1)                                         return (temp*(int) Math.pow(inInt, (length-1)))+recuForw (inInt, inStr.substring(1, length));                                 else return temp;                         }                         public String recuBackw(int invoer, int getalstlsl){                                 int uitkomst = invoer % getalstlsl;                                 invoer /= getalstlsl;                                 if (invoer != 0)                                                                                 return recuBackw(invoer, getalstlsl) + intToChar(uitkomst);                                 else                                         return Character.toString(intToChar(uitkomst));                         }                         private int charToInt(char c) {  // omrekening van de de character naar een Int                                 if (c >'9'){  // als c is groter dan 9                                         c &= ~('A'^'a');  // maakt alles kleine letter                                 }                                         if (c>'9')                                                 return (c-'A'+10);                                         else                                                 return (c-'0');                         }                         private char intToChar(int i){                                                                                if (i >= 0 && i <= 9)                                         return (char) (i+'0');                                 if (i >= 10 && i <= 35)                                         return (char) (i-10+'A');                                 else                                                                                                                return 0;                         }                 }                 }         } }```
• 01-03-2012, 02:51 PM
Norm
Re: Recursion
Do you get any errors? Please copy and paste here the full text of the error messages.
• 01-03-2012, 04:40 PM
KA5poker
Re: Recursion
o sorry , When i run i dont get an error. but when i push the button to generate the answer i got this:

Exception in thread "AWT-EventQueue-1" java.lang.Error: Unresolved compilation problems:
Syntax error on token "int", delete this token
i cannot be resolved to a variable
Syntax error on token "char", delete this token
c cannot be resolved to a variable

at week4.Invoerpaneel\$KnopHandler.actionPerformed(Inv oerpaneel.java:46)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton\$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access\$000(Unknown Source)
at java.awt.EventQueue\$1.run(Unknown Source)
at java.awt.EventQueue\$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext\$1.doIntersectio nPrivilege(Unknown Source)
at java.security.AccessControlContext\$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue\$2.run(Unknown Source)
at java.awt.EventQueue\$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext\$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
• 01-03-2012, 04:44 PM
Norm
Re: Recursion
Can you compile the program and get the errors from the compiler. Your IDE(???) is hiding the compilation errors.
The compiler will show the source line with the error and place a ^ underneath the error.
Here is a sample:
Code:

```TestSorts.java:138: cannot find symbol symbol  : variable var location: class TestSorts         var = 2;         ^```
• 01-03-2012, 05:21 PM
KA5poker
Re: Recursion
this error is in the compiler:

The method setText(String) in the type JTextComponent is not applicable for the arguments () Invoerpaneel.java /Recursie/src/week4 line 46 Java Problem

Code:

```package week4; import javax.swing.JApplet; public class Display extends JApplet{         public void init(){                 setContentPane( new Invoerpaneel());         } }```
btw. this part is also a class who has to be in the same project.
• 01-03-2012, 05:29 PM
Norm
Re: Recursion
I'm not sure I understand your post.
Where is the source statement where the error occurred?
The error message mentions the setText() method, but I do not see that method used in the code you just posted.

The message could be saying that you did NOT give the setText() method a String argument, which is required.
• 01-12-2012, 01:52 PM
KA5poker
Re: Recursion
I seperated the two classes. one with the panel ( invoerpaneel) and the calculation ( bereken) . Now I have another problem.
I uses JTextfield to get the input. but this is a string?? i want to convert those into Integers. whats the best way to do that?

Code:

```package week4; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Invoerpaneel extends JPanel {         private JTextField Stelsel1, Stelsel2, GetalIn, GetalUit;         Bereken recCal;         public Invoerpaneel(){                 setLayout( new GridLayout(5, 2));                 Stelsel1 = new JTextField (10);         //        int stlsl1 = Integer.parseInt(Stelsel1); // Dont work                                 Stelsel2 = new JTextField (10);                 GetalIn = new JTextField (10);                 GetalUit = new JTextField (10);                 GetalUit.setEditable(false);                 GetalUit.setBackground( Color.green);                 recCal = new Bereken();                                 add(new JLabel("Stelsel invoer"));                 add(Stelsel1);                                 add(new JLabel("Stelsel uitvoer"));                 add(Stelsel2);                                 add(new JLabel("Getal"));                 add(GetalIn);                                 add(new JLabel("Uitkomst"));                 add(GetalUit);                                 JButton berekenknop = new JButton("Bereken");                 berekenknop.addActionListener(new KnopHandler());                 add( new JLabel()); // leeg vak                 add(berekenknop);                 }                 public class KnopHandler implements ActionListener {                 public void actionPerformed(ActionEvent e) {         //                        int stlsl1 = Stelsel1.getInt();                                 int stlsl2 = Stelsel2.getInt();                                 int getalIn = GetalIn.getInt();                                 GetalUit.clear();                                 int GetalInInt = recCal.recuForw(stlsl1, getalIn);                                 String uitkomst = recCal.recuBackw(GetalInInt, stlsl2);                                 GetalUit.setText(uitkomst);                         }                                }         }```
Code:

```package week4; public class Bereken {         public int recuForw(int inInt, String inStr) {                                int length = inStr.length();                 char c = inStr.charAt(0);                 int temp = charToInt(c);                                 if (length > 1)                         return (temp*(int) Math.pow(inInt, (length-1)))+recuForw(inInt, inStr.substring(1, length));                 else return temp;         }         public String recuBackw(int GetalIn, int tlstlsl){                 int uitkomst = GetalIn % tlstlsl;                 GetalIn /= tlstlsl;                 if (GetalIn != 0)                                                 return recuBackw(GetalIn, tlstlsl) + intToChar(uitkomst);                 else                         return Character.toString(intToChar(uitkomst));         }         private int charToInt(char c) {  // omrekening van de de character naar een Int                 if (c >'9'){  // als c is groter dan 9                         c &= ~('A'^'a');  // maakt alles kleine letter                 }                         if (c>'9')                                 return (c-'A'+10);                         else                                 return (c-'0');         }         private char intToChar(int i){                // int to char                                 if (i >= 0 && i <= 9)                         return (char) (i+'0');                 if (i >= 10 && i <= 35)                         return (char) (i-10+'A');                 else                                                                                                return 0;         } }```
• 01-12-2012, 01:57 PM
Norm
Re: Recursion
Quote:

convert those into Integers.
Look at the Integer class. It has a method that you could use for that.
• 01-12-2012, 02:05 PM
KA5poker
Re: Recursion
you mean the char to int ? can i use that for a string to int?
• 01-12-2012, 02:17 PM
Norm
Re: Recursion
Sorry, what method and class are you talking about?
• 01-12-2012, 02:23 PM
KA5poker
Re: Recursion
i thought you meaned this method?

Code:

```  private int charToInt(char c) {  // omrekening van de de character naar een Int         if (c >'9'){  // als c is groter dan 9             c &= ~('A'^'a');  // maakt alles kleine letter         }             if (c>'9')                 return (c-'A'+10);             else                 return (c-'0');     }```
• 01-12-2012, 02:26 PM
Norm
Re: Recursion