Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Question~! need help with my codes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question~! need help with my codes" Watch "Question~! need help with my codes" New topic

Question~! need help with my codes

Franky Massenger

Joined: Oct 22, 2004
Posts: 2
Hi. I just started learning Java ,and I'm working on a drill in the Java-self teaching book.
But I can't get it work.
Please tell me what the problem is and teach me a little bit.
Thank you

Here is problem that's been a huge pain in my butt. :

As a young boy Franklin Roosevelt signed his letters to his mother backwards: Tlevesoor Nilknarf. Write a program that accepts a person's name and print it backwards. Make sure that you have the appropriate capital letters.

And here goes my codes:

public class backwad
public static void main (String [] args)
String first = "Tlevesoor";
String last = "Nilknarf";
String firstN = "";
String lastN = "";
for (int i = first.lenth()-1; i <= 0; i--)
char c1 = first.charAt(i);
firstN += c1;
char b1 = firstN.charAT(0);
String a1 = firstN.substring(1,8);
firstN = character.toUpperCase(b1) + a1;
for (int i = last.lenth()-1; i <= 0; i--)
char c2 = last.charAt(i);
lastN += c2;
char b2 = lastN.charAT(0);
String a2 = firstN.substring(1,7);
lastN = character.toUpperCase(b2) + a2;

System.out.print(lastN = " ");

[ October 22, 2004: Message edited by: Franky Massenger ]
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
It is homework time already? Why don't you tell us what you understand about the code, and we'll tell you if we agree.

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
How about making life easier?

String -> an array -> reverse array -> String

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
marc weber

Joined: Aug 31, 2004
Posts: 11343

Two hints that will allow this code to compile: One of your braces is backwards, and Java identifiers are case sensitive.

(Then be prepared for Runtime Exceptions...)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Ashutosh Gupta

Joined: Oct 22, 2004
Posts: 1
There seems to be lote of typo errors. Simply, retryping from the book, taking case sensitiveness into care will do

Regards,<br />Ash
Franky Massenger

Joined: Oct 22, 2004
Posts: 2
Thank you for giving me hints. But can you guys pinpoint the part where
the errors seem to be, and fix it for me? I read through two books and I still can't firgure out what the problem is. Or Could you try to write your own codes so I can get some ideas how to compose codes. Thank you
rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
did you try compiling your java code? If so what compile errors did you get. You should be able to identify whats wrong in your code based on compile errors.

Check for the following in your code and fix it & make sure it compiles.

1) Check for the for loop braces. One of the for loop's closing braces are wrong.
2) Strings do not have lenth() & CharAT() function. What are the correct ones?

Hope this helps
marc weber

Joined: Aug 31, 2004
Posts: 11343

Once you correct that reversed brace at the end of your first "for" loop, the compiler errors will pinpoint the other syntax issues for you (the misspellings and incorrect case).

Address these one by one so you know you're headed in the right direction: Compile, fix one of the errors, compile again and you should have one less error. Compile, fix one of the errors, compile again...
I agree. Here's the link:
subject: Question~! need help with my codes
It's not a secret anymore!