Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830
this week in the
Programmer Certification
forum!
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
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Java in General
Minor Detail - for(String b: sa).. what's the 'b' for?
Jill Snider
Greenhorn
Posts: 6
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi all!
New to the forum and I hope someone can explain this minor detail to me.
class StringSplit { public static void main(String [] args) { String s = "x1234 y56 z7 a"; String [] sa = s.split("\\d"); // The \d means that every digit is a terminator int count = 0; for(String b : sa) count++; System.out.println("total: " + count); } }
It's that darn 'b' in the for loop.
I can change it to any value but am not sure what it's purpose is?
Thanks for your help!
Jill
Paul Clapham
Sheriff
Posts: 28333
97
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
It's the name of the
String
variable which can be used inside the loop to refer to each of the String objects in the iteration. Example (note the use of the Code tags and please use them next time):
for(String b: sa) { System.out.println("This string in upper-case is " + b.toUpperCase()); count++; System.out.println("total: " + count); }
And welcome to JavaRanch.
Campbell Ritchie
Marshal
Posts: 79987
399
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the Ranch
Always use the
code button
when you quote code (but you can't use coloured code). I have edited your post and you can see how much better it looks.
Jill Snider
Greenhorn
Posts: 6
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Paul and Campbell,
Thanks for your help.
Of course the 'b' was for each String object!
I did something stupid whilst trying to print it out.
Thank you for the clarity.
Also, I will use the code format next time I post.
Thanks again, Jill
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Sun's New Sample Q&A. Java 2 Question 1
Dan's: Thread Q 39
How to count duplicate entries in one column
doubt in new exam question
String Tokenizing doubt
More...