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 7 of 7

Thread: Checking if input value is in an array

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Checking if input value is in an array

    Hi
    So I'm trying to make a somewhat simple Android application.
    A user enters a value in a numberbox.
    The program checks if that number is in an array.
    If it is, in a textbox it says "Yes" if it isn't, in the same text box it says "No"




    public class MainActivity extends AppCompatActivity {
     
        private Integer enterDate; private Button btnCheck; private TextView resVal;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
     
            enterDate = (Integer) findViewById(R.id.enterDate);
            btnCheck = (Button) findViewById(R.id.btnCheck);
            resVal = (TextView) findViewById(R.id.resVal);
     
            btnCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
     
                    Integer[] array = {1,3,5,7};
                    Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
                    boolean enterDate = set.contains(enterDate);
                    if(enterDate) {
                        resVal.setText("Yes");
                    }else {
                                resVal.setText("No");
                            }
                        }
                }
        }});

    I'm getting three errors:

    error: ')' expected
    error: illegal start of type
    error: reached end of file while parsing

    They all point me to the last line of code
    Attached Images Attached Images
    Last edited by tetanusot; October 9th, 2018 at 06:10 PM.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    279
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Checking if input value is in an array

    You are declared enterDate as integer, so just initialized it like this
    int enterDate = 0

    Below code is not needed.
    enterDate = (Integer) findViewById(R.id.enterDate);
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Oct 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checking if input value is in an array

    Quote Originally Posted by John Joe View Post
    You are declared enterDate as integer, so just initialized it like this
    int enterDate = 0

    Below code is not needed.
    enterDate = (Integer) findViewById(R.id.enterDate);
    Thanks for your reply.
    I deleted this line
    enterDate = (Integer) findViewById(R.id.enterDate);
    but where do I put this?
    int enterDate = 0
    and by the way, this enterDate variable is actually the number that I want to get from input in the numberbox

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    279
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Checking if input value is in an array

    and by the way, this enterDate variable is actually the number that I want to get from input in the numberbox
    Then you need to add an editText, and then assign the input to enterDate variable when button is clicked.

    See the example below

    public class MainActivity extends AppCompatActivity {
     
        private Button btnCheck;
        private TextView resVal;
        private EditText editText;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
     
            editText = (EditText) findViewById(R.id.editText);  // create an editText
     
            btnCheck = (Button) findViewById(R.id.btnCheck);
            resVal = (TextView) findViewById(R.id.resVal);
     
            btnCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
     
                    Integer[] array = {1, 3, 5, 7};
                    Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
     
                    String enterDate = editText.getText().toString();    // get the input from editText, then assign to enterDate variable
                    int value = Integer.parseInt(enterDate);
     
                    boolean enterDate1 = set.contains(value);
                    if (enterDate1) {
                        resVal.setText("Yes");
                    } else {
                        resVal.setText("No");
                    }
                }
     
            });
        }
    }
    Last edited by John Joe; October 10th, 2018 at 03:38 AM.
    Whatever you are, be a good one

  5. #5
    Junior Member
    Join Date
    Oct 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checking if input value is in an array

    Quote Originally Posted by John Joe View Post
    Then you need to add an editText, and then assign the input to enterDate variable when button is clicked.

    See the example below

    public class MainActivity extends AppCompatActivity {
     
        private Button btnCheck;
        private TextView resVal;
        private EditText editText;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
     
            editText = (EditText) findViewById(R.id.editText);  // create an editText
     
            btnCheck = (Button) findViewById(R.id.btnCheck);
            resVal = (TextView) findViewById(R.id.resVal);
     
            btnCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
     
                    Integer[] array = {1, 3, 5, 7};
                    Set<Integer> set = new HashSet<Integer>(Arrays.asList(array));
     
                    String enterDate = editText.getText().toString();    // get the input from editText, then assign to enterDate variable
                    int value = Integer.parseInt(enterDate);
     
                    boolean enterDate1 = set.contains(value);
                    if (enterDate1) {
                        resVal.setText("Yes");
                    } else {
                        resVal.setText("No");
                    }
                }
     
            });
        }
    }
    This returns an error
    error: cannot find symbol variable editText


    I just changed the ID attribute in content and it worked.
    I really appreciate your help.
    Thanks a TON!
    Last edited by tetanusot; October 10th, 2018 at 06:36 AM.

  6. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    279
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Checking if input value is in an array

    You''re welcome
    Whatever you are, be a good one

  7. #7
    Junior Member
    Join Date
    Nov 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Checking if input value is in an array

    Please check the last line where you have put a curly brace and a semi colon.

Similar Threads

  1. 2D array input
    By Rain_Maker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 13th, 2013, 07:16 PM
  2. [SOLVED] Return an array of Pairs that have the same length as the input array of Strings.
    By hemla in forum Object Oriented Programming
    Replies: 3
    Last Post: August 8th, 2013, 08:17 AM
  3. Input into array.
    By Saintroi in forum What's Wrong With My Code?
    Replies: 22
    Last Post: May 18th, 2012, 08:12 AM
  4. BufferedReader.read() blocks,when checking if there's any input for some URLs
    By redateksystem in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: March 27th, 2012, 09:33 AM
  5. checking array list items!!! urgent help please
    By hacikho in forum Java Theory & Questions
    Replies: 1
    Last Post: December 14th, 2011, 04:28 PM