Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes pulling my hair out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pulling my hair out" Watch "pulling my hair out" New topic

pulling my hair out

Joe Panully

Joined: Oct 10, 2004
Posts: 22
I am writing a program that takes a string of numbers, and runs it though the following method to make sure that it has exactly seven digits and I know this part of the method works. It makes sure there aren't 1's or 0's in the number, and I know this part of the method works. Lastly, I am trying to make it make sure that the only things in the number are infact numbers, if they aren't I want it to error. I cannnot for the life of me see why it always errors on that, even if I put in a completely valid string. I know the other parts work perfectly, however when I put in a normal test string it errors because of the part that checks to make sure there aren't other anything other than digits in it.

public String test () {

String error1="Error type1";
String error3="Error type3";
int length=numbers.length();

if(length<7 || length >7)

for(int i=0; i<length; i++){
char number=numbers.charAt(i);
if(number =='0' || number =='1')
if(number!='2' || number!='3' || number!='4' || number!='5' || number!='6' || number!='7' || number!='8' || number!='9')

Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
small change will solve it
thie following thing

should be like

to work correctly.

bcause if the number is 3 and when the condition checks number!='2' it will return true and throw an error.

hope you get what i come to say

Karthikeyan<br />SCJP 1.4, SCWCD.
Joe Panully

Joined: Oct 10, 2004
Posts: 22
Jesus I am an iditot. I was thinking literally in terms of, "well it should be wrong if it isn't a 2 or 3 or 4..." but yeah I'm just dumb. Thanks.
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
You could also make sure it has only digits by attempting to parse the entire String to a double (useing a double because 7 is a lot of digits). If this attempt throws an exception you know you have characters in the String that are not numerical.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Joe,

You can use regular expression to simplify the code:

For example:

For more info about regular expression, you can check out Sun Java tutorial: Regular Expressions.

[ October 29, 2004: Message edited by: Joyce Lee ]
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
dont feel bad for making simple mistakes..

try to learn from it and dont repeat...

always try to trace the flow... and here in this case you would have found it within seconds.
Fendel Coulptz

Joined: Sep 23, 2004
Posts: 24
hi, u can also test whether the character is a digit by using a method

isDigit(char ch)
Java API link for isDigit(char ch)

its shorter and more elegant.
I agree. Here's the link:
subject: pulling my hair out
It's not a secret anymore!