one more time about loop that doesn't work

Posted by unit on Stack Overflow See other posts from Stack Overflow or by unit
Published on 2011-02-17T23:18:52Z Indexed on 2011/02/17 23:25 UTC
Read the original article Hit count: 189

Filed under:
|

I have asked a couple of questions about this for loop:

  String[] book = new String [ISBN_NUM];
  bookNum.replaceAll("-","");
  if (bookNum.length()!=ISBN_NUM)
    throw new ISBNException ("ISBN "+ bookNum + " must be 10 characters");
  for (int i=0;i<bookNum.length();i++)
  {
      if (Character.isDigit(bookNum.charAt(i)))
      book[j]=bookNum.charAt(i);  //this is the problem right here
      j++;
      if (book[9].isNotDigit()|| 
          book[9]!="x"        ||
          book[9]!="X")
      throw new ISBNException ("ISBN " + bookNum + " must contain all digits" + 
                               "or 'X' in the last position");
  }

which will not compile. An answer I had from the other question I asked told me that the line where the error occurs is wrong in that bookNum.charAt(i) is an (immutable) string, and I can't get the values into a book array that way. What I need to do on my assignment is check an ISBN number (bookNum) to see that it is all numbers, except the last digit can be an 'x' (valid ISBN). Is this the best way to do it? If so, what the hell am I doing wrong? If not, what method would be a better one to use?

© Stack Overflow or respective owner

Related posts about java

Related posts about homework