aspose file tools*
The moose likes Beginning Java and the fly likes Split a string and add the contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Split a string and add the contents" Watch "Split a string and add the contents" New topic
Author

Split a string and add the contents

Koren Washington
Greenhorn

Joined: Mar 24, 2010
Posts: 3
Hello all, i have a question. as the title says, how can i split a string and knowing the the contents of the string are like "2.98+4.17", add them? Thank you very much
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Look at String.split


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Koren Washington
Greenhorn

Joined: Mar 24, 2010
Posts: 3
so lets say my string was s, how would i split it at the plus sign?

s.split(s, '+');

?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

Koren Washington wrote:so lets say my string was s, how would i split it at the plus sign?


Did you read the link that was provided?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Koren Washington
Greenhorn

Joined: Mar 24, 2010
Posts: 3
yes i did, i put s.split(s, '+');

im just wondering how can i make it stop at the plus sign? do i have to declare a separate string for plus then do

String plus = "+";
s.split(plus);

is that what the regex is referring to?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What do you mean, "stop at the plus sign"?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Look at the link I provided again because it is clearly explained what the regex is for. There is even a link to the pattern javadoc. And you probably need the method with only 1 argument.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Non void methods return values. And be careful that "+" is a special symbol used in regular expressions.


[My Blog]
All roads lead to JavaRanch
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Koren Washington wrote:yes i did, i put s.split(s, '+');

Unlikely - that would have produced a compiler error. Perhaps you meant s.split(s, "+");?

Yeah, I know it sounds like I'm nitpicking. But the compiler is much less forgiving than I. It's important to pay attention to these details. One is a char, and one is a String. Completely different things, as far as the compiler is concerned.

Wouter Oet wrote:Look at the link I provided again because it is clearly explained what the regex is for.

Hmmm, I think it's not nearly as clear as one might like. While I agree that we should look first to the official documentation, I think that the Java docs on regular expressions are less than clear. They are precise, and correct, but not clear. To most of the human race, I think.

Christophe Verré wrote:And be careful that "+" is a special symbol used in regular expressions.

I think this is a particularly useful hint. The "+" sign has special meaning in regular expressions. If you want to refer to a character that is really a simple "+" sign, without any special meaning in the regex world, you probably need to escape it. Try "\\+" instead. And see Backslashes, escapes, and quoting for more explanation.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

Mike Simmons wrote:
Koren Washington wrote:yes i did, i put s.split(s, '+');

Unlikely - that would have produced a compiler error. Perhaps you meant s.split(s, "+");?


Actually, I think that this will compile. It is calling the split(String, int) method -- where the string itself will be passed in as the regex, and the ASCII for '+' will used as the limit.

Henry
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Ooo, you're right. Nice catch.

However, for Koren's benefit, I think everything I wrote after that still applies. Starting with:
Perhaps you meant s.split(s, "+")?

Try this first. And if it doesn't work, then try s.split("\\+").
Md Ibrahim
Greenhorn

Joined: Mar 24, 2010
Posts: 14
Hi koren,

First of all, hey guys there's a wonderful way of splitting a String in java, have you all forgotten
StringTokenizer... Its THE ULTIMATE !!! chek out below

String YourString = "2.65+4.56";
ArrayList<String> aTokenCollector = new ArrayList<String>();

StringTokenizer newTokenizer = new StringTokenizer(YourString ,"+");

while (newTokenizer.hasMoreTokens()){

aTokenCollector.add(newTokenizer.nextToken());

}

Now your aTokenCollector Arrray list contents will be

2.65
4.56

What more you need, if you want to add these two as numeric values, Type cast them to int
using Integer.parseInt("2.65") and do it.


Hey koren...hope you would love using Tokenizer,,,do try and write back..take care

By,
Ibrahim




Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

have you all forgotten StringTokenizer... Its THE ULTIMATE !!!

It's actually the old way of splitting strings, nothing ultimate here If you look carefully at the API, it says : "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. "
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Christophe Verré wrote:
have you all forgotten StringTokenizer... Its THE ULTIMATE !!!

It's actually the old way of splitting strings, nothing ultimate here

Penultimate, perhaps.

The only nice thing about StringTokenizer is that, if you don't want to use a regex, it's cool that you don't have to escape special characters.

Other than that, it kind of sucks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Split a string and add the contents