java


Singular Word Substring Method


I'm having trouble determining why my substring when it is returned is always "". When single words are passed (Ex. Chemistry) I get something like the following:
C
h
e
m
i
s
t
r
y
A blank line is then printed as the substring value of "".
public String NextString(String lstring){
String substring="";
int i=0;
while (lstring.charAt(i)!=' ') {
System.out.println(lstring.charAt(i));
substring.concat(Character.toString(lstring.charAt(i)));
i++;
}
System.out.println(substring);
return substring;
}
Please help!
You never reassign the concatenated string. Try this instead:
substring = substring.concat(Character.toString(lstring.charAt(i)));
Reference: Oracle Doc
String objects are immutable. Thus concat() does not change substring, but returns a changed string. Thus you need
substring = substring.concat(...
Beyond that: you want to iterate the chars of that string. You absolutely do not need that complicated while loop for that. Instead use the for-each loop style here:
for (char singleChar : lstring.getCharArray()) {
Reason why your substring is not getting printed, because when you are doing concatenation you are not storing it in substring as a result at the end of loop substring is still blank.
Try below changes :
String substring="";
int i=0;
while (lstring.charAt(i)!=' ') {
System.out.println(lstring.charAt(i));
substring=substring.concat(Character.toString(lstring.charAt(i)));
i++;
}
System.out.println(substring);
return substring;
the string in java is immutable and when you assign it you can't change the value in it again
for that like "Devendra Lattu" said you need to assign the output of concatenation every time you concatenate new character to variable
substring=substring.concat(Character.toString(lstring.charAt(i)));
if you don't like this you can use StringJoiner from java 8 link to it its faster and mutable
I'm sorry for first answer :)

Related Links

How to know size of folder using java program? [duplicate]
Java console calculator with user-defined variables
Calling methods - Not quite understand parameters
Filter for duplicate values.
IBM Wepshere JET Transformation pageLayout Unavailable
Ouput value from sentinel loop is wrong
Does the JVM cache assembly code? [duplicate]
Send notification to all the devices connected to a Wi-Fi network
Where i Can find text file created by servlet in Eclipse
Stopped DefaultMessageListenerContainer after its initialization
My code takes up 2000MB+ to run, no idea why
How to retrieve COM4J Requirement Coverage list
How to make POST request using retrofit to server in android
Compare session value whether it matches with the textbox value [closed]
SwingBot with ellipse and rectangle [duplicate]
How to convert a string to a JSON and then extract required values from the JSON object?

Categories

HOME
cakephp
debugging
oracle11g
google-api-php-client
malloc
jpeg
bs4
gitpitch
volttron
mapserver
postgres-xl
correlation
commonmark
decomposition
undefined
flux
lucene.net
scichart
introduction
xlsxwriter
cx-freeze
clickonce
wkwebview
poltergeist
pingfederate
phpfox
tooltipster
social-media
kendo-datasource
form-data
nssegmentedcontrol
file-format
dosbox
espeak
hilbert-curve
hue
lumberjack
email-templates
midl
qwt
flickr-api
taffy
upstart
blogengine.net
worker-thread
qtwebview
account-kit
topbeat
messenger
wdf
adxstudio-portals
phishing
yaws
dataview
bluegiga
design-by-contract
system.management
natvis
intellij-14
rgeo
ibaction
graph-drawing
createprocessasuser
sonarqube5.1.2
bigbluebutton
ng-animate
coldbox
phalanger
marmalade
seaside
prettify
ccss
commoncrypto
dotnetnuke-5
cos
back-stack
quickdialog
spring-portlet-mvc
code-cleanup
gnustep
appender
createwindow
web-application-design
infrastructure
virtual-functions
project-hosting
ugc
caching-application-block

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App