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

Thread: quick question, please HELP!

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post quick question, please HELP!

    Hey!
    Quick and simple question for you guys. I would like the user to enter any 2 digit numbers. Then, make those two numbers reverse.

    Example: 92 would become 29. How do I do that?

    Thank you.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: quick question, please HELP!

    What have you tried? Have you read the java tutorials over at Oracle (see the link in my signature)? Where are you stuck? Is there something confusing? See the following:
    http://www.javaprogrammingforums.com...e-posting.html

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Location
    toronto
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: quick question, please HELP!

    show me how you have done so far or is there any idea to solve your problem?

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: quick question, please HELP!

    I figured it out! But now I'm stuck on another problem... I'm trying to enter a sentence and then make the first word of the sentence go to the end. As in:
    Hello my name is Mat.

    would be transformed into:

    my name is Mat. Hello

    Thanks!

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: quick question, please HELP!

    Once again...what have you tried? You solved your first problem, evidence that you can probably tackle this problem as well. See the API for String, which contains a wealth of methods to help you accomplish this task.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: quick question, please HELP!

    Okay, here's where I'm at right now. However I found myself to be completely blocked from here on end, I don't think we covered this in class, probably why it's a bonus question.

    Scanner keyboard = new Scanner (System.in);
     
            String space = " ";
            String sentence = keyboard.nextLine();
            String[] tokens = sentence.split(space);
            String newSentence = "";
     
                for (int i = 1; i<tokens.length; i++)
                {
                 newSentence = sentence + space;
                }
            newSentence += tokens[0];
            System.out.println(newSentence);


    When I enter Java is fun. it displays Java is fun Java so yah, that's the problem.

    Thank you

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: quick question, please HELP!

    That's a darn good start, and I'm glad you think java is fun

    Something that might help you is to debug this using some println statements (see Debugging with System.out.println) This lets you print out the logic of the program - you can then see what the variables are at different times and locations within the program. For example - how does newSentence change when you loop over the tokens? Often times just looking at these changes - writing down and following how the variables change - in a stepwise manner lets the solution jump right out.

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: quick question, please HELP!

    Thanks, and I do like Java! However I don't find it user friendly at all!

    Yah... I'm still stuck on this, I just can't my fingers on how to delete the first word of a string sentence after the word has been copied to the end of the sentence. If you could hint me or advise me anything, I would greatly appreciate it because this is starting to frustrate me . Thank you!

  9. #9
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: quick question, please HELP!

    You've actually performed what is required already, but you haven't imposed it in all the required areas.

    Consider this:
    newSentence = sentence + space;
    What you're essentially saying is: myNewSentence = "Java is fun" + SPACE;.
    Now you know that's not what you want.

    But once you exit the loop, you extract a word from the array instead: newSentence += tokens[0];.
    I.e - What you're saying is: newSentence = "Java is fun" + "Java";
    Why don't you try taking that approach inside the for loop, and see where that gets you.

    If you did take copeg's suggestion and use println statements to track what's happening, you would have gotten this a while back
    Last edited by newbie; September 22nd, 2011 at 07:05 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: quick question, please HELP!

    public class Ex2 {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    }

    }
    Last edited by copeg; September 26th, 2011 at 08:55 AM.

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: quick question, please HELP!

    RaviPKorra, please don't spoon-fed solutions. I recommend you read the forum rules, and the following link:
    The Problem with Spoon-feeding
    I have edited your post.

  12. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: quick question, please HELP!

    Why would you do that..? I did most of the code myself, I had one small problem and you keep sending me on wild goose chases. I thought this subforum was called What's wrong my code. You keep replying to my questions with even more questions...

  13. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: quick question, please HELP!

    Quote Originally Posted by Nemus View Post
    Why would you do that..? I did most of the code myself, I had one small problem and you keep sending me on wild goose chases. I thought this subforum was called What's wrong my code. You keep replying to my questions with even more questions...
    I apologize if my time and knowledge, handed over to you unpaid and is considered leading you in wild goose chases is not appreciated, but handing over answers to a problem denies you of the learning process, ends up leading you in many more wild goose chases down the line - especially answers which show the poster handing over answers hasn't even read the problem at hand.

  14. #14
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: quick question, please HELP!

    Example:
    "Java is fun."
    You want to take the first word in the end, right?
    Did you hear about String? If no, find it out.
    Split your main string into two strings.
    1. Java 2. is fun.
    Concatenate string2+string1.
    Hope it will help.
    Anyways, copeg and newbie has provided you a great help. If you can't take that, you will not be able to try this as well. Good luck

  15. #15

    Default Re: quick question, please HELP!

    "Java is fun"

    1. Locate the first " ". *int spacePos = " "

    2. Create two separate strings: strSentence1 and strSentence2.

    3. strSentence1.substring(spacePos + 1)
    strSentence2.substring(0,spacePos)

    4. Create a strNewSentence then concatenate 1 and 2 together.
    Please insert NO COINS.

Similar Threads

  1. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  2. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  3. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM
  4. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  5. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 05:48 PM