Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Strings (need help, won't take long at all for those who know what they are doing)
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
renaissance




PostPosted: Sun Oct 05, 2008 9:04 pm   Post subject: Java Strings (need help, won't take long at all for those who know what they are doing)

Here is my code:



c.print ("Are you male or female (m/f)? ");
gender = c.readLine ();
while (true)
{
if (!(gender.equals (male)) || !(gender.equals (female)))
{
c.println ("Please enter 'm' if you are male, or 'f' if you are female.");
gender = c.readString ();
}
else
break;
}



I am asking the user to enter "m" if they are male and "f" if they are female. I want to compare the string they enter with the characters "m" and "f" to make sure they entered on of those characters. Will someone explain how to do this? Thanks.

I know this code will achieve what I want...but how do I do it with the format above?




c.print ("Are you male or female (m/f)? ");
gender = c.readLine ();
while (true)
{
if (gender.equals ("m") || gender.equals ("f"))
break;
else
{
c.println ("Please enter 'm' if you are male, or 'f' if you are female.");
gender = c.readString ();
}
}


Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Sun Oct 05, 2008 10:32 pm   Post subject: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

What will the following do?
code:
String var = "a";
if (var.equals("a")) {
     System.out.println("Yes");
} else {
     System.out.println("No");
}

If you understand that small piece of code, you should understand what to do to solve your problem.

Edit: Ugh my Java skills are getting rusty.
syntax_error




PostPosted: Mon Oct 06, 2008 12:26 am   Post subject: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

Just a general point: never use breaks horrible way to program.
Euphoracle




PostPosted: Mon Oct 06, 2008 2:24 pm   Post subject: Re: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

syntax_error @ Mon Oct 06, 2008 12:26 am wrote:
Just a general point: never use breaks horrible way to program.


What about when you want to prevent a fall through with a switch structure? Breaks are pretty useful then.
wtd




PostPosted: Tue Oct 07, 2008 8:12 am   Post subject: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

Then use a better language? Wink
S_Grimm




PostPosted: Tue Oct 07, 2008 7:00 pm   Post subject: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

umm. did anyone else notice that said individual who started this thread is NOT using regular java? They are using Ready to Program by Holtsoft. just thought i'd throw that out there. the code will still work for him, but if you try to compile it using anything else, you get errors galore. Holtsoft programs suck.

edit: if you try to compile rennisence code you get an error.
Clayton




PostPosted: Tue Oct 07, 2008 9:27 pm   Post subject: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

So just change 'c' into "System.out" The logic still applies.
S_Grimm




PostPosted: Wed Oct 08, 2008 7:39 am   Post subject: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

I know. just pointing it out for anyone else who reads this and trys to compile it.
Sponsor
Sponsor
Sponsor
sponsor
renaissance




PostPosted: Wed Oct 08, 2008 8:04 pm   Post subject: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

Hmm...well...I figured out an alternative...so all is working Smile. Didn't realize that using Holtsoft's Ready To Program program would be that bad :i...oh well...that's what I'm stuck with for my course so I'll just have to learn it Smile Thanks for everyone's help...and side conversations ^.^ rather entertaining.
S_Grimm




PostPosted: Thu Oct 09, 2008 8:07 am   Post subject: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

when you do program with Ready, avoid the Console C stuff. use System.out.print or System.out.println
there universal.

edit: Sorry, forgot to capitalize the System.out
wtd




PostPosted: Thu Oct 09, 2008 11:20 am   Post subject: RE:Java Strings (need help, won\'t take long at all for those who know what they are doing)

Please no. Mindless copying and pasting of code so that the program "just works" is the path to the dark side.

Understand the code you're writing.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: