Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding strings together

 
Garrett Leon
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;
}
 
Pramod P Deore
Ranch Hand
Posts: 632
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: 632
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: 632
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: 632
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: 632
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: 632
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: 632
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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
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: 632
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic