Contain method to read array
I am trying to find a method to read an array like this.
Code :
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,
Code :
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.
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.
Re: Contain method to read array
Code :
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?
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.
Code :
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.
Re: Contain method to read array
Quote:
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.
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("!");
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.
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
Quote:
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?
Re: Contain method to read array
Quote:
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))