Zelaron Gaming Forum

Zelaron Gaming Forum (http://zelaron.com/forum/index.php)
-   Tech Help (http://zelaron.com/forum/forumdisplay.php?f=329)
-   -   Java programming help Plz (http://zelaron.com/forum/showthread.php?t=50074)

Slyvr 2010-02-25 05:56 PM

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?

Lenny 2010-02-25 06:48 PM

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.

Lenny 2010-02-26 05:12 AM

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 {

        public static void main(String[] args) {
                System.out.println("" + getFirst("Hello"));        //true
                System.out.println("" + getFirst("hello"));        //false
                System.out.println("" + getSecond("Hello"));        //true
                System.out.println("" + getSecond("HEllo"));        //false
        }

        private static boolean getFirst(String s) {
                String str = "" + s.charAt(0);
                        //get the first char of the string, put into a string
                return(str.equals(str.toUpperCase()));
                        //compare and return
        }
       
        private static boolean getSecond(String s) {
                String str = "" + s.charAt(1);
                return(str.equals(str.toLowerCase()));               
        }
}


Demosthenes 2010-02-26 06:32 AM

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)))
{
    // do something
}
else
{
    // do something else
}

.

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

Lenny 2010-02-26 10:46 AM

I remembered that in the middle of my last lecture. :(

Slyvr 2010-02-27 11:15 PM

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.

Slyvr 2010-02-28 12:54 PM

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:

for (i=0; i<30; i++){
if (Character.isLetterOrDigit(LastName.charAt(i))){
LastChar_count++;
}
else{
break;
}
}
for (i=0; i<30; i++){
if (Character.isLetterOrDigit(FirstName.charAt(i))){
FirstChar_count++;
}
else{
break;
}
}
I just need to count the number of characters.

Knight Sir Rick 2010-02-28 07:56 PM

Quote:

Originally Posted by Slyvr (Post 686916)
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:


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.

Demosthenes 2010-03-01 11:27 AM

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)
{
    //  do this if the string is not lower case
}
else
{
    // do this if string is lower case
}

No loops necessary.

Slyvr 2010-03-02 09:56 AM

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 08:47 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.