Ahmed February 2016

How to match user input with arraylist

package BankingSystem;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Bank {

  public static void main(String [] args){

      List<String> AccountList = new ArrayList<String>(); 
      AccountList.add("45678690");
      Scanner AccountInput = new Scanner(System.in);
      System.out.println("Hi whats your pin code?");
      AccountInput.nextLine();

      for  (int counter = 0; counter < AccountList.size(); counter++){  
          if (AccountInput.equals(AccountList.get(counter))){ //If Input = ArrayList number then display "hi"
              System.out.println("Hi");

          }
          else { //If not = to ArrayList then display "Incorrect"
              System.out.println("Incorrect");

          }
          }
      }
      }

Hi, in here I am trying to match the userInput to arrayList, if its correct then display "hi" if not display "Incorrect", for the incorrect part do I to use exception handling? and how can I get it to match the ArrayList number - 45678690?

Answers


EDEdDNEdDYFaN February 2016

First things first you need to store your user's input into some string as you currently aren't doing that.

Instead of using a counter and iterating through your list you can instead just use

AccountList.contains(the string variable assigned to AccountInput)

If it's false then the entry isn't there, otherwise it's in there. The exception handling you might want to use in this scenario would be to handle a user inputting letters instead of numbers.


Clyme February 2016

You have to store the input value in a string to check the number :

String value = AccountInput.nextLine();
if (value.equals(AccountList.get(counter))) ...


Sujit February 2016

.nextLine() returns a string which needs to be assigned to a variable .... And then compare the variable with elements in the arraylist using .contains() method ... If you also want the index position use .indexOf() method ...

String input = AccountInput.nextLine();
if(AccountList.contains(input))
      // do something
else
      // do something else


Morten Kolstad February 2016

Start variables with lower case. Names that start with upper case is for Classes only in java. So use List<String> accountList , and not List<String> AccountList .

The main problem in your code is that you are comparing the elements in list with the Scanner-object. And that will always be false. You also never store the input from the Scanner any place. You need to place the return value somewhere, like

String input = scanner.nextLine();

and compare the strings in the list to this string, not the Scanner-object.

I've added a flag so that it works correctly with multiple items in the accountList.

List<String> accountList = new ArrayList<String>(); 
    accountList.add("45678690");
    accountList.add("1");
    accountList.add("0");

    Scanner scanner = new Scanner(System.in);
    System.out.println("Hi whats your pin code?");
    String accountInput = scanner.nextLine();

    boolean listContainsInput = false;
    for  (int counter = 0; counter < accountList.size(); counter++){  
        if (accountInput.equals(accountList.get(counter))){
            listContainsInput = true;
            break;
        }
    }
    if(listContainsInput) {
        System.out.println("Hi");
    } else {
        System.out.println("Incorrect");
    }


bedbad February 2016

You are comparing the instance of the Class Scanner

Scanner AccountInput = new Scanner(System.in);

To a String:

AccountInput.equals(AccountList.get(counter))

(ArrayList.get(int) returns a String or fires an Exception)

You need to start with comparing String to String first:

AccountInput.nextLine().equals(AccountList.get(counter))

If you need additional debbuging see how both strings look like(e.q. print 'em)

Here is documentation on Scanner: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html Read it, scanner is important thing in programming languages.

Post Status

Asked in February 2016
Viewed 1,676 times
Voted 4
Answered 5 times

Search




Leave an answer