Results 1 to 6 of 6
Thread: Incorrect String Returns
- 12-19-2012, 06:27 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
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
The print lines are what I use to test and make sure that the String properly changes with each method called.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); } }
If you could tell me how I can over come this roadblock I will be very grateful :D
- 12-19-2012, 06:30 PM #2
Member
- Join Date
- Nov 2012
- Posts
- 11
- Rep Power
- 0
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.
- 12-19-2012, 06:31 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Incorrect String Returns
Yeah, yeah.... You sitting next to me dude.....
I have asked the IT...
- 12-19-2012, 06:32 PM #4
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!
- 12-19-2012, 06:36 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Incorrect String Returns
How exactly would that be done? I am using the BlueJ program to write up my code.
- 12-19-2012, 06:37 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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:
probably there are more errors in your code; I didn't look.Java Code:return str.toLowerCase();
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Inherited method that returns string
By Xeal Rebad in forum New To JavaReplies: 5Last Post: 05-23-2011, 01:26 PM -
String passed as argument to a constructor returns null value
By eLancaster in forum New To JavaReplies: 1Last Post: 02-07-2011, 10:44 AM -
What is the Java library method that takes String parameter and returns a Boolean?
By CaptainBlood in forum New To JavaReplies: 2Last Post: 10-15-2010, 05:09 AM -
String file and carriage returns
By AJArmstron@aol.com in forum New To JavaReplies: 2Last Post: 04-17-2010, 01:28 AM -
Checking if string contains only tabs/spaces/newlines/carriage returns
By Singing Boyo in forum New To JavaReplies: 3Last Post: 08-20-2009, 03:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks