Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Program to take name as input and write it backwards

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program to take name as input and write it backwards

    Can someone please help me with my programming project?

    The directions are:
    Solution must employ at least one method other than the main() method. Program should be annotated. An explanation of all features of the program.

    I'm trying to make a working program that takes your name and writes it backwards. Is that possible?
    Can someone please help me write this and give an explanation. Please! I need this ASAP!

    Thanks!


  2. #2
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: Need Help with Programming project

    hi,

    here is my code. hope it helps.

     
    import java.io.*;
     
    public class WordInverter {
     
     
     
     private static BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
     
        public  static void main(String[] args)throws IOException
     
         { 
     
           String strInv = null,word;
     
            System.out.print("Enter a word : ");
            word=br.readLine();
            strInv = invert(word);
     
     
        System.out.println("The inverted word  : " + strInv);
     
     
          }
     
    //another method
        public static String invert (String s) {
             String temp = "";
             for (int i=s.length()-1; i>=0; i--)
                  temp += s.charAt (i);
             return temp;
        }
     
    }

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Need Help with Programming project

    Hello snake101 and welcome to the Java Programming Forums.

    Yes what you are trying to do is possible.

    I would use the Scanner class to read in the persons name from the console and then use the StringBuffer reverse() method to reverse the String.

    Here is an example. You can comment it yourself.

    import java.util.Scanner;
     
    public class Snake101 {
     
        /**
         * JavaProgrammingForums.com
         */    
        private static String name, nameReversed;
     
        public static void reverse(String n){
     
            nameReversed = (new StringBuffer(n)).reverse().toString();
            System.out.println(nameReversed);
        }
     
        public static void main(String[] args) {
     
            Snake101 snake = new Snake101();
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter your name:");
            name = sc.next();
            snake.reverse(name);
            sc.close();        
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Need Help with Programming project

    Well have you main function prompt for the name and then pass it to a function called reverse that returns the revrse of thee String given to it and then print it out

    public class snake{
         public static void main(Sting[] args){
              System.out.print("Please enter your name: ");
              String name = (new Scanner(System.in)).nextLine();
              System.out.println("The reverse of your name is: " + this.reverse(name) );
         }
     
         private static String reverse(String name){
              // You can write the reverse function, or at least have a shot
              return reversedName;
         }
    }

    Regards,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Thumbs up Re: Need Help with Programming project

    You are spoilt for choice snake101

    Thank you for your post Truffy. That is a good way to reverse a String, although the StringBuffer reverse() method is much easier IMO.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Need Help with Programming project

    Good posts guys, shame I wasn't willing to give an answer straight away . Imight have to adapt my style so that my answer is as helpful as other when we post at the same time hehe

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  7. #7
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Thumbs up Re: Need Help with Programming project

    Quote Originally Posted by JavaPF View Post
    You are spoilt for choice snake101

    Thank you for your post Truffy. That is a good way to reverse a String, although the StringBuffer reverse() method is much easier IMO.

    LOL. i didn't think of using scanner. LOL. i do it on a long way. Anyway, atleast snake can see different approach in solving it.


    Cheers guys.

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Need Help with Programming project

    Quote Originally Posted by Freaky Chris View Post
    Good posts guys, shame I wasn't willing to give an answer straight away . Imight have to adapt my style so that my answer is as helpful as other when we post at the same time hehe

    Chris
    Well this was my first post of the morning. I was feeling kind! Nothing like a Java problem & a cup of coffee to wake you up.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Programming partner that can earn revenue
    By Jay in forum Project Collaboration
    Replies: 0
    Last Post: May 25th, 2009, 02:33 PM
  2. Adventure ride project
    By Ciwan in forum Java Theory & Questions
    Replies: 12
    Last Post: February 15th, 2009, 11:08 AM
  3. Database connection without using netbease
    By haygaurav in forum Java IDEs
    Replies: 9
    Last Post: December 9th, 2008, 06:14 AM
  4. Replies: 4
    Last Post: May 22nd, 2008, 10:59 AM

Tags for this Thread