1 2 Previous Next 15 Replies Latest reply on Oct 29, 2006 12:39 AM by 807598

    Two problems - Greater than and character for the word character

    807598
      First off, sorry about the title.

      public void setRefNumber (String ref)
      {
      if (ref >= "2")
      refNumber = ref; // Asks the user to input the reference number.
      else System.out.println("The reference number you entered was less than three characters long");
      }

      That is the code I have, I am looking at making it so the user has to input a 'reference number' at least 3 characters long, if they do it accepts the value. If not it returns the error message.

      I want to say "If ref is greater than 2 characters long" but I'm struggling on how to word it. I've tried putting > in but obviously it won't work with a String, and also how do I set it so it knows if it's more than 2 characters long, because at the moment it's just greater than 2.

      Sorry if that's very longwinded. Help appreciated.
        • 1. Re: Two problems - Greater than and character for the word character
          807598
          ref.length() will give you the number of characters in the string.
          • 2. Re: Two problems - Greater than and character for the word character
            807598
            Thanks.

            How would I put that in? as reNumber has been decalred in my fields as a String, as it will have numbers and letters in.

            public void setRefNumber (String ref)
            {
            if (ref.length(2))
            refNumber = ref; // Asks the user to input the reference number.
            else System.out.println("The reference number you entered was less than three characters long");
            }

            Or should I be putting it where I have, because if so it won't compile or should it be after setRefNumber?

            Sorry, I'm new to this.
            • 4. Re: Two problems - Greater than and character for the word character
              807598
              String.length() returns an int value, compare that with however long you need it to be.
              • 5. Re: Two problems - Greater than and character for the word character
                807598
                Thanks a lot. Worked exactly.

                No doubt will be back soon with a few more problems as I carry on, will jsut use this thread.

                Thanks again.
                • 6. Re: Two problems - Greater than and character for the word character
                  807598
                  Also, I have a method that when a user click it, it adds a value to the total:
                  public void Borrow()
                  {
                  borrowed = borrowed +1;
                  }

                  borrowed is declared a int in the field at the top and in the constructor it starts as 0. So when the user click the method Borrow it adds another value to it, I then made a accessor called getBorrowed which returns the value.

                  I now need to edit this so that when a user click the Borrow method to in theory borrow the book it check whether the book is available or not. If it is then it allows the book to be borrowed and updates the borrowed field. If it isn?t available I?ll get ti to show a message saying so.

                  I?ve also been told to use Boolean types in this. Could someone give me a couple of pointers as I?m not too sure how to get start?
                  • 7. Re: Two problems - Greater than and character for the word character
                    807598
                    Make a boolean variable called available and set it to true. When some one borrows the book, set it to false.
                    • 8. Re: Two problems - Greater than and character for the word character
                      807598
                      Right cheers, will have a play with that.

                      Thank you.
                      • 9. Re: Two problems - Greater than and character for the word character
                        807598
                        public void Borrow()
                        {
                        if (available = true)
                        borrowed = borrowed +1;
                        available = false;
                        else return "Book currently unavailable";
                        }

                        It says "else without if". But as you can see if is there. I have an almost identical method above it set out the same way but no error message?

                        Is it to do with the boolean type? Or have I just mucked up?
                        • 10. Re: Two problems - Greater than and character for the word character
                          807598
                          > if (available = true)
                          You're confusing = and ==. What that line of code does is to first make available be true and then check whether it's true. It's never necessary to compare with a boolean literal: simply use
                          if (available)
                          It says "else without if". But as you can see if is there.
                          Because you don't have any braces, only one statement is taken as the body of the if. It's equivalent to
                           if (available) // (I've corrected this line, so it's not quite equivalent)
                          {
                              borrowed = borrowed +1;
                          }
                          available = false;
                          else return "Book currently unavailable";
                          The intervening assignment means that the else can't be matched to the if. You need to add braces:
                           if (available)
                          {
                              borrowed = borrowed +1;
                              available = false;
                          }
                          else return "Book currently unavailable";
                          Once you've done that you'll find further problems with returning a value from a void method.

                          Have you come across the Java Tutorial? I think you'd find it helpful. http://java.sun.com/docs/books/tutorial/
                          • 11. Re: Two problems - Greater than and character for the word character
                            807598
                            Once you've done that you'll find further problems with returning a value from a void method.
                            Yeah, I changed it to a println which is what it needed to be anyway.

                            And no I've not seen the tutorial. Will bookmark that and have a read tomorrow when my brain is in gear, too late now.

                            Thanks for your help today, been much appreciated.
                            • 12. Re: Two problems - Greater than and character for the word character
                              3004
                              Yeah, I changed it to a println which is what it
                              needed to be anyway.
                              In general, it's better to separate the business logic from the display. That is, a method that does some work or calculation generally is not responsible for displaying the results.

                              So rather than having that method print the message, it might be better to have it return a boolean indicating whether the borrow was successful. The caller of that method can then decide what to do if it was not--print an error message to the console, send an email, display an alert, etc.

                              This is a small program, you're just beginning, and you're getting a lot thrown at you at once, so it's not a big deal right now. It's just something to be aware of and keep in mind as your classes and programs grow more complex.
                              • 13. Re: Two problems - Greater than and character for the word character
                                807598
                                So rather than having that method print the message, it might be better to have it return a boolean indicating whether the borrow was successful. The caller of that method can then decide what to do if it was not--print an error message to the console, send an email, display an alert, etc.
                                Not exactly sure how this would be done but I understand the meaning in it, no doubt it'll come clearer in time.

                                Thanks again.
                                • 14. Re: Two problems - Greater than and character for the word character
                                  3004
                                  So rather than having that method print the message,
                                  it might be better to have it return a boolean
                                  indicating whether the borrow was successful. The
                                  caller of that method can then decide what to do if
                                  it was not--print an error message to the console,
                                  send an email, display an alert, etc.

                                  Not exactly sure how this would be done but I
                                  understand the meaning in it, no doubt it'll come
                                  clearer in time.

                                  Thanks again.
                                  Currently:
                                  blah.borrow(); // borrow prints out "already checked out" 
                                  Preferred:
                                  boolean borrowed = blah.borrow(); // borrow doesn't print, but returns true or false
                                  if (!borrowed) {
                                      // print  out "already checked out'
                                  }
                                  
                                  ...
                                  
                                  public boolean borrow() {
                                      if (available) {
                                          borrowed++;
                                          available = false;
                                          return true;
                                      }
                                      else { // always use braces, even when there's only one statement
                                          return false;
                                      }
                                  } 
                                  
                                  OR
                                  
                                  public boolean borrow() {
                                      boolean wasAvailable = available;
                                      if (available) {
                                          borrowed++;
                                          available = false;
                                      }
                                  
                                      return wasAvailable;
                                  } 
                                  A couple other points:

                                  * The member variable borrowed keeps track of how many times it was borrowed, yes? If so, I'd give it a more desriptive name like checkoutCount or something. "Borrowed" sounds like it's about whether it's ever been borrowed or is currently borrowed--a boolean.

                                  * Method names and variable names conventionally start with lowercase. Hence borrow() rather than Borrow(). Class names start with uppercase.
                                  1 2 Previous Next