Hi

As I said, there are many ways to make it working and to look nice.

One way to write your code might also be:
    ...
    for ( int i = name.length() - 1 ; i >= 0 ; i-- )
        System.out.print(name.charAt(i));
    System.out.println();
    ...

In this way neither the String ans nor the char and (nor the int l) need to be declared.

Hope it helps
Mihai

Le 02/09/2010 13:14, Olukosi, Olanrewaju Temitope a écrit :
Hello Guys,

I really appreciate your responses, Thanks Alfor for introducing me to some new methods as I really appreciate it. Also Mihai, you have been there before now and I will say thanks to you again but why is it that I must initialize before using the String ans? because going by your solution, I must still have to initialize the String for it to work.

Thanks G for the Naming Convention thing as it will also go a long way.

Regards
On Wed, Sep 1, 2010 at 9:47 PM, Mihai DINCA <[email protected] <mailto:[email protected]>> wrote:

    Hi

    You say "String and, ans;", then, later, "and = name.charAt(i);".

    As the error message says, the method "charAt(...)" returns a
    value of type "char", while your variable "and" is of type "String".

    There are many ways to make your code working, but the simplest
    way is to replace the line of code:
        ...
        String and, ans;
        ...
    by two lines:
        ...
        String ans;
    *char and;*
        ...

    The line of code "ans = ans + and;" will still work after this
    modification, as the "+" operator allows to append a char (or any
    other simple type) to a String (this is what allows you to write
    "for(int i = 0; i < 10; i++) System.out.println(*"Iteration # " +
    i*);").

    Hope it helps
    Mihai


    Le 01/09/2010 22:10, Olukosi, Olanrewaju Temitope a écrit :
    Hello Guys,

    I am having some problems running this program. Kindly help

    import java.util.Scanner;
    class backward
    {
        public static void main (String[] Args)
        {
            Scanner in=new Scanner(System.in);
            System.out.print("Enter a Name to Reverse:");
            String name = in.nextLine();
            String and, ans;
            int l=name.length();
            int i;
            for (i=l-1; i>=0; i--)
            {
                and=name.charAt(i);
                ans= ans+and;
            }
            System.out.println("The Output is:" + ans);
        }
    }


    The error I have is stated thus using TextPad

    C:\JavaClass\backward.java:14: incompatible types
    found   : char
    required: java.lang.String
                and=name.charAt(i);
                               ^
    1 error

    Tool completed with exit code 1

-- DISCLAIMER
    This message contains privileged and confidential information and
    is intended only for the individual named. If you are not the
    intended recipient you should not disseminate, distribute, store,
    print, copy or deliver this message. Please notify the sender
    immediately by e-mail if you have received this e-mail by mistake
    and delete this e-mail and any attachments from your system.
    E-mail transmission cannot be guaranteed to be secure or
    error-free as information could be intercepted, corrupted, lost,
    destroyed, arrive late or incomplete or contain viruses. The
    sender therefore does not accept liability for any errors or
    omissions in the contents of this message which arise as a result
    of e-mail transmission.
    Thank you   --   Tope Olukosi
-- To post to this group, send email to
    [email protected]
    <mailto:[email protected]>
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:[email protected]>
    For more options, visit this group at
    http://groups.google.com/group/javaprogrammingwithpassion?hl=en




--
DISCLAIMER
This message contains privileged and confidential information and is intended only for the individual named. If you are not the intended recipient you should not disseminate, distribute, store, print, copy or deliver this message. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail and any attachments from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message which arise as a result of e-mail transmission.
Thank you   --   Tope Olukosi

--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to