Load String into CharArray and then Print String in Reverse?

Hey, what would the code be to:1. Ask user for name. (scanner)2. Load his name into a CharArray.3. Then print out his name backwards.thanks a lot!
[181 byte] By [dforevergolda] at [2007-11-26 22:53:03]
# 1
Ahh... Sounds like one of my school assignments.Maybe if you can write the code yourself and come back if you have any problems, then the nice people here might help you.Otherwise, you're going to be insulted by them.
lethalwirea at 2007-7-10 12:16:03 > top of Java-index,Java Essentials,New To Java...
# 2

> Hey, what would the code be to:

class LazyBones {

// declare instance variables

// constructor(s)

// method(s)

public static void main(String[] args) {

LazyBones me = new LazyBones();

me.fail();

}

}

Just fill in the gaps!

floundera at 2007-7-10 12:16:03 > top of Java-index,Java Essentials,New To Java...
# 3
well, considering this is my first post ever on this forum, i had no idea people were going to be so nice. i'll try better next time.
dforevergolda at 2007-7-10 12:16:03 > top of Java-index,Java Essentials,New To Java...
# 4
also, because I have no experience with Java?maybe if I saw someone's example code, I would learn something?
dforevergolda at 2007-7-10 12:16:03 > top of Java-index,Java Essentials,New To Java...
# 5

What you are trying to accomplish is somewhat advanced for a person just learning java.

I'd suggest reading through the Java tutorial here.

http://java.sun.com/docs/books/tutorial/getStarted/index.html

1) import the appropriate classes for the scanner

2) Use the API to find the appropriate methods you will need in order to convert the string into a char array.

3) Use a for loop to print the array backwards.

lethalwirea at 2007-7-10 12:16:03 > top of Java-index,Java Essentials,New To Java...
# 6

dforevergold has a head on his neck. So, I don't understand people saying about assignments etc... If he needs help and we can help then we must help.

Here's the code (but don't forget about comments!):

import java.util.Scanner;

public class Just {

public static void main(String[] args) {

// a scanner to get user's input

Scanner scan = new Scanner(System.in);

// Scanner.nextLine() returns the entered line

String name = scan.nextLine();

// toCharArray() converts a 'String' object to a 'char[]' object

char[] ch = name.toCharArray();

// next FOR-loop goes from the last element in the array to the first one and prints them onscreen

for (int i = ch.length - 1; i >= 0; --i) System.out.print(ch[i]);

}

}

I hope it helps you!

Yours Sincerely,

Nikaustr

Nikaustra at 2007-7-10 12:16:03 > top of Java-index,Java Essentials,New To Java...
# 7

> If he needs help and we can help

> then we must help.

Looks like you just made a friend for life. With the above statement you will be providing code for the rest of his/her school/college life. Can you do my work for me while I stay at home?

Message was edited by:

JStudent911

JStudent911a at 2007-7-10 12:16:03 > top of Java-index,Java Essentials,New To Java...
# 8

It took about 1 minute for me to write that code. So, if you have a one-minute job - I will be happy to help you ;)

I have a biological background, so I know that only BEST of the BEST will remain. Thus, if I am doing your job - you won't have a chance to become that "best of the best" ;)

Nikaustra at 2007-7-10 12:16:03 > top of Java-index,Java Essentials,New To Java...