Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vendettabass

macrumors 6502a
Original poster
Jul 8, 2006
895
2
Wellington, New Zealand
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!!
 
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...
 
your first line should be more like
Code:
if (usrid.substring(0,3) = "stf")
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? 
}
 
Better yet, use String.equals().

Example
----------
String h = "hello";
if (h.equals("hello))
System.out.println("They are equal");
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.