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 13 of 13

Thread: Reversing letters of a words in a sentence keeping the words in the same order

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Reversing letters of a words in a sentence keeping the words in the same order

        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.println("Welcome to the word reversing program");
            System.out.println("First you will enter a sentence and then the letters of each word will be in the reversed order");
            System.out.print("Please enter a Sentence: ");
            String original = input.next();
            wordReverse(original);
            System.out.println("Reversing the words in the sentence " + original + " looks like: " + );
        }
     
        public static String wordReverse(String original){
     
            StringTokenizer string = new StringTokenizer(original);
     
            Stack<Character> charStack = new Stack<Character>();
     
            while (string.hasMoreTokens()){
     
            String temp = string.nextToken();
     
            for (int i = 0; i < temp.length(); i ++){
     
            charStack.push(temp.charAt(i));
        }
            charStack.push(' ');
        }
     
            StringBuilder result = new StringBuilder();
            while(!charStack.empty()){
            result.append(charStack.pop());
        }
     
            return result.toString();   
        }
    }

    I have two problems. The first problem is with the output. How do you return the string to System.out.println("Reversing the words in the sentence " + original + " looks like: " + );. The second problem is the outcome. When I had the code a little different to see the outcome, the only thing appeared is the first word of the string. I need all the words in the stack to appear.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    Quote Originally Posted by georger55 View Post
    I have two problems. The first problem is with the output. How do you return the string to System.out.println("Reversing the words in the sentence " + original + " looks like: " + );.
    You need to assign the String returned by the wordReverse(...) method into a String variable, and then add this variable to the end of your println method call.

    The second problem is the outcome. When I had the code a little different to see the outcome, the only thing appeared is the first word of the string. I need all the words in the stack to appear.
    Can you show us sample output to let us know exactly what you mean here?

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    Quote Originally Posted by Fubarable View Post

    Can you show us sample output to let us know exactly what you mean here?
    if a user typed in: java is fun then the output would be avaj si nuf

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    To get all the words reversed, first you have to read in *all* the words. You only read in one word since you call Scanner#next().

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    Can you possibly give an example or show me?

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    In using consoles scanner method you would use nextLine(). EG
    Scanner in = new Scanner(System.in);
     
    System.out.println("Go forth and type something.");
    String message = in.nextLine();

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    Exactly as Tjstretch has shown is how to do it, but more importantly, you should learn to refer to the Scanner API to see what methods are available before asking for code handouts here. Studying the API is a necessary skill that you should practice as it will only get better with use.

  8. #8
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    Fubarable - I am not asking for handouts, I was asking for some help on a part I was stuck on. If it was a hand out I am sure I wouldn't had as much done as I already did. Second, the input.next line reverses the whole sentence. I need to the words to stay in the same order and just have the words reversed.

    Tjstretch - Thank you for helping. The only problem with that output it reverses the whole sentence. If you type in hello world the output would be dlrow olleh. I need the output to be olleh dlrow.

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    Quote Originally Posted by georger55 View Post
    Fubarable - I am not asking for handouts, I was asking for some help on a part I was stuck on. If it was a hand out I am sure I wouldn't had as much done as I already did. Second, the input.next line reverses the whole sentence. I need to the words to stay in the same order and just have the words reversed.

    Tjstretch - Thank you for helping. The only problem with that output it reverses the whole sentence. If you type in hello world the output would be dlrow olleh. I need the output to be olleh dlrow.
    I understand. Now that you've got the whole String you need to change your program logic to get the words text reversed but not the words themselves. I'm betting that you can solve this if you try. Why not give it a go first and then show us your attempt.

  10. #10
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    Hint: use a ArrayList<String> and add each reversed word to it.

  11. #11
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.println("Welcome to the word reversing program");
            System.out.println("First you will enter a sentence and then the letters of each word will be in the reversed order");
            System.out.print("Please enter a Sentence: ");
            String original = input.nextLine();
            wordReverse(original);
            String reverse = wordReverse(original);
            fixOrder(reverse);
            ArrayList newSentence = fixOrder(reverse);
            System.out.println("Reversing the words in the sentence: " + original + ", looks like: " + newSentence );
        }
     
        public static String wordReverse(String original){
     
            StringTokenizer string = new StringTokenizer(original);
     
            Stack<Character> charStack = new Stack<Character>();
     
            while (string.hasMoreTokens()){
     
            String stack = string.nextToken();
     
            for (int i = 0; i < stack.length(); i ++){
     
            charStack.push(stack.charAt(i));
        }
            charStack.push(' ');
        }
     
            StringBuilder result = new StringBuilder();
            while(!charStack.empty()){
            result.append(charStack.pop());
        }
     
            return result.toString();   
        }
     
        public static ArrayList fixOrder(String reverse){
     
           ArrayList arrayList = new ArrayList();
           arrayList.add(reverse);
           Collections.reverse(arrayList);
           return arrayList;
     
         }
    }

    I tried to reverse the order of the words in this manner but the words were still in the incorrect order.

  12. #12
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Reversing letters of a words in a sentence keeping the words in the same order

    I would use an ArrayList<String> inside of your wordReverse method. When each word has been reversed, take that char array, create a String from it and put it into the ArrayList.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Reversing letters of a words in a sentence keeping the words in the same order

    Hi, I did not use any ArrayList/Stack. Hope this helpful.


    import java.util.Scanner;

    public class ReverseString {

    private static Scanner input;

    public static void reverse(String s)
    {
    String s1="";
    for (int i=0; i<=s.length()-1; i++)
    {
    if(s.charAt(i)!=' ')
    s1=s1+s.charAt(i);
    else {
    String ops=revst(s1);
    System.out.print(ops+" ");
    s1="";
    }
    if(i==s.length()-1){
    String ops=revst(s1);
    System.out.print(ops);
    }
    }
    }

    public static String revst(String s)
    {
    String revString = "";
    for (int j=s.length()-1; j!=-1; j--)
    revString= revString + s.charAt(j);
    return revString;
    }

    public static void main(String[] args)
    {
    input = new Scanner(System.in);
    System.out.println("Please enter a Sentence: ");
    String ips = input.nextLine();
    System.out.println("Sentence after reversing the words is:");
    reverse(ips);
    }

    }

Similar Threads

  1. Reverse Words Problem
    By mulligan252 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 16th, 2011, 04:03 PM
  2. Highlighted words Not resolved
    By Rajiv in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 4th, 2011, 07:28 AM
  3. numbers to words using do-while & if-else & JOptionPane
    By mikkko in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 15th, 2011, 05:28 AM
  4. Arranging words in a sentence in alphabatical order.
    By J2000 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 24th, 2011, 10:04 AM
  5. Help: Num to words
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2010, 06:55 PM