1. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## 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:

Java 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.setBounds(new Rectangle(30, 221, 94, 14));
jBtnOK.setBounds(new Rectangle(30, 188, 77, 25));
jBtnOK.setText("Run");
}

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:
Java 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

2. ## Re: Recursion

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.

3. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## 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?
Java Code:
`return (c>'9')? c-'A'+10 : c-'0'; // A-Z = 10-36 : 0-9 = 0-9`
or isnt this what you mean?

4. ## Re: Recursion

a number like B5
What kind of number is that? What would be its int value?

5. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## 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?

6. ## 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?
B is int 66
Are you referring to the ASCII value of the char 'B'?

7. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## 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.

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?

8. ## Re: Recursion

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.

9. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## 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(??);

Java 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);

JButton berekenknop = new JButton("Bereken");
add( new JLabel()); // leeg vak
}

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;
}
}
}
}
}```

10. ## Re: Recursion

Do you get any errors? Please copy and paste here the full text of the error messages.

11. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## 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)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)

12. ## 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:
Java Code:
```TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
var = 2;
^```

13. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## 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

Java 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.

14. ## 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.

15. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## 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?

Java 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();

JButton berekenknop = new JButton("Bereken");
add( new JLabel()); // leeg vak
}

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);
}

}

}```
Java 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;
}
}```

16. ## Re: Recursion

convert those into Integers.
Look at the Integer class. It has a method that you could use for that.

17. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## Re: Recursion

you mean the char to int ? can i use that for a string to int?

18. ## Re: Recursion

Sorry, what method and class are you talking about?

19. Member
Join Date
Dec 2011
Posts
16
Rep Power
0

## Re: Recursion

i thought you meaned this method?

Java 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');
}```

20. ## Re: Recursion

I was answering this question:
i want to convert those into Integers
Do you want to write your own method to do that
or do you want to use a method from the java SE library?

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•