File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Adding strings together Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding strings together" Watch "Adding strings together" New topic
Author

Adding strings together

Garrett Leon
Greenhorn

Joined: Mar 31, 2011
Posts: 7
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

Joined: Jul 15, 2008
Posts: 629

Then what is problem? use length() method of string class to calculate length of String. Use < , &,! operators. And to add string use' +='


Life is easy because we write the source code.....
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

And Welcome to Javaranch . If you are facing still problem then let us know.
Garrett Leon
Greenhorn

Joined: Mar 31, 2011
Posts: 7
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

Joined: Jul 15, 2008
Posts: 629

Try this

Garrett Leon
Greenhorn

Joined: Mar 31, 2011
Posts: 7
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

Joined: Jul 15, 2008
Posts: 629

My previous post has complete program. What missing in above program?
Garrett Leon
Greenhorn

Joined: Mar 31, 2011
Posts: 7
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

Joined: Jul 15, 2008
Posts: 629

Tanner look carefully at code it has"!".
Garrett Leon
Greenhorn

Joined: Mar 31, 2011
Posts: 7
It gives me an error when I insert the exclamation mark.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

What error? The same program I ran without any error. Copy above program and run it.
Garrett Leon
Greenhorn

Joined: Mar 31, 2011
Posts: 7
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

Joined: Jul 15, 2008
Posts: 629

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

Joined: Mar 31, 2011
Posts: 7
Works for me! Thanks again
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

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
 
 
subject: Adding strings together
 
Similar Threads
String array
class As A Data Type
Could anyone point out why the do while will not terminate
Need quick summary of == and equals()
Help please !!