Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Have a few odd problems.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Unhappy Have a few odd problems.

    /**
     * 
     */
     
    /**
     * @author pradcoc
     *
     */
     
    import java.io.*;
    import java.util.*;
     
    public class RecursionClassOne
    { // beginning of class
     
     
    public static void main(String[] args)
    { // beginning of main
      System.out.println("the first argument inputted is " + args[0]);
        int n = Integer.parseInt(args[0]);
    printLines(n);
    binary(n); 
    binary(n).toString();
    System.out.println(binary(n).toString());
    } // end of main
     
    public static void printLines(int n)
    { // beginning of method
     
    if (0 <= n <= 9)
    { // beginning of if
    System.out.println(n);
    return;
    } // end of if
     
    else
    { // beginning of else
     
    System.out.println(n%10);
    printLines(n/10);
     
    } // end of else
     
    } // end of method
     
    public static String binary(int n)
    { // beginning of method;
     
    	Integer n2 = n;
    String str = "";
    if (n2 == 0)
    { // beginning of if
    str.concat(n2.toString());
    return(str);
    } // end of if
     
    else if (n ==1);
    { // beginning of else if
    str.concat(n2.toString());
    return(str);
    } // end of else if
     
    else
    { // beginning of else
    str.concat((n2 %2).toString());
    binary(n/2);
     
    } // end of else
     
    } // end of method
     
    } // end of program

    First of all, I'm getting a silly message saying that for

    if (0<=n <=9)

    "The operator <= is undefined for the argument type(s) boolean, int"

    It doesn't like the else before str.concant((n2 % 2).toString());

    and it doesn't like str.concant((n2 % 2).toString()); either;

    Also, for both methods, the way it's printing or returning the values now is backwards of the order I want it in.

    For printLines(1234)

    It's returning, or will return:

    4
    3
    2
    1

    And for binary 26, it's returning, or will return

    01011

    But I want it to return

    11010

    How do I fix that?



  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Have a few odd problems.

    Check your syntax where all those compilation errors occur. For example,

    if (0<=n && n<=9)//is correct
    else if (n ==1);//I assume you do not want a semi-colon here.

  3. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (October 18th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    import java.io.*;
    import java.util.*;
     
    public class RecursionClassOne
    { // beginning of class
     
     
    public static void main(String[] args)
    { // beginning of main
      System.out.println("the first argument inputted is " + args[0]);
        int n = Integer.parseInt(args[0]);
    printLines(n);
    binary(n).toString();
    System.out.println(binary(n).toString());
     
    } // end of main
     
    public static void printLines(int n)
    { // beginning of method
     
    if (0 <= n && n<= 9)
    { // beginning of if
    System.out.println(n);
    return;
    } // end of if
     
    else
    { // beginning of else
     
    System.out.println(n%10);
    printLines(n/10);
     
    } // end of else
     
    } // end of method
     
    public static String binary(int n)
    { // beginning of method;
     
    	Integer n2 = n;
    String str = "";
    if (n2 == 0)
    { // beginning of if
    str.concat(n2.toString());
    return(str);
    } // end of if
     
    else if (n ==1)
    { // beginning of else if
    str.concat(n2.toString());
    return(str);
    } // end of else if
     
    else
    { // beginning of else
    	Integer n3 = n2%2;
    str.concat((n3).toString());
    binary(n/2);
     
    } // end of else
     
    } // end of method
     
    } // end of program

    Ok, it's saying for my last else in binary, that I have to return something, but I don't want it to return something there.

    Also, my stuff is still printing out backwards of the way I want it to.

  5. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Have a few odd problems.

    Quote Originally Posted by javapenguin View Post
    Ok, it's saying for my last else in binary, that I have to return something, but I don't want it to return something there.
    Well, you've defined the function to return a String so by definition you must. Return an empty string if you have to.

    Also, my stuff is still printing out backwards of the way I want it to.
    Recursive algorithms are quite dependent upon order of operation. If you have 'single branch recursion' (which these function are), calling the recursive function before any output will print things in reverse order compared to calling any output before calling the recursive function.

  6. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (October 18th, 2010)

  7. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    Quote Originally Posted by copeg View Post
    Well, you've defined the function to return a String so by definition you must. Return an empty string if you have to.



    Recursive algorithms are quite dependent upon order of operation. If you have 'single branch recursion' (which these function are), calling the recursive function before any output will print things in reverse order compared to calling any output before calling the recursive function.


    Why would I want it to return anything for the else part?

    public static String binary(int n)
    { // beginning of method;
     
    	Integer n2 = n;
    String str = "";
    if (n2 == 0)
    { // beginning of if
    str.concat(n2.toString());
    return(str);
    } // end of if
     
    else if (n ==1)
    { // beginning of else if
    str.concat(n2.toString());
    return(str);
    } // end of else if
     
    else
    { // beginning of else
    	Integer n3 = n2%2;
    str.concat((n3).toString());
    binary(n/2);
     
    } // end of else
     
    } // end of method

    returning ends the method.

    Also, I can't figure out how to reverse the output of the two methods.

    There is no String.reverse() method. I looked and am baffled as to how to do it.

    The closest thing to do would be to figure out the length of my String,

    and keep replacing index i with index length() - i - 1; till I hit the middle.

    However, that will replace all occurences, not just for those two characters only.

    Anyway, yeah I should be storing what it's returning the teacher told me, but still, how do I set that up?
    Last edited by javapenguin; October 18th, 2010 at 05:21 PM. Reason: You're right.

  8. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Have a few odd problems.

    Quote Originally Posted by javapenguin View Post
    Why would I want it to return anything for the else part?
    Because you defined it that way. If that is not what you want I suggest you redefine the method in terms you DO want (wouldn't you want to do something with the recursive returned strings from the binary function?) Recursion isn't an easy concept, so I suggest you seriously sit down and step through the process with some sort of input writing down the values and function class. Doing so will layout out how it truly works and perhaps answer many of your questions.

  9. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (October 18th, 2010)

  10. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    Quote Originally Posted by copeg View Post
    Because you defined it that way. If that is not what you want I suggest you redefine the method in terms you DO want (wouldn't you want to do something with the recursive returned strings from the binary function?) Recursion isn't an easy concept, so I suggest you seriously sit down and step through the process with some sort of input writing down the values and function class. Doing so will layout out how it truly works and perhaps answer many of your questions.
    Why isn't it recursively going to the base cases?

    public static String binary(int n)
    { // beginning of method;
     
    	Integer n2 = n;
    String str = "";
    if (n2 == 0)
    { // beginning of if
    str.concat(n2.toString());
    return(str);
    } // end of if
     
    else if (n ==1)
    { // beginning of else if
    str.concat(n2.toString());
    return(str);
    } // end of else if
     
    else
    { // beginning of else
    	Integer n3 = n2%2;
    str.concat((n3).toString());
    binary(n/2);
     
    } // end of else
     
    } // end of method

    Why isn't it, when I call binary(n/2), checking to see if n/2 is 0 or 1 and if not, keep checking till it is and then returning the String?

  11. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    I found the problem area. Why won't it concat anything to str?
    else
    { // beginning of else
    	Integer n3 = n2%2;
    str.concat((n3).toString()); // should concat n2%2
    System.out.println("Str is: " + str);
    binary(n/2);
     
     
    } // end of else

    What do I do?

    I need to get the else part to return something and also get the method to return the String with the correct binary value, no its value in reverse. But I'm not allowed, I think, to use the method toBinaryString(int n).

  12. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Have a few odd problems.

    Ok, quick recursion review.

    Look at my code below of an example of recursion. Review the comments to see if you understand how it works:
    public class TestRead 
    {
     
        public static void main(String[] args) 
        {
            //Test Cases
            System.out.println(recursiveMethod(1));
            System.out.println(recursiveMethod(2));
            System.out.println(recursiveMethod(3));
            System.out.println(recursiveMethod(4));
            System.out.println(recursiveMethod(5));
            System.out.println(recursiveMethod(6));
            System.out.println(recursiveMethod(7));
            System.out.println(recursiveMethod(8));
            System.out.println(recursiveMethod(9));
            System.out.println(recursiveMethod(10));
        }
        /* Recursive Method
         * Two important things regarding how we declare here: 
         * First is what we send this method
         * Second is what this method returns
         *
         * Those two things will be important to remember while we implement our method
         */
        public static int recursiveMethod(int n)
        {
            /* Base Case:
             * For this method, our Base Case (or, end case) is when n==1.
             * When this occurs, we want to return n (which will equal 1)
             */
            if(n==1)
                return n;
            /* If this is not the Base Case, we want to do our next step. For this method,
             * we want to multiply n with our call to recursiveMethod again. The very thing
             * that makes a recursive method so it that it will call itself.
             */
            else
                /* Now, there are two important things here. The first is the fact that we
                 * are sending this method a variable that is different from the variable
                 * that this method is currently running with. If you send it the same variable,
                 * it will do the same thing, and just loop forever. The second important thing
                 * is our return call. This says we want to return the value of n mulitplied by
                 * our next call this method. 
                 *
                 * Remember, this method will return an int, so when we call this method below,
                 * it will return an int which we can multiply by n. We will then return THAT
                 * value to wherever this method was called from. That could be at our main, or
                 * it could be at another instance of this method.
                 */
                return n*recursiveMethod(n-1);
        }
    }

    This program results in the following cases and results:
    1 - 1
    2 - 2
    3 - 6
    4 - 24
    5 - 120
    6 - 720
    7 - 5040
    8 - 40320
    9 - 362880
    10 - 3628800
    Lets have a look at the first 3 cases and trace them.
    CASE 1:
    Call: recursiveMethod(1)
    Hits Base Case, returns 1 to main
    CASE 2:
    Call: recursiveMethod(2)
    Passes Base Case, calls recursiveMethod(1)
    Hits Base Case, returns 1 to previous recursiveMethod call
    Returns (2*1) to main
    CASE3:
    Call: recursiveMethod(3)
    Passes Base Case, calls recursiveMethod(2)
    Passes Base Case, calls recursiveMethod(1)
    Hits Base Case, returns 1 to previous recursiveMethod call
    Returns (2*1) to previous recursiveMethod call
    Returns (3*2) to main


    Do you see how the further we start from the base case, the more exponential our calls to our recursive method becomes? That is the essence of recursion. Do you understand this much?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  13. The Following User Says Thank You to aussiemcgr For This Useful Post:

    javapenguin (October 18th, 2010)

  14. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    Quote Originally Posted by aussiemcgr View Post
    Ok, quick recursion review.

    Look at my code below of an example of recursion. Review the comments to see if you understand how it works:
    public class TestRead 
    {
     
        public static void main(String[] args) 
        {
            //Test Cases
            System.out.println(recursiveMethod(1));
            System.out.println(recursiveMethod(2));
            System.out.println(recursiveMethod(3));
            System.out.println(recursiveMethod(4));
            System.out.println(recursiveMethod(5));
            System.out.println(recursiveMethod(6));
            System.out.println(recursiveMethod(7));
            System.out.println(recursiveMethod(8));
            System.out.println(recursiveMethod(9));
            System.out.println(recursiveMethod(10));
        }
        /* Recursive Method
         * Two important things regarding how we declare here: 
         * First is what we send this method
         * Second is what this method returns
         *
         * Those two things will be important to remember while we implement our method
         */
        public static int recursiveMethod(int n)
        {
            /* Base Case:
             * For this method, our Base Case (or, end case) is when n==1.
             * When this occurs, we want to return n (which will equal 1)
             */
            if(n==1)
                return n;
            /* If this is not the Base Case, we want to do our next step. For this method,
             * we want to multiply n with our call to recursiveMethod again. The very thing
             * that makes a recursive method so it that it will call itself.
             */
            else
                /* Now, there are two important things here. The first is the fact that we
                 * are sending this method a variable that is different from the variable
                 * that this method is currently running with. If you send it the same variable,
                 * it will do the same thing, and just loop forever. The second important thing
                 * is our return call. This says we want to return the value of n mulitplied by
                 * our next call this method. 
                 *
                 * Remember, this method will return an int, so when we call this method below,
                 * it will return an int which we can multiply by n. We will then return THAT
                 * value to wherever this method was called from. That could be at our main, or
                 * it could be at another instance of this method.
                 */
                return n*recursiveMethod(n-1);
        }
    }

    This program results in the following cases and results:


    Lets have a look at the first 3 cases and trace them.
    CASE 1:
    Call: recursiveMethod(1)
    Hits Base Case, returns 1 to main
    CASE 2:
    Call: recursiveMethod(2)
    Passes Base Case, calls recursiveMethod(1)
    Hits Base Case, returns 1 to previous recursiveMethod call
    Returns (2*1) to main
    CASE3:
    Call: recursiveMethod(3)
    Passes Base Case, calls recursiveMethod(2)
    Passes Base Case, calls recursiveMethod(1)
    Hits Base Case, returns 1 to previous recursiveMethod call
    Returns (2*1) to previous recursiveMethod call
    Returns (3*2) to main


    Do you see how the further we start from the base case, the more exponential our calls to our recursive method becomes? That is the essence of recursion. Do you understand this much?
    Yes, my code for printLines(int n) used recursion too and it is now working perfectly. However, I don't know what to call recursively. I thought that....ok, now I'm starting to understand something.

    I want it to return something and pass that value to the method to concat.

    But how do I do that?

    Also, I think it's going to print the binary out backward.

  15. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Have a few odd problems.

    Well, you have two problems. The first is exactly what the compiler told you. You want to return something or there is no way to capture the value. So, you want this:
    else
    { // beginning of else
        Integer n3 = n2%2;
    str.concat((n3).toString()); // should concat n2%2
    System.out.println("Str is: " + str);
    return binary(n/2);
     
     
    } // end of else

    The second is that the String method concat(String) returns a String, but you are not capturing that String (I've made this mistake several times, do it once and never forget the reason again). So, you have two options, you can use the concat method, or you dont have to. You can concat String with the plus sign. Here are both ways:
    WITH CONCAT:
    str = str.concat((n3).toString());
    WITHOUT CONCAT:
    str = str+(n2%2);

    Since the mathematics are in parentheses and is not between quotes, it will be evaluated prior to concatenating to the Strings around it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  16. The Following User Says Thank You to aussiemcgr For This Useful Post:

    javapenguin (October 18th, 2010)

  17. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    Ok, I had to break the code into two separate programs because he made me.

    I noticed that it's working backwards and so returning nothing, i.e. .

    Something is wrong with me order. The only reason why i thought the second program worked yesterday is because I got it to print the binary value using toBinary(), which, although fun to use, is not allowed for this assignment.

    package Assignment5;
     
    /**
     * @author pradcoc
     *
     */
     
    import java.io.*;
    import java.util.*;
     
    import javax.swing.JOptionPane;
     
    public class RecursionClassTwo
    { // beginning of class
     
     
    // Try using a negative as an argument if you want a good laugh.  
     
    public static void main(String[] args)
    { // beginning of main
     
    	System.out.println("the first argument inputted is " + args[0]);
     
      int n = Integer.parseInt(args[0]);
     
    binary(n);
     binary(n).toString();
    System.out.println(binary(n).toString());
     
    } // end of main
     
    public static String binary(int n)
    { // beginning of method;
     
    	Integer n2 = n; // makes my int value be set copied into an Integer value
    	 System.out.println("The binary value should be " + n2.toBinaryString(n)); // just wanted to see what this would do
     
    String str = "";
     
    // my humor 
    if (n < 0)
    {
    	String oops = "Sorry, no can do!";
    	String doh = "Charlie, this is not your day!";
    	String tryAgain = "Please try again.  Have a nice day. ";
    	String funnyString = oops+ " " + doh + " " + tryAgain;
    	JOptionPane.showMessageDialog(null, "Due to increasing costs and the bad economy and the recent raise in taxes and global warming taxes, we did not have the resources to be able to have this method work with negative values.", "No negatives.", JOptionPane.ERROR_MESSAGE);
    	JOptionPane.showMessageDialog(null, "That and it won't work.");
    	JOptionPane.showMessageDialog(null, "Thank President Obama", "Blame the Democrats", JOptionPane.ERROR_MESSAGE);
    	JOptionPane.showMessageDialog(null, "Blame Al Gore too!", "The stupid 'Inconvenient Lie'!", JOptionPane.ERROR_MESSAGE);
    	return funnyString;
    }
    // a base case
     
    if (n == 0)
    { // beginning of if
    str.concat(n2.toString()); // concats this to the previous Strings, if any.  If none, concats nothing to this.  
    return("The final value is " +str);
    } // end of if
     
    else if (n ==1)
    { // beginning of else if
    str.concat(n2.toString());
    return("The final value is " +str);
    } // end of else if
     
    else
    { // beginning of else
    /*	Integer n3 = n2%2;
    	System.out.println(n3.toString());
     
    str.concat((n3).toString());
    System.out.println("Str is: " + str);
    binary(n/2); */
     
    Integer n3 = n2%2;
    str.concat((n3).toString()); // should concat n2%2
    System.out.println("Str is: " + str);
    return binary(n/2);
     
     
    } // end of else
     
     
    } // end of method
     
    } // end of program

    It seems that it'll say
    "Str is: " if I don't have the toBinaryString() method called

    and "Str is: value that keeps getting smaller till it is empty or almost empty" if I do.

    What's going on?

    the first binary value I'm getting with toBinaryString(int n) is not the same binary value I'm getting with final value. I'm sure something is working backwards, but what, and how do I get it to work nicely so it'll print the binary the right way and also have it equal to the value of the first time I call toBinaryString(int n)?
    Last edited by javapenguin; October 19th, 2010 at 12:31 PM.

  18. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Have a few odd problems.

    "Str is: value that keeps getting smaller till it is empty or almost empty" if I do.
    But, that is what recursion is...

    There really isn't any point in having that System.out.println in you else statement in your recursive method. There is no real way to track it by doing that other than just watching the digits go down.

    I still don't quite understand what this method should do.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  19. The Following User Says Thank You to aussiemcgr For This Useful Post:

    javapenguin (October 19th, 2010)

  20. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Post Re: Have a few odd problems.

    Quote Originally Posted by aussiemcgr View Post
    But, that is what recursion is...

    There really isn't any point in having that System.out.println in you else statement in your recursive method. There is no real way to track it by doing that other than just watching the digits go down.

    I still don't quite understand what this method should do.
    No, no, no. It's returning nothing.

    Program 2
    Write a recursive method binary(int n) that will return a String that stores the binary representation of that number (i.e. the string will have only 0s and 1s).
    Here is how the binary of an integer number is calculated: let the number be 53.
    53/2 = 26, 53-2*26 = 1 26/2 = 13, 26-2*13 = 0 13/2 = 6 , 13-2*6 = 1 6/2 = 3 , 6-2*3 = 0 3/2 = 1 , 3-2*1 = 1 1/2 = 0 , 1-2*0 = 1 The binary representation of 53 is 110101, which are the above answers read from bottom to top. Thus binary(53) should return a string 110101.
    Hint: The binary representation of 26 is 11010.

    I'm gonna fiddle with Eclipse and see what I can get.

    Hmmm....is it losing my String value and not concatinating it?

    I think something is backward.

  21. #15
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Have a few odd problems.

    Re: string concat

    Reread (or read for the first time) post #11... aussiemcgr has already explained how you are using the concat method incorrectly, and you seem to be ignoring it.

  22. The Following User Says Thank You to DavidFongs For This Useful Post:

    javapenguin (October 19th, 2010)

  23. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    Quote Originally Posted by aussiemcgr View Post
    Well, you have two problems. The first is exactly what the compiler told you. You want to return something or there is no way to capture the value. So, you want this:
    else
    { // beginning of else
        Integer n3 = n2%2;
    str.concat((n3).toString()); // should concat n2%2
    System.out.println("Str is: " + str);
    return binary(n/2);
     
     
    } // end of else

    The second is that the String method concat(String) returns a String, but you are not capturing that String (I've made this mistake several times, do it once and never forget the reason again). So, you have two options, you can use the concat method, or you dont have to. You can concat String with the plus sign. Here are both ways:
    WITH CONCAT:
    str = str.concat((n3).toString());
    WITHOUT CONCAT:
    str = str+(n2%2);

    Since the mathematics are in parentheses and is not between quotes, it will be evaluated prior to concatenating to the Strings around it.
    Do I want it to be evaluated after or before?

  24. #17
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    Ok, how do I capture this String? It seems to be getting lost.

  25. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    It seems the values after my first digit are now getting lost.

    package Assignment5;
     
    /**
     * @author pradcoc
     *
     */
     
    import java.io.*;
    import java.util.*;
     
    import javax.swing.JOptionPane;
     
    public class RecursionClassTwo
    { // beginning of class
     
     
    // Try using a negative as an argument if you want a good laugh.  
     
    public static void main(String[] args)
    { // beginning of main
     
    	System.out.println("the first argument inputted is " + args[0]);
     
      int n = Integer.parseInt(args[0]);
     
    // binary(n);
    // binary(n).toString();
    System.out.println(binary(n).toString());
     
    } // end of main
     
    public static String binary(int n)
    { // beginning of method;
     
    	Integer n2 = n; // makes my int value be set copied into an Integer value
    	// System.out.println(n2.toBinaryString(n)); // just wanted to see what this would do
     
    String str = "";
     
    // my humor 
    if (n < 0)
    {
    	String oops = "Sorry, no can do!";
    	String doh = "Charlie, this is not your day!";
    	String tryAgain = "Please try again.  Have a nice day. ";
    	String funnyString = oops+ " " + doh + " " + tryAgain;
    	JOptionPane.showMessageDialog(null, "Due to increasing costs and the bad economy and the recent raise in taxes and global warming taxes, we did not have the resources to be able to have this method work with negative values.", "No negatives.", JOptionPane.ERROR_MESSAGE);
    	JOptionPane.showMessageDialog(null, "That and it won't work.");
    	JOptionPane.showMessageDialog(null, "Thank President Obama", "Blame the Democrats", JOptionPane.ERROR_MESSAGE);
    	JOptionPane.showMessageDialog(null, "Blame Al Gore too!", "The stupid 'Inconvenient Lie'!", JOptionPane.ERROR_MESSAGE);
    	return funnyString;
    }
    // a base case
     
    if (n == 0)
    { // beginning of if
    str = str.concat(n2.toString()); // concats this to the previous Strings, if any.  If none, concats nothing to this.  
    return(str);
    } // end of if
     
    else if (n ==1)
    { // beginning of else if
    str = str.concat(n2.toString());
    return(str);
    } // end of else if
     
    else
    { // beginning of else
    /*	Integer n3 = n2%2;
    	System.out.println(n3.toString());
     
    str.concat((n3).toString());
    System.out.println("Str is: " + str);
    binary(n/2); */
     
    Integer n3 = n2%2;
     
    str = str.concat((n3).toString()); // should concat n2%2
    System.out.println("Str is: " + str);
    return  binary(n/2);
     
     
    } // end of else
     
     
    } // end of method
    } // end of class

    It's capturing them with Str is: but it won't concat these.

    I think it's from possible dual return statements.

  26. #19
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Have a few odd problems.

    It's outputting this:
    the first argument inputted is 32
    Str is: 0
    Str is: 0
    Str is: 0
    Str is: 0
    Str is: 0
    Str is finally: 1
    1

    I'd like it to output Str is finally: 100000

    How do I get the zeroes in there?

Similar Threads

  1. scanner problems
    By deepthought in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 29th, 2010, 09:49 AM
  2. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  3. Problems with If validation
    By websey in forum Loops & Control Statements
    Replies: 1
    Last Post: November 18th, 2009, 08:43 AM
  4. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM