Saddly Stuck Stupid Strings

Thread Solved

Join Date: Nov 2006
Posts: 7
Reputation: bmanoman is an unknown quantity at this point 
Solved Threads: 0
bmanoman bmanoman is offline Offline
Newbie Poster

Saddly Stuck Stupid Strings

 
0
  #1
Mar 7th, 2008
I'm trying to create a program where it takes the first character of the Last name and place the person in a group due to the name.
I tried comparing the words to numbers i.e. a=1, s=19, etc.
any help at all would be appreciated
  1. package GroupAssignment;
  2. import System.*;
  3. /**
  4.  * Bushra Osman
  5.  */
  6. public class Program
  7. {
  8. public static void main(String[] args)
  9. {
  10. int group;
  11. String fn, ln;
  12. Console.Write("Enter your first name: ");
  13. fn = Console.ReadLine();
  14. Console.Write("Enter your last name: ");
  15. ln = Console.ReadLine();
  16. Console.Write(fn+" "+ln);
  17. Console.Write(" is part of group ");
  18. if ((ln > 0) && (ln <= 9))
  19. {
  20. group = 1;
  21. Console.Write(group);
  22. }
  23. else if ((ln >= 10) && (ln <= 19))
  24. {
  25. group = 2;
  26. Console.Write(group);
  27. }
  28. else if ((ln >= 20) && (ln <= 26))
  29. {
  30. group = 3;
  31. Console.Write(group);
  32. }
  33.  
  34. Console.ReadLine();
  35. }
  36. }
Last edited by ~s.o.s~; Mar 9th, 2008 at 5:13 am. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: Saddly Stuck Stupid Strings

 
0
  #2
Mar 7th, 2008
You cannot compare string with number, the way you are doing it. however you can compare strings with equals() or equalsIgnoreCase() methods of String.

What is it that you try to compare, e.g. 0 to 9 , 10 to 19 and etc...? you are trying to compare a last name say, Smith with 20.?
Last edited by new_2_java; Mar 7th, 2008 at 3:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 10
Reputation: Zork'nPalls is an unknown quantity at this point 
Solved Threads: 2
Zork'nPalls Zork'nPalls is offline Offline
Newbie Poster

Re: Saddly Stuck Stupid Strings

 
1
  #3
Mar 8th, 2008
You want to compare characters, not strings
  1. char c = ln.toUpperCase().charAt(0) // gets the first letter in the String
  2. if (c >= 'A' && c <= 'K') group = 1;
although you can compare characters to integers, its better to compare characters to other characters because it makes your code more readable

also make sure to use the toUpperCase() or toLowerCase() methods because 'A' doesn't equal 'a'
Last edited by Zork'nPalls; Mar 8th, 2008 at 12:45 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 55
Reputation: mickinator is an unknown quantity at this point 
Solved Threads: 5
mickinator mickinator is offline Offline
Junior Poster in Training

Re: Saddly Stuck Stupid Strings

 
0
  #4
Mar 8th, 2008
you could use:

  1. if( ln.charAt(0).equalsIgnoreCase( 'a' ) )
  2. blah;
Last edited by mickinator; Mar 8th, 2008 at 9:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 10
Reputation: Zork'nPalls is an unknown quantity at this point 
Solved Threads: 2
Zork'nPalls Zork'nPalls is offline Offline
Newbie Poster

Re: Saddly Stuck Stupid Strings

 
0
  #5
Mar 8th, 2008
Originally Posted by mickinator View Post
you could use:

  1. if( ln.charAt(0).equalsIgnoreCase( 'a' ) )
  2. blah;
sorry, but that first line of code doesn't even compile because you tried to call the equalsIgoreCase method on a char. You can't call any methods on chars
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 7
Reputation: bmanoman is an unknown quantity at this point 
Solved Threads: 0
bmanoman bmanoman is offline Offline
Newbie Poster

Re: Saddly Stuck Stupid Strings

 
0
  #6
Mar 17th, 2008
Thank you for the help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC