Results 1 to 12 of 12
- 08-24-2011, 06:03 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
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 :DLast edited by jumoo; 08-24-2011 at 06:11 AM.
- 08-24-2011, 06:13 AM #2
This is probably the most common problem that n00bs have. Do not compare objects with ==, use the equals method instead.
- 08-24-2011, 06:14 AM #3
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.
- 08-24-2011, 06:16 AM #4
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
cool! thanks thanks! I tried with equals and it works! :D
- 08-24-2011, 06:18 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
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?
- 08-24-2011, 06:20 AM #6
- 08-24-2011, 06:21 AM #7
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
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");
- 08-24-2011, 06:28 AM #8
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.
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?Java Code:int x = input -97; if (wardName == arrayWard[x])
- 08-24-2011, 06:40 AM #9
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
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:
- 08-24-2011, 06:43 AM #10
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.
- 08-24-2011, 06:48 AM #11
Member
- Join Date
- Aug 2011
- Posts
- 8
- Rep Power
- 0
I don't quite understand how you put the value in String. Can you give me an example? Thanks heaps for your help :D
- 08-24-2011, 06:54 AM #12
Yes you do. You know what a String is. If not you have some serious revision to do immediately.
Then you can use a method of the String class to see if a 't' or a 'a' or a '*' is located in theJava Code:String values = "abcde";
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
-
Strange behaviour of If statement inside an Array derived from ResultSet
By matthelazy in forum New To JavaReplies: 3Last Post: 08-08-2011, 05:07 PM -
using an if statement to populate an array
By MrJinx in forum New To JavaReplies: 3Last Post: 04-30-2011, 12:01 PM -
"not a statement" error to array declaration
By SpaceMonkey in forum New To JavaReplies: 3Last Post: 11-25-2010, 11:01 PM -
If Statement in SQL
By Steffi1013 in forum JDBCReplies: 6Last Post: 04-10-2010, 03:19 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks