Zelaron Gaming Forum  
Stats Arcade Portal Forum FAQ Community Calendar Today's Posts Search
Go Back   Zelaron Gaming Forum > The Zelaron Nexus > Science and Art > Tech Help

 
 
Thread Tools Display Modes

 
Java programming help Plz
Reply
Posted 2010-02-25, 05:56 PM
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?
"Ban shampoo, demand real poo"
Old
Profile PM WWW Search
Slyvr enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzSlyvr enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Slyvr
 



 
Reply
Posted 2010-02-25, 06:48 PM in reply to Slyvr's post "Java programming help Plz"
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.
Old
Profile PM WWW Search
Lenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basics
 
 
Lenny
 



 
Reply
Posted 2010-02-26, 05:12 AM in reply to Lenny's post starting "No need to put them into an array - you..."
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()));		
	}
}

Last edited by Lenny; 2010-02-26 at 05:14 AM.
Old
Profile PM WWW Search
Lenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basics
 
 
Lenny
 



 
Reply
Posted 2010-02-26, 06:32 AM in reply to Lenny's post starting "I've had a look at the API, and I can't..."
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

Last edited by Demosthenes; 2010-02-26 at 06:38 AM.
Old
Profile PM WWW Search
Demosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to be
 
Demosthenes
 



 
Reply
Posted 2010-02-26, 10:46 AM in reply to Demosthenes's post starting "Another method if you want it: ..."
I remembered that in the middle of my last lecture.
Old
Profile PM WWW Search
Lenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basicsLenny simplifies with no grasp of the basics
 
 
Lenny
 



 
Reply
Posted 2010-02-27, 11:15 PM in reply to Lenny's post starting "I remembered that in the middle of my..."
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.
"Ban shampoo, demand real poo"
Old
Profile PM WWW Search
Slyvr enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzSlyvr enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Slyvr
 



 
Reply
Posted 2010-02-28, 12:54 PM in reply to Slyvr's post starting "Thank you good sirs. I'm sure that'll..."
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.
"Ban shampoo, demand real poo"
Old
Profile PM WWW Search
Slyvr enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzSlyvr enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Slyvr
 



 
Reply
Posted 2010-02-28, 07:56 PM in reply to Slyvr's post starting "Thank youu. The .charAt worked but I..."
Slyvr said: [Goto]
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.

Last edited by Knight Sir Rick; 2010-02-28 at 08:10 PM.
Old
Profile PM WWW Search
Knight Sir Rick enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzKnight Sir Rick enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Knight Sir Rick
 



 
Reply
Posted 2010-03-01, 11:27 AM in reply to Slyvr's post starting "Thank youu. The .charAt worked but I..."
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.
Old
Profile PM WWW Search
Demosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to be
 
Demosthenes
 



 
Reply
Posted 2010-03-02, 09:56 AM in reply to Knight Sir Rick's post starting "You can use the String method length()..."
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
"Ban shampoo, demand real poo"
Old
Profile PM WWW Search
Slyvr enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzSlyvr enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Slyvr
 
 

Bookmarks

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules [Forum Rules]
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming Faq Demosthenes Tech Help 4 2004-10-25 04:43 PM
Jeeze Ppl Plz Post! I)ark_Phoenix General Gaming 6 2002-05-05 05:04 PM
plz help me araider Diablo I & II 11 2002-04-06 02:16 AM
Programming GameCube Tech Help 7 2002-03-12 12:23 PM


All times are GMT -6. The time now is 10:07 AM.
'Synthesis 2' vBulletin 3.x styles and 'x79' derivative
by WetWired the Unbound and Chruser
Copyright ©2002-2008 zelaron.com
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.