Help!! in Java!

Discussion in 'Web Design and Development' started by vendettabass, Nov 1, 2006.

  1. vendettabass macrumors 6502a

    vendettabass

    Joined:
    Jul 8, 2006
    Location:
    Seoul, South Korea
    #1
    Having so much difficulty with Java at uni at the moment, basically I've gotta create a book library service kind of thing... One of the questions I have is,

    staff user ids start with the three characters 'stf' all student user ids start with the three characters 'mcs'. Modify your code so that counts are taken of how many staff and how many students have borrowed this book.

    now I'm really lost.. I have a field for usrID, which is the users ID and so far I've come up with

    if (usrid.substring(0,3) = stf )
    {
    usrType = staff;
    }

    else
    {
    usrType = student
    }

    usrType is also another field....

    its completely wrong and isnt compliling, can someone please point me to which bit is wrong?!

    thanks a lot!!
     
  2. Thom_Edwards macrumors regular

    Joined:
    Apr 11, 2003
    #2
    first off, i don't know if the code you posted is an exact copy/paste, but i will assume that it is....

    your first line should be more like
    Code:
    if (usrid.substring(0,3) = "stf")
    so that stf is in quotes.

    next, setting usrType to a string (which would also need to be wrapped in quotes) is not the way to do this. it should be more like...
    Code:
    {
    staff++;
    }
    
    else
    {
    student++;
    }
    you want to add 1 to the variable that is holding the number of people that have checked out the book. the way you are doing it is not counting at all. instead, it is just setting the usrType to a string which gets written over each time the conditional is run. also, you're missing a ; at the end of one of your lines.

    start with that and see where it takes you...
     
  3. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #3
    An assignment as an if() condition?

    Using == won't work, either. You need String.compareTo() or String.compareToIgnoreCase().

    So ...
    Code:
    java.lang.String s1 = usrId.substring(0.3);
    
    if(s1.compareToIgnoreCase("stf") == 0)
    {
      // do something.
    }
    
    else if(s1.compareToIgnoreCase("mcs") == 0)
    {
      // do something
    }
    
    else
    {
      // error? 
    }
     
  4. Thom_Edwards macrumors regular

    Joined:
    Apr 11, 2003
    #4
    chris is right. i totally overlooked that. too much php (which still would have used ==)...
     
  5. Compile 'em all macrumors 601

    Compile 'em all

    Joined:
    Apr 6, 2005
    #5
    Better yet, use String.equals().

    Example
    ----------
    String h = "hello";
    if (h.equals("hello))
    System.out.println("They are equal");
     
  6. vendettabass thread starter macrumors 6502a

    vendettabass

    Joined:
    Jul 8, 2006
    Location:
    Seoul, South Korea
    #6
    cheers for the help guys!! macrumors rule! I think i got the problem sorted OK now!

    thanks again!
     

Share This Page