473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Word Scramble

hello everyone! i m very here. i tried to make a program that will reverse the letters in each of a sequence of words while preserving the order of the words themselves.

Sample Input
Hello World!

Sample Output
olleH !dlroW

My problem is when i give a default string sentence it works properly but when the program asks input from the user, it only prints the first word of the sentence in reverse order. how can i fix the problem?

My code:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class WordScramble{
  3.   public static void main(String [] args){
  4.      Scanner sc = new Scanner(System.in);
  5.      String sentence = sc.next();
  6.      String[] words = sentence.split(" ");
  7.  
  8.      for (int i = 0; i < words.length; i++) {
  9.         StringBuffer buffer = new StringBuffer(words[i]);
  10.         buffer = buffer.reverse();
  11.         String rev = buffer.toString();
  12.         System.out.print(rev + " ");
  13.      }
  14.   }
  15. }
Jul 31 '09 #1
3 7457
JosAH
11,448 Expert 8TB
You can try sc.nextLine() instead of sc.next(); it'll read the entire line instead of just one String (that is separated by white space).

kind regards,

Jos
Jul 31 '09 #2
yeah it works. thnx JosAH.
Jul 31 '09 #3
JosAH
11,448 Expert 8TB
@onindita
You're welcome of course; tip: read the very first article in this group; it contains a link to the API documentation of all the core classes; download it on your disk, it's worth it.

kind regards,

Jos
Jul 31 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Klaus Ambrass | last post by:
Hi all, I write applications for my company's intraweb, and recently we've had some eager users trying to get at some data they shouldn't. The way they did it was to look at the pages input tags...
3
by: gdogg1587 | last post by:
Greetings. I'm working on a program that will "descramble" words. Think of a word scramble game where there is a list of characters, and several blank spaces to denote the word(s) that you are...
4
by: Steve | last post by:
Hi, I am trying to do a very simple "encryption" of a text string in java script. For instance, if the user enters : steve, I want to just convert each character to its ASCII value and add 5...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
3
by: Jeremy S | last post by:
What are some options for scrambling information in the QueryString. Consider this URL for example: SomePage.aspx?userid=15 I don't care if there is something in the querystring - I'd just...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
5
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and...
8
by: Scholar | last post by:
Hello friends, there is yet another problem for you... We have got a string say HOUR Now we need a program that can choose the specified number of words say n from this word in all possible ways...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.