Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accepting a String and a String Array in just ONE Method  RSS feed

 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a really dumb question! But i thought why not give it a try...

Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.


I have started on it and I know it is wrong, could someone please help me. Give me an answer and a explanation, that would be really great because Im so confused at the moment, Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You're supposed to create a method that takes two parameters. The parameters must be a String[] (string array) and a String.

At the moment your class has three methods:

  • main, which takes one parameter, a String[]
  • Printhelloworld, which takes no parameters
  • printArrays, which takes one parameter, a String[]

  • So, how do you think can you create a method that takes a String[] and a String?

    See this tutorial: Passing Information to a Method or a Constructor.
     
    Mark Do
    Ranch Hand
    Posts: 70
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ummm......I dont know..!

    I am thinking of making just a



    ???
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No. That is not what is required.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mark Do wrote:ummm......I dont know..!

    I am thinking of making just a



    ???


    Perhaps, you should read the tutorial linked by Jesper. At the very least, you need to to understand how to create a method that takes more than one parameter.

    Henry
     
    Shubham Semwal
    Ranch Hand
    Posts: 176
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are you learning from a book ?? try either some book or tutorials
     
    Paweł Baczyński
    Bartender
    Posts: 2085
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mark Do wrote:I am thinking of making just a


    I am nitpicking. I know. But the compiler will do that too. It should not be string[]. It should be String[].
    Remember, Java is case sensitive language.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mark Do wrote:ummm......I dont know..!
    I am thinking of making just a

    Question: Forget arrays for the moment. What would you do if the question asked you to write a method that takes a String and an int? Or two Strings?

    If you don't know how to do that, then you are not ready for this problem; and probably (as Henry says) need to read the tutorials.

    And don't worry: you'll get there; just maybe not today.

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!