• Post Reply Bookmark Topic Watch Topic
  • New Topic

arraylist  RSS feed

 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a arraylist which parses the string into

hconr 9.0Qgggg 4.0f...
ArrayList List = parser.fcomp:
I did for loop and put all the value in one obj
for(inti=0;i<List.size:i++)
{

Object obj = List.get(i);

}

But I need to get only floating values and sum it up. How would I do it?

Could pleas help me in this case

Thanks
 
Gladwin Burboz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like your original string is "". What is format of this string? Fixed-length/Space-deliminated/other

I am not sure here if you are trying to parse this string in List or you already have it parsed in a list. If you have already parsed it in a list then what are the contents of this list?
 
Paul Brabban
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, don't know if this is helpful, but I'd parse your input string like so

public static void main(String[] args) {
// set up a scanner - there are other constructors
// for other types of input device than literal String
Scanner s = new Scanner("hconr 9.0Qgggg 4.0f...");
// may want to refine this regex but it works for your sample string
s.useDelimiter("[a-zA-Z ]");
float total = 0f;
while(s.hasNext()) {
if (s.hasNextFloat()) {
// if it's a float, get it and add to the total
float nextFloat = s.nextFloat();
System.out.println("Found float: " + nextFloat);
total += nextFloat;
} else {
// skip it
s.next();
}
}
System.out.println("Total: " + total);
}

you'll need to import java.text.Scanner - which may be a new addition to Java 5?

Cheers,
 
Paul Brabban
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops, code tags again...

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!