error while trying .charAt( )

here is the portion of my program that matters. all the variables have been declared already so just go with that. The point of this program is to read in a binary number (like 010011) and then 1) check that is actually binary 2) convert to decimal.

In this part of the code I want it to read in a string, and then read out a specific character so that I can examine it with a if ( ) statement to see if it is a 0 or a 1.

Now I get a exception error everytime I run and I know it is from the charAt part because I dont have a normal intiger inside the ( ).

String binaryNumber;

int numberOfDigets = 1;

char specificCharacter;

Scanner scan = new Scanner(System.in);

System.out.println("Input a valid binary number.");

binaryNumber = scan.next();

numberOfDigets = binaryNumber.length();

specificCharacter = binaryNumber.charAt( numberOfDigets );

System.out.println(specificCharacter);

if you have any idea how i can get this to work or why it isnt I need all the help I can get

p.s. if you need more clarification on what I am doing /going to do I will tell all :P

[1157 byte] By [sadrobota] at [2007-10-2 16:53:25]
# 1
wow I got it now. After some reading I was reminded that charAt starts counting at 0 and .length( ) starts at 1 doh
sadrobota at 2007-7-13 18:05:30 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

This a little beyond your question, but it sounds like you're trying to ensure the string contains only ones & zeroes before converting it. If you have some flexibility, a simpler way to do this is to jsut read in the string, then call Integer.parseInt(theString, 2). If it throws a parse exception, it's invalid.

Much easier than trying to write your own parser.

Dick_Adamsa at 2007-7-13 18:05:30 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3
> Much easier than trying to write your own parser.We hope it is not a homework or an assignment to reproduce that functionality as an exercise.
BIJ001a at 2007-7-13 18:05:30 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

for check whether its a proper binary number use this String method

binaryNumber.matches("[01]+")

where binaryNumber is ur input string .

it returns false if the input does not represent a binary num otherwise it returns false.

but beware this matches() method is not there till jdk1.3 i guess...

then converting it to decimal , i think u already know it .

Nabakumara at 2007-7-13 18:05:30 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...