Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Question Incorrect String Returns

    I am trying to make a program that will read the String that a user inputs, remove the lower cases, remove the spaces, remove the punctuation, and reverse and add the reversed String onto the old one, but I am getting incorrect returns (testing using breakpoints and println())
    Here is my code so far

    Java Code:
    import java.util.*;
    public class Palindrome
    {
        public String lowerCase(String str)
        {
            str.toLowerCase();
            return str;
        }
    
        public String reverse(String str)
        {
            if (("" == str) || (str.length()  <= 1))
            {
                return str;
            }
            else
            {
                return reverse(str.substring(1)) + str.charAt(0);
            }
        }
        public String space(String str)
        {
            int temp = 0;
            if(temp == -1)
            {
                return str;
            }
            else
            {
                return str.substring(0,temp) + space(str.substring(temp+1)); 
            }
            
            
            
        }
    import java.util.*;
    
    public class Driver
    {
        private static Scanner in;
        public static void main(String[] args)
        {
            Palindrome pal = new Palindrome();
            in = new Scanner(System.in);
            String tempPhrase = "";
            String phrase = "";
            System.out.println("Hello and welcome to your Palindrome confirmer for a lack of a better name!");
            System.out.println("First off what is the phrase that you want us to check? ");
            tempPhrase = in.nextLine();
            phrase = tempPhrase;
            System.out.println();
            System.out.println("OK, give us a few milli-seconds of your worthless life to do this :D");
            System.out.println(phrase);
            phrase = pal.lowerCase(phrase);
            System.out.println(phrase);
            phrase = pal.reverse(phrase);
            phrase = pal.space(phrase);
            System.out.println(phrase);
            
        }
    }
    The print lines are what I use to test and make sure that the String properly changes with each method called.
    If you could tell me how I can over come this roadblock I will be very grateful :D

  2. #2
    thomachan21 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Re: Incorrect String Returns

    Ask your IT for any help as we cannot solve it for you.
    Or give you any step by step instructions
    Dont ask us to review your code, as you should be able to do it yourself.

    If your IT doesnt give u the response you want, then you can ask us.

  3. #3
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: Incorrect String Returns

    Yeah, yeah.... You sitting next to me dude.....
    I have asked the IT...

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Incorrect String Returns

    You're going to have to debug the different pieces of this code separately. Start with the very first thing you do. Does that work? If so, move on to the next small step and test that BY ITSELF, using hardcoded values as input and simply printing out the output. Repeat that process until you've found the offending piece, than you can create an SSCCE that further examines the problem.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: Incorrect String Returns

    How exactly would that be done? I am using the BlueJ program to write up my code.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Incorrect String Returns

    For a first error: line # doesn't change String str to lower casse; i.e. it returns another String converted to lower case characters; so you can combine lines #6 and #7 to one line:

    Java Code:
    return str.toLowerCase();
    probably there are more errors in your code; I didn't look.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Inherited method that returns string
    By Xeal Rebad in forum New To Java
    Replies: 5
    Last Post: 05-23-2011, 02:26 PM
  2. Replies: 1
    Last Post: 02-07-2011, 11:44 AM
  3. Replies: 2
    Last Post: 10-15-2010, 06:09 AM
  4. String file and carriage returns
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 02:28 AM
  5. Replies: 3
    Last Post: 08-20-2009, 04:57 PM

Posting Permissions

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