-3

Hello I am new to Java and had a quick question about looking for the name of a String in an ArrayList. Here is the code I wrote:

import java.util.*;
public class Rubix{

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String input;
        String FRS="U (R U' R')";
        String RBS="y' U' (R' U R)";
        String FLD="y' (R' U' R)";
        ArrayList RA = new ArrayList();
        RA.add(FRS);
        RA.add(RBS);
        RA.add(FLD);

        System.out.println("Name the situation.");
        input=sc.nextLine();
        for(int i=0; i<RA.size(); i+=1){
            if (input==RA.get(i)){
                System.out.println(RA.get(i));
            }
        } 
    }
}

What I am asking for help with is writing the if statement. I want to test to see if that name for the String is within the ArrayList. I know what I have currently is wrong so please help.

2
  • if (RA.contains(input)), also, = is assignment, not comparison Commented Jan 5, 2016 at 2:43
  • 1
    Possible duplicate of Java ArrayList search Commented Jan 5, 2016 at 2:43

5 Answers 5

1

You can try :

If (input.equals(RA.get(i)))

Also at end of for statement it should be I++ not I+=1

Sign up to request clarification or add additional context in comments.

Comments

1

When you add an item to a list, you're not adding the variable itself to the list to be referenced by name later. Rather, you're copying the value of that variable into the list.

In your case, after

RA.add(FRS);

RA does not contain the text "FRS". It has no idea what "FRS" is. What it DOES contain, is the value of FRS, in this case, a String with the value "U (R U' R')", stored in the first position (index 0) of the list.

It also looks like you're trying to take an input and match it against the list of values in the array. But this line:

if (input=RA.get(i)){

has a bug. A single "=" is the assignment operator, used to assign values on the right to variables on the left. So in your case, when the code executes this if check, it's actually ASSIGNING the value of RA.get(i) into input, overriding the input value. Assignments return "true" if they succeed, and since this one can't fail, it will always be true.

When comparing strings in java, you need to use the ".equals()" method that every String object has. So in your case, your line should read:

if (input.equals(RA.get(i)){

Comments

0

1. Loop through 2. Compare arraylist using contains method

for (int i = 0; i < list.size(); i++) {
 System.out.println(list.get(i));
 if (list.contains(inputstring)) { 
// You have found a mach! 
}
 }

The contains method just checks whether your list contains the string which is put into the paramater. You call the method on your array[list] object.

Comments

0

This is how your code should be like :

private static Scanner sc;

public static void main(String[] args) {
    sc = new Scanner(System.in);
    String input;
    String FRS="U (R U' R')";
    String RBS="y' U' (R' U R)";
    String FLD="y' (R' U' R)";
    ArrayList<String> RA = new ArrayList<String>();
    RA.add(FRS);
    RA.add(RBS);
    RA.add(FLD);


    System.out.println("Name the situation.");
    input=sc.nextLine();
    for(int i=0; i<RA.size(); i= i+1){
        if (input.equals(RA.get(i))){
            System.out.println(RA.get(i));
        }
    }


}

Comments

-1

ArrayList has a contains method, so just do this:

if (RA.contains(input)) {
  // do stuff
}

2 Comments

doesn't the contain method just search through the list the String in the arrayList and not the objects holding the strings. And sorry for some of the errors, I kinda made this in like a minute just so I could ask this question
The ArrayList is holding Strings, not another kind of object. You should probably make this explicit in your constructor: ArrayList<String> RA = new ArrayList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.