943,771 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 760
  • Java RSS
Sep 23rd, 2008
0

Simple Question

Expand Post »
hi im new to java and just need a little help in the right direction. I am trying to display an input of int on number at a time. So for example the user would enter 1998 and the output would be:
1
9
9
8
and we have to use the operators / and %. I know that % is to show the remainder but it just doesn't make sense to me how they could output that on each line. I have tried solving this using just / and - but it doesn't work. Also the input can only be a 4 digit Int
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BlackNinja is offline Offline
18 posts
since Nov 2007
Sep 23rd, 2008
1

Re: Simple Question

Click to Expand / Collapse  Quote originally posted by BlackNinja ...
hi im new to java and just need a little help in the right direction. I am trying to display an input of int on number at a time. So for example the user would enter 1998 and the output would be:
1
9
9
8
and we have to use the operators / and %. I know that % is to show the remainder but it just doesn't make sense to me how they could output that on each line. I have tried solving this using just / and - but it doesn't work. Also the input can only be a 4 digit Int
Create five variables, all integers. Have a variable called year that stores 1998. Have four more variables: thousands , hundreds , tens , ones . If year = 1998, use the / and % operators to put the proper value into each variable so that thousands = 1, hundreds = 9, tens = 9, ones = 8, then display them with 4 separate println commands.
Last edited by VernonDozier; Sep 23rd, 2008 at 7:31 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Sep 23rd, 2008
0

Re: Simple Question

thanks so much , I'm new to this. You solved one of my problems, so I'm gonna ask another one that you can maybe answer. I was wondering if there was a way to take the first word in a sentence and replace it with last one and vice versa. For example the sentence "My friend is out" would turn into "Out friend is My". I've tried looking for a solution through google and found the .replace() operator, but this requires you to know what you are going to be replacing the word with. What if i don't know what the user will type in and just want the last and first words switched. Also i wouldn't know the index of the last word
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BlackNinja is offline Offline
18 posts
since Nov 2007
Sep 23rd, 2008
0

Re: Simple Question

Click to Expand / Collapse  Quote originally posted by BlackNinja ...
thanks so much , I'm new to this. You solved one of my problems, so I'm gonna ask another one that you can maybe answer. I was wondering if there was a way to take the first word in a sentence and replace it with last one and vice versa. For example the sentence "My friend is out" would turn into "Out friend is My". I've tried looking for a solution through google and found the .replace() operator, but this requires you to know what you are going to be replacing the word with. What if i don't know what the user will type in and just want the last and first words switched. Also i wouldn't know the index of the last word
Well if you start with a String that contains a sentence, you can break that String into words and have a Vector of type String called words , each element of words containing a single word. Once you have your words Vector, display the words in any order you wish. So your main task is to break the larger String containing the sentence into smaller Strings, each containing a word. There are several ways to do that. You need to know where a word begins and ends and what defines a word and what separates a word. The Character class can help you with that with functions like isLetter . The String class' substring function can come in handy once you know where a word starts and stops.

http://java.sun.com/javase/6/docs/ap...ng/String.html
http://java.sun.com/javase/6/docs/ap...Character.html
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Sep 24th, 2008
0

Re: Simple Question

yes! thanks so much.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BlackNinja is offline Offline
18 posts
since Nov 2007
Sep 24th, 2008
0

Re: Simple Question

String.split(java.lang.String) is also very useful for such things.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Swing and threads
Next Thread in Java Forum Timeline: Runtime classname of null object





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC