Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
String/String Array: if function
guenay oezcan
Greenhorn
Posts: 2
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi folks,
My question is:
(I shortened the code, but you will get the point)
String s; String []a={("a"),("b"),("c"),("d")}; System.out.printf("Please input a letter: "); s=scan.nextLine(); if(s.equals([]a) //The Goal is to search through the if-Function only?? the values in the array[..] and compare them with the normal string. { System.out.printf("Yay :P"); } else { System.out.printf("Didn't work out :/"); } ..
I didn't find out yet, how i can manage that to work..
I hope the right way, however, just requires an editing inside the if(..) part :P
Joel Christophel
Ranch Hand
Posts: 250
1
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
First off, welcome to the ranch!
The way we easily gain access to the values of an array is through the use of an enhanced for loop.
String[] array = {"a", "b", "c", "d"};
To check the equality of
s
with the values of the array you could use an enhanced for loop like this:
for(String index : array) { if(s.equals(index)){ System.out.println("Yay :P"); break; } }
Here's an explanation of enhanced for loops:
http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/enhanced-for-loops.html
Bear Bibeault
Sheriff
Posts: 67746
173
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
By the way,
if
is not a function. It's a statement.
[
Asking smart questions
] [
About Bear
] [
Books by Bear
]
guenay oezcan
Greenhorn
Posts: 2
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thank you both for the advice
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Java Help: NullPointerException
ArrayIndexOutOfBoundsException that I just can't figure out.
get numeric character representation for Polish unicode characters
How to populate array with user input
system properties
More...