Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Contain method to read array

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Contain method to read array

    I am trying to find a method to read an array like this.

    String[] operators = {"+", "-"};
    if(operation.contains(operators)) 
    System.out.println("noob");
    else 
    System.out.println("noobcake");


    The contains method will not read from the array but it works when I use it like this,

    if(operation.contains("/") || (operation.contains("divide") || (operation.contains("division") || (operation.contains("Divide") || (operation.contains("Division")))))) 
    System.out.println(total = firstnumber / secondnumber);
    else
    System.out.println("You didn't enter a Valid Operator");

    but this uses a bunch of unnecessary space and would be easier to edit with an array.

    So my question is what method can I use to read from the array and apply to this situation?

    I started java a week ago, so I am just trying to get used to it, and write lots of test programs.


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Contain method to read array

    Any help? I would really like to get my program going, I was searching the String API Docs and only saw things pertaining to char/string nothing about array/sting.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: Contain method to read array

    if(operation.contains(operators))
    How is the contains() method of the class for operation defined? You should be able to define the contains method to take a String array.
    Are you writing the class that operation is an instance of?

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Contain method to read array

    This is how the contains method is defined

    type boolean
    method contains(CharSequence s)
    description Returns true if and only if this string contains the specified sequence of char values.

    I am not sure what you mean but here is my class.

    import java.util.Scanner;
     
    class test {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
     
    		System.out.print("Enter Operation: ");
    		String operation = sc.nextLine();
     
    		System.out.print("Enter First Number: ");
    		double firstnumber = sc.nextDouble();
     
    		System.out.print("Enter Second Number: ");
    		double secondnumber = sc.nextDouble();
     
     
    		double total = 0;
     
    		/*if (operation.contains("+") || (operation.contains("add") || (operation.contains("addition") || (operation.contains("Add") || (operation.contains("Addition"))))))
    			System.out.println(total = firstnumber + secondnumber);
    		else
    			if(operation.contains("*") || (operation.contains("multiply") || (operation.contains("multiplication") || (operation.contains("Multiply") || (operation.contains("Multiplication"))))))
    				System.out.println(total = firstnumber * secondnumber);
    			else
    				if(operation.contains("/") || (operation.contains("divide") || (operation.contains("division") || (operation.contains("Divide") || (operation.contains("Division"))))))
    					System.out.println(total = firstnumber / secondnumber);
    				else
    					System.out.println("You didn't enter a Valid Operator"); */
     
    		//test
    		String[] operators = {"+", "-"};
     
    //was trying different methods to test, equals did not show an error but it did not work like I wanted it to.
    		if(operation.equals(operators))
    			System.out.println("noob");
    		else
    			System.out.println("noobcake");
     
     
     
    	}
    }

    Thanks for taking a look at my post.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: Contain method to read array

    String operation =
    The operation variable is a String. You have to use the methods that the String class has, you can't make them up.
    If you write your own class then you can write your contains() method to take a String array or any other parameter(s) that you want.
    To test if any element of an array is in a String you could write a loop to test the String using each element in the array.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Contain method to read array

    I am looking for a method that works like "contains" but for arrays, so i want it to parse the array to see if any of the variables match with an if statement and if not return false.

    Is there no method in the string class that works this?

    String[] operators = {"+", "-"};
    if(userinputtocheck.Arraycontainsmethod(arrayname)
    System.out.println("it works!");
    else
    System.out.println("!");

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: Contain method to read array

    The fastest way to see if there is a method in the string class that works this? is to read the API doc.
    There is not a method for every possible way of using a class. You will have to write your own.

    Pass your method the String and the array and have it return true or false.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Contain method to read array

    I kind of understand what you mean, but I do not know how to go about implementing a method like this.

    Method I am trying to use from API
    public boolean contains(CharSequence s)
    Returns true if and only if this string contains the specified sequence of char values.
    Parameters:
    s - the sequence to search for
    Returns:
    true if this string contains s, false otherwise
    Throws:
    NullPointerException - if s is null
    Since:
    1.5
    public boolean containsArray(CharSequence s)
    ^
    The issue I have is here, I saw some methods in the String class that grab an Index, but I do not know how to pass an array check into the parameters.
    Can you show me an example?

    Thank you for all the help!

    What I need for my program is to have a list of variables that it checks when the user enters what operator they wish to use, would an array be the best way to do this?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: Contain method to read array

    how to pass an array check into the parameters.
    Define a method like this:
    public boolean checkIfContains(String operation, String[] operators) {


    Usage:
    if(checkIfContains(operation, operators))

Similar Threads

  1. Replies: 0
    Last Post: June 19th, 2011, 02:16 AM
  2. how to read a text delimited file using 2 dimentional array in java ??
    By pooja123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 09:11 AM
  3. Read in file and store in 2D array start of The Game of Life
    By shipwills in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2011, 08:52 AM
  4. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 08:13 PM
  5. NullPointerException when trying to read Strings into an array of myObject
    By sdivinyi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 24th, 2010, 09:45 AM