• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Adding strings together  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to add two strings together, but I am trying to add the second string to the first string only if the second String's size is less than 5 and is not blank. I need to fill in the blanks to make the code work.

public class Strings {
public static void main(String[] args) {
String firstString = "Something";
String secondString = "Help";
if(secondString.____ ______ 5 ___ ____ secondString.equals("")){
firstString _____ secondString;
}
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then what is problem? use length() method of string class to calculate length of String. Use < , &,! operators. And to add string use' +='
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to Javaranch . If you are facing still problem then let us know.
 
Garrett Leon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's awesome! thanks I'm glad to be here!

So now i have

if(secondString.length(firstString) < 5 ______ ____ secondString.equals("")){
firstString + secondString;

I'm not sure what to put in these two blanks.

 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this

 
Garrett Leon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well for my assignment I have to complete the program with the blanks provided, which makes it much more difficult :/
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My previous post has complete program. What missing in above program?
 
Garrett Leon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where it says

(firstString)<5 & ____ secondString.equals

theres still a blank that's supposed to go there.
I'm getting an error on that line that says there aren't any arguments.
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanner look carefully at code it has"!".
 
Garrett Leon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gives me an error when I insert the exclamation mark.
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error? The same program I ran without any error. Copy above program and run it.
 
Garrett Leon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome it works! I have just become a very large fan of JavaRanch.
Thank you very much for your time.

Just for future reference, is there anything else that could
go in place of the exclamation mark?
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tanner Reed wrote:Awesome it works! I have just become a very large fan of JavaRanch.
Thank you very much for your time.



Welcome .

Your Query says:

add the second string to the first string only if the second String's size is less than 5 and is not blank



And your code is like

So there must be "!".
 
Garrett Leon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works for me! Thanks again
 
Sheriff
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use && instead of &. Logically it means the same (only execute the body if both are true) but & will always evaluate both operands, whereas && will ignore the second if the first one is already false.

An example:
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:I would use && instead of &. Logically it means the same (only execute the body if both are true) but & will always evaluate both operands, whereas && will ignore the second if the first one is already false.

An example:



+1
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!