Results 1 to 12 of 12
  1. #1
    jumoo is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default if statement using an array. please help! :(

    In this code, I want to check whether the ward name is available or not.

    import java.util.*;
    public class test
    {
    private String arrayWard [] = {"a","b","c","d","e"}; //This is my ward array

    public test()
    {
    test(); // this is to call the method
    }

    public void test()
    {
    Scanner in = new Scanner (System.in);
    System.out.println("please put a/b/c/d/e");
    String wardName = in.next();
    char input = wardName.charAt(0);
    int x = input -97;
    if (wardName == arrayWard[x])
    {
    System.out.println("This ward does exist");
    }
    else
    {
    System.out.println("This ward doesn't exist");
    }
    }
    }

    but whenever I put a/b/c/d/e. The result will be this ward doesn't exist. What's wrong with my code?

    Thanks for any help :D
    Last edited by jumoo; 08-24-2011 at 06:11 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    This is probably the most common problem that n00bs have. Do not compare objects with ==, use the equals method instead.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I should add that there is a small percentage of the time you do want to use == to compare objects but this is not one of them.

  4. #4
    jumoo is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    cool! thanks thanks! I tried with equals and it works! :D

  5. #5
    jumoo is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    but hey, when I tried to put any other alphabets, it doesn't show the
    System.out.println("This ward doesn't exist");
    it just automatically open my code, do you know why?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by jumoo View Post
    it just automatically open my code
    I have no idea what you mean by that.

  7. #7
    jumoo is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    oh ,it just doesn't work when I typed any other alphabets. Doesn't even show the
    System.out.println("This ward doesn't exist");

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    When you get errors when you compile or run your code you should copy and paste the exact and full error message and post it here.

    Java Code:
    int x = input -97;
    if (wardName == arrayWard[x])
    What if user enters 'A' which has an ascii value of 65? The value of x will be -32 (65 - 97). Then you try to access the array at index -32 which obviously doesn't exist. If you want to test if a character entered by a user is in the array or not then you will need to find another solution. Hint: how would you do it with pen and paper?

  9. #9
    jumoo is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    So, the ward name must be one of the value of arrayWard isn't it? Sorry I am still newbie. DO you put limitation on the code? by using for loop?
    the error code is java.lang.ArrayIndexOutOfBoundsException:

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You could use a loop to compare each value in the array against the user input. Or you could put the values in String instead of the array and use a method of the String class to see if the user input is there.

  11. #11
    jumoo is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default

    I don't quite understand how you put the value in String. Can you give me an example? Thanks heaps for your help :D

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Yes you do. You know what a String is. If not you have some serious revision to do immediately.
    Java Code:
    String values = "abcde";
    Then you can use a method of the String class to see if a 't' or a 'a' or a '*' is located in the
    String. Go and read what methods the String class has in the Java API and see which one will help.

    Note that I am deliberately not telling you what the answer is so that you can work it out for yourself and learn.

Similar Threads

  1. Replies: 3
    Last Post: 08-08-2011, 05:07 PM
  2. using an if statement to populate an array
    By MrJinx in forum New To Java
    Replies: 3
    Last Post: 04-30-2011, 12:01 PM
  3. "not a statement" error to array declaration
    By SpaceMonkey in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 11:01 PM
  4. If Statement in SQL
    By Steffi1013 in forum JDBC
    Replies: 6
    Last Post: 04-10-2010, 03:19 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •