473,320 Members | 1,879 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,320 software developers and data experts.

concatenating string input into one string in JAVA

This code is supposed to take in string input from the user and concatenate it into one string and print it but it doesn't print out any output HELP please :
import java.util.*;
public class STRSPL{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String S = sc.next();
String Result = "";
while (S!= null) {
Result=S+Result;
S=sc.next();
}
System.out.println(Result);
}
}
Dec 23 '11 #1
6 2724
Rabbit
12,516 Expert Mod 8TB
That's because you're stuck in the loop. Scanner.next() is blocking the program because it will always wait for input.
Dec 23 '11 #2
then how can i get the program to get out of the loop whenever there is no more input from the user?
Dec 23 '11 #3
Rabbit
12,516 Expert Mod 8TB
You can't know when the user has decided to stop inputting data. Maybe they just decided to take a break. Or they're just slow typers. They need to indicate that they have stopped inputting data. By typing a key phrase like stop or quit. Then you can have the loop exit when they type said key phrase.
Dec 23 '11 #4
Thank you.............
Dec 23 '11 #5
Then how about this version?

import java.util.*;
public class STRSPL {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String S = sc.next();
String Result = "";
while (S !="stop") {
Result = Result + S;
S = sc.next();
}
System.out.println(Result);
}
}
Dec 23 '11 #6
Rabbit
12,516 Expert Mod 8TB
Yeah, that could work.
Dec 23 '11 #7

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

Similar topics

1
by: dont bother | last post by:
Hey, I have these attributes: index which is a numerical value value vector which is a numerical float value and I want to concatenate like this:
2
by: privetv7 | last post by:
ppl... HELP!!!!! i don't know how to convert String into char..... for example i can do String input; int number; input = JOptionPane.showInputDialog( "Enter what ever" ); number =...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
4
by: priyanka | last post by:
Hi, I want to input a string from command line. I use the following program to input the string. #include<stdio.h> int main(){ char input;
7
by: Monu2301 | last post by:
hello! I am new to java but as compared to any other language like c or c++ taking input in java is difficult but I tried using Keyborad.readInt(); for reading the inputs given by the...
3
by: satyakarvvk | last post by:
Hi everybody! Please help me to overcome below runtime exception. Actually it is a simple program on basics. I want to print odd nos upto which the user asks and after printing the task,...
3
by: Energizer100 | last post by:
Hey. I'm new to Java and I'm trying to make a grades program for a class assignment. This is what I have so far. import java.util.Scanner; import static java.lang.System.out; public class...
2
by: Jose Oliver | last post by:
I am a bit baffled on this error. A windows app written in C# is attempting to execute the following query: SELECT * FROM Employee WHERE (Name LIKE '%' + @PARAM1 + '%') AND...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.