Help me tweak this method

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 803 Views - Last Post: 03 March 2013 - 06:37 PM Rate Topic: -----

#1 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Help me tweak this method

Posted 26 February 2013 - 12:34 AM

public static void evenDigits(int n) {
if (n!=0){
    if(n%2 == 0){
        evenDigits(n/10);
		  System.out.print(n%10);
    } else{
       evenDigits(n/10);
    } 
}

}




For example:

evenDigits(8342116);

would return 8426



Ok, so the method recursively removes all odd digits from an integer. Currently it prints the correct output. However, instead of printing the output, I want the method to return the integer.

What is the best way to go about this. I tried using string builder to append the n%10 to a string and then parse the SB to an INT but this also placed line breaks where the odd numbers were removed.

Is This A Good Question/Topic? 0
  • +

Replies To: Help me tweak this method

#2 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 285
  • Joined: 22-August 11

Re: Help me tweak this method

Posted 26 February 2013 - 06:50 AM

View Postcglidden, on 26 February 2013 - 02:34 AM, said:

I want the method to return the integer.


If you want to return the value to another method you can't use the VOID keyword and somewhere you have to tell it to RETURN your value.
So maybe:
public int evenDigits(int n) {
if (n!=0){
    if(n%2 == 0){
        evenDigits(n/10);
	return(n%10);
        } 
    else{
       evenDigits(n/10);
        } 
  }
}


This post has been edited by torind_2000: 26 February 2013 - 06:51 AM

Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Help me tweak this method

Posted 26 February 2013 - 06:57 AM

A 'return' has to be at the end of all possible logic paths.
Was This Post Helpful? 1
  • +
  • -

#4 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 26 February 2013 - 07:33 AM

Yes, I know it can't be void to return an int. That part is rather obvious and not really what I needed help with.

The problem with is I am not trying to return just one int at a time.

I want to remove the odd digits from the integer leaving only the even digits.

So if n = 8342116 before the call, n would = 8426 after the call.

Not 8 then 4 then 2 then 6.

I have tried this a couple different ways. I have tried adding n%10 to a list if its even, chopping off the composite by using /10 and then repeating all the way through the number, then loop or iterate through the list to form a string and then parse the string to an int and return that int. The problem with this is that the recursion also adds blank lines to the list.

I've also tried it with concatenation and string builder but get the same effect. It adds a blank line when the odd composite is chopped off which then gives me an error when trying to parse the string to an int.. for example:

public static int evenDigits(int n) {
String number = "";
if (n!=0){
    if(n%2 == 0){
        evenDigits(n/10);
		  number = number + n%10 + "";
    } else{
       evenDigits(n/10);
    }
}
System.out.println(number);
int even = Integer.parseInt(number);
return even;
}


Was This Post Helpful? 0
  • +
  • -

#5 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 26 February 2013 - 07:38 AM

public static void evenDigits(int n) {
String number = "";
if (n!=0){
    if(n%2 == 0){
        evenDigits(n/10);
		  number = number + n%10 + "";
    } else{
       evenDigits(n/10);
    }
}
System.out.println(number);

}



output:
8

4
2


6



My real question is -- how do I get it to not add the blank lines to the string? Oh and before any clever person mentions it: changing println to print is not a solution either. I'm not trying to print in the end - this is just a test to see what the string is ending up as. I want to parse the string into an int and can't do it with blank lines.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Help me tweak this method

Posted 26 February 2013 - 07:52 AM

Try moving the line:

String number = "";

outside the method to be a class variable.
Was This Post Helpful? 1
  • +
  • -

#7 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: Help me tweak this method

Posted 26 February 2013 - 08:12 AM

I think this is what you are looking for:

public static int evenDigits(int n, String s) {
	if(n==0) return Integer.parseInt(s);
	else if(n%2==0) return evenDigits(/*cut n, append correct number to s*/);
	else return evenDigits(/*cut n, do nothing to s*/);
}



Recursion can be quite hard to grasp at first, let me know if you have any questions :)

This post has been edited by oha055: 26 February 2013 - 08:19 AM

Was This Post Helpful? 1
  • +
  • -

#8 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 26 February 2013 - 08:14 AM

You were right in that the String = "" was causing problems, however, now I am beginning to think my logic is off.

There should be a way to do this recursively all within one method, and no global classes.
Was This Post Helpful? 0
  • +
  • -

#9 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 26 February 2013 - 08:19 AM

View Postoha055, on 26 February 2013 - 08:12 AM, said:

Recursion can be quite hard to grasp at first, let me know if you have any questions :)/>/>


Indeed it is. There must be some way to do this with only accepting and int as a parameter?
Was This Post Helpful? 0
  • +
  • -

#10 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: Help me tweak this method

Posted 26 February 2013 - 08:33 AM

Yes, there is :)

public static String evenDigits(int n) {
	if(n == 0) return "";
	else if(n%2 == 0) return /* drop valid number */ + evenDigits(n/10);
	else return evenDigits(n/10);
}



Don't forget to vote up posts that may have helped you! :)

This post has been edited by oha055: 26 February 2013 - 08:35 AM

Was This Post Helpful? 1
  • +
  • -

#11 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 26 February 2013 - 09:55 AM

Hmmm, this is similar to the logic I was thinking. The problem with this is that it returns a String and I want it to a return an int
Was This Post Helpful? 0
  • +
  • -

#12 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: Help me tweak this method

Posted 26 February 2013 - 10:19 AM

Yes, this was the easiest way to do it, considering the assignment was to return a equence of even integers and not the sum of all the even integers.

you could just do this though:

int evenSeq = Integer.parseInt(evenDigits());


This post has been edited by oha055: 26 February 2013 - 10:20 AM

Was This Post Helpful? 1
  • +
  • -

#13 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 26 February 2013 - 11:51 AM

That's what makes this question so tricky and why I am needing some help lol.

The call signature must be:
evenDigits(81283123);
evenDigits(324);

etc

The parameter must be only an integer and it must return only an INTEGER that has no odd digits.

It has me rather stumped.
Was This Post Helpful? 0
  • +
  • -

#14 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 26 February 2013 - 05:48 PM

Well, I am glad no one has a simple answer to this because then I would just feel dumb. I guess it is tricky for more than just me?
Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Help me tweak this method

Posted 27 February 2013 - 05:59 AM

View Postoha055, on 26 February 2013 - 12:19 PM, said:

Yes, this was the easiest way to do it


Um, no, it's kind of a hack. It's almost certainly not what an instructor would be looking for. It's about problem solving, not method calling.


Here are the first two lines:
int evenDigits(int n) {
if(n == 0) { return 0; }
int digit = n % 10;
[/code]

You have to ask yourself why I pulled the digit out. The return statement must include a digit or not. It must also have the collection of all prior values.

Rather than just cough up the code, let's have a look at how recursion works:
-call evenDigits(8342116)
--digit = 6
--call evenDigits(834211)
---digit = 1
---call evenDigits(83421)
----digit = 1
----call evenDigits(8342)
-----digit = 2
-----call evenDigits(834)
------digit = 4
------call evenDigits(83)
-------digit = 3
-------call evenDigits(8)
--------digit = 8
--------call evenDigits(0)
--------return 0  // evenDigits(0)
-------return 8 // evenDigits(8)
------return 8 // evenDigits(83)
-----return 84 // evenDigits(834)
----return 842 // evenDigits(8342)
---return 842 // evenDigits(83421)
--return 842 // evenDigits(834211)
-return 8426 // evenDigits(8342116)



The rules appear to be that if you have a valid digit, you have to shift your number and add it. If not, just continue to pass back the current result. How would you do that?

Hope this helps.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2