• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pulling my hair out

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
return(error1);

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

return(numbers);
}
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joe Panully
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Joyce
[ October 29, 2004: Message edited by: Joyce Lee ]
 
Karthikeyan Rajendraprasad
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic