| | |
Saddly Stuck Stupid Strings
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 7
Reputation:
Solved Threads: 0
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
I tried comparing the words to numbers i.e. a=1, s=19, etc.
any help at all would be appreciated
Java Syntax (Toggle Plain Text)
package GroupAssignment; import System.*; /** * Bushra Osman */ public class Program { public static void main(String[] args) { int group; String fn, ln; Console.Write("Enter your first name: "); fn = Console.ReadLine(); Console.Write("Enter your last name: "); ln = Console.ReadLine(); Console.Write(fn+" "+ln); Console.Write(" is part of group "); if ((ln > 0) && (ln <= 9)) { group = 1; Console.Write(group); } else if ((ln >= 10) && (ln <= 19)) { group = 2; Console.Write(group); } else if ((ln >= 20) && (ln <= 26)) { group = 3; Console.Write(group); } Console.ReadLine(); } }
Last edited by ~s.o.s~; Mar 9th, 2008 at 5:13 am. Reason: Added code tags, learn to use them.
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
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.?
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.
•
•
Join Date: Mar 2008
Posts: 10
Reputation:
Solved Threads: 2
You want to compare characters, not strings
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'
Java Syntax (Toggle Plain Text)
char c = ln.toUpperCase().charAt(0) // gets the first letter in the String if (c >= 'A' && c <= 'K') group = 1;
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.
•
•
Join Date: Oct 2007
Posts: 55
Reputation:
Solved Threads: 5
Last edited by mickinator; Mar 8th, 2008 at 9:00 pm.
•
•
Join Date: Mar 2008
Posts: 10
Reputation:
Solved Threads: 2
•
•
•
•
you could use:
java Syntax (Toggle Plain Text)
if( ln.charAt(0).equalsIgnoreCase( 'a' ) ) blah;
![]() |
Other Threads in the Java Forum
- Previous Thread: Java GUI problem - "BeanBox" swing component implementation
- Next Thread: problem in bitwise logical operators
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api applet application arc arguments array arrays automation banking binary bluetooth byte chat chatprogramusingobjects class client code color compare component count database design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong problem producer program programming project projectideas read recursion reference replaysolutions rim scanner server set size sms sort sql string swing terminal threads transforms tree ui unicode validation web windows





