Java programming help Plz
Hey,
So I'm in this java class and our newest assignment is to test whether data input is valid and then do some kinda output. I'm having trouble figuring out a way to determine if a string starts with an Uppercase and is followed by lowercase (As for a first and last name). I think I need to break the string up into an array and then determine if the array[0] is uppercase, then...some other crap for lowercase, idk. Anyways, anybody know java that can tell me what I need to use to break up a string like that and then test for upper or lower case? |
No need to put them into an array - you can use str.charAt(0) and str.charAt(1) for the first and second letters.
It's a wee bit hacky but you could try checking if the first letter is uppercase by comparing it to itself in uppercase: return (str.charAt(0).equals(str.charAt(0).toUppercase()) ); The same can be done for the second letter. I think it will work. I don't have a copy of the API to hand, so I can't give you a better answer right now. |
I've had a look at the API, and I can't see another way to do it. I made a small mistake above - you can't tack .equals on the end of a char. Instead, get the first char using charAt and add it to a string, then return the result of the comparison.
This is very simple, so I have no qualms in giving you the code, but when you get to more complex things I'll just give you a push in the right direction. Code:
public class CheckCharCase { |
Another method if you want it:
Suppose you have String str. Then you could say: Character.isUpperCase(str.charAt(0)); This returns a boolean value. So you might have something like Code:
if(Character.isUpperCase(str.charAt(0))) Here are a couple of links that might help: http://java.sun.com/j2se/1.4.2/docs/...Character.html http://java.sun.com/j2se/1.4.2/docs/...ng/String.html |
I remembered that in the middle of my last lecture. :(
|
Thank you good sirs. I'm sure that'll work. I don't have time now, but tomorrow I'll finish the program and see if it compiles and runs correctly. Damn my procrastination.
|
Thank youu. The .charAt worked but I still have a problem. I need to know how many characters are in each string now so I can test each string's characters if they are lowercase. This is what I have so far for that portion...but I get a String Index Out Of Bounds Exception:
Quote:
|
Quote:
You can use the String method length() to determine how many characters there are in a string object. For instance, you may want to use something like for(int i=0; i < LastName.length(); i++) to iterate through each character in the string LastName. |
If you want to see if all the characters in a string are lowercase, for String str, then you can do the following
Code:
if(str.compareTo(str.toLowerCase())!=0) |
Thanks, I realized I could use the .length like ten minutes after posting this Lol. I finished the program and it works nicely. Thanks for your help
|
All times are GMT -6. The time now is 11:51 PM. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.