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.
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.
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.
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
show me how you have done so far or is there any idea to solve your problem?
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!
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.
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
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.
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!
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
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.
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.
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.
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
"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.