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

Nested if, else if, without brackets
How to auto reload app engine dev server?
How do I access variables from a broadcast receiver?
Serializing and Deserializing
How to set Action Listener to 3 buttons
How to Split the cell in itext?
Read a file from a FTP server but InputStream is always null
Printing DEBUG messages in Log file
XML Read one special element out of different xml files using java saxparser
Jenkins SVN Update only on manual build
Removing duplicate listView items
TestNG functional testing #Test order sequence
How to read values from xml and set it to bean in java? [duplicate]
Access values of JSON objects from a JSON array in Servlet (JAVA)
Automatic string to date conversion
Errors in sending sms through java [closed]

Categories

HOME
log4j
c#-4.0
server
google-play
filtering
amortized-analysis
sqlite3
baqend
ravendb
enterprise-library-5
add
slick-slider
here-api
floating-action-button
modelica
quickbooks
django-simple-history
uitypeeditor
graphlab
highlight.js
tostring
pc
lucene.net
introduction
ml
opencover
dxf
cas
user-interaction
traffic
rst2pdf
subdomains
primitive
lightswitch-2013
phonegap
http-status-code-503
language-concepts
mapdb
file-format
main
gesture
xenforo
unobtrusive-validation
hue
ruby-on-rails-3.1
android-mediaprojection
.net-4.6.2
thin
smartcontracts
promela
yii2-extension
brightcove
crypt
font-size
lync-client-sdk
color-profile
dwscript
background-service
xcb
url-pattern
deadbolt-2
probability-density
underscore.js-templating
nsfilemanager
gstreamer-0.10
fputcsv
ios4
jfugue
cannon.js
comobject
wireshark-dissector
citrus-pay
android-listview
operation
e10s
apache-commons-net
facebook-graph-api-v2.4
oracle-warehouse-builder
farseer
jmapviewer
django-nonrel
sqlperformance
sabredav
distutils
unc
windows-phone-7.1.1
osql
spring-portlet-mvc
psi
recent-documents
file-comparison
celltable
createwindow
asp.net-profiles
fixed-width
photoshop-cs4

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