C# TO JAVA CODE CONVERSION HELP

have code in c# to make the equivalent code in java

Page 1 of 1

4 Replies - 997 Views - Last Post: 22 November 2010 - 07:25 AM Rate Topic: -----

#1 djronbxs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 18-November 09

C# TO JAVA CODE CONVERSION HELP

Posted 12 November 2010 - 12:54 AM

Hi,

I have the below code in C# to make a preorder traversal:

static void PrintTreeDF(Node node, int depth)
{
if (node != null)
{
Console.Write(new string(' ', depth));
Console.WriteLine(node.value);

PrintTree(node.LeftChild, depth + 1);
PrintTree(node.RightChild, depth + 1);
}
else
(
Console.Write(new string(' ', depth));
Console.WriteLine("-");
}
}


can you help me convert this code into Java ? actually the only problem I have is to convert into Java code the Console.Write(new string(' ', depth)); and Console.WriteLine(node.value);

thanks
ron

This post has been edited by djronbxs: 12 November 2010 - 12:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C# TO JAVA CODE CONVERSION HELP

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: C# TO JAVA CODE CONVERSION HELP

Posted 12 November 2010 - 01:01 AM

Uhm, System.out.print and System.out.println ?

Why don't you show us what you have and post the compiler/error messages.
Was This Post Helpful? 0
  • +
  • -

#3 djronbxs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 18-November 09

Re: C# TO JAVA CODE CONVERSION HELP

Posted 12 November 2010 - 01:13 AM

I can show you, its almost identical but the only problem I have is when I tried the
Console.Write(new string(' ', depth));
with
System.out.print(new string(' ', depth));
it is not supported because it will be underlined in red in netbeans.
Was This Post Helpful? 0
  • +
  • -

#4 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: C# TO JAVA CODE CONVERSION HELP

Posted 12 November 2010 - 01:28 AM

Well, yeah, of course. What is "new string(' ', depth)"? I assume that creates a string consisting of "depth" number of spaces " "? Well, try something like
"                    ".substr(0, depth)

if you know what the "maximum" length might be. Otherwise you are going to need a StringBuilder and a loop (probably in a method for easy use).
public String emptyString(int length) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < length; i++) sb.append(" ");
    return sb.toString();
}


Come on man, think a little bit, read the API docs for the classes in question (if you think there might be a one-to-one equivalent) and think some more when there isn't. And then experiment some. Netiher of these solutions is very hard to come up with
Was This Post Helpful? 0
  • +
  • -

#5 djronbxs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 18-November 09

Re: C# TO JAVA CODE CONVERSION HELP

Posted 22 November 2010 - 07:25 AM

ok thanks I will try these proposed examples.

thanks
ron
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1