• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Whats wrong here? (small code sample)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Jakalwarrior

Member
Joined
Apr 5, 2004
Location
Louisiana
Im trying to teach myself if else statements so I wrote a short example proggy just to play and learn. I thought I had it but it dosent work. It always reverts to the else?

public class Brain {


String name;
String location;
String age;
String sex;


public void person() {
name = JOptionPane.showInputDialog("Hey there, whats your name?");
location = JOptionPane.showInputDialog("well " + name +" where ya from?");
age = JOptionPane.showInputDialog("well ****, I dont know anything about " + location +". anywho, how old did you say you are?");
sex = JOptionPane.showInputDialog("well " + name +" are you a boy or a girl");
if (sex == "Boy") {JOptionPane.showMessageDialog(null, "sorry but I only talk to females");}
else if (sex == "boy") {JOptionPane.showMessageDialog(null, "sorry but I only talk to females");}
else if (sex=="girl") {JOptionPane.showMessageDialog(null, "*Well thats nice","A girl?",JOptionPane.PLAIN_MESSAGE);}
else if (sex=="Girl") {JOptionPane.showMessageDialog(null, "Well thats nice","A girl?",JOptionPane.PLAIN_MESSAGE);}
else {JOptionPane.showMessageDialog(null, "no speaky english?");}
System.exit(0);
}
}
 
The problem is that the String object sex and the String objects "Boy", etc, don't refer to the same places in memory. In this context, == is used to check if two objects are the same, not if they have the same contents. For your code, you want something like sex.equals("Boy") .

Edit: Also, the
Code:
 tags are your friend.  Short of a script to generate correctly-highlighted code, they're the best way to increase code readibility.
 
jack222 said:
Um, what language did you write that in?
Sorry, is java. What Christoph said worked though so problem solved and I learned something.
I took the intro class, found out I like it, and now it feels like they're going soooo slow. Learning at home means I got noone to bug when im stumped though.... except google and you guys :D
 
Only that it exists lol. Half the fun for me is figuring stuff out. Piddling around till the lil red squiggles in Jbuilder go away lol.
 
Jakalwarrior said:
Only that it exists lol. Half the fun for me is figuring stuff out. Piddling around till the lil red squiggles in Jbuilder go away lol.
When your starting out, you might want to write in notepad...just for a little. JCreator indents and does a lot for you...In notepad, it doesn't so you don't get lazy and stuff....its what my teacher told me to do.
 
if you want your code to be organized so you debug easly just do it as blocks of {} so you know where this block
or that block starts and end with all variables and values related to that specific block. in small programs you wont find
it that much but when code goes complex and long
i.e
Code:
public class test
{
	int tmp;
	
	private void getTmp()
	{
		Return tmp;
	}
	private int setTmp(int x)
	{
		tmp = x;
	}
	Private void loops()
	{
		for(int i=0 ; i<90 ; i++)
		{
			while(tmp != 10)
			{
				for(int j=0 ; j>6 ; j--)
				{
				}
			}
		}
	}
}
 
Last edited:
Instead of notepad, let me reccomend you try out vim (www.vim.org) -- it's got a lot of features such as highlighting, auto-indenting (ick), and plenty of other stuff such as a full regular expression engine. It's a swiss army knife and, whatever your editor of choice, it beats out notepad ;)
 
Hagios said:
Instead of notepad, let me reccomend you try out vim (www.vim.org) -- it's got a lot of features such as highlighting, auto-indenting (ick), and plenty of other stuff such as a full regular expression engine. It's a swiss army knife and, whatever your editor of choice, it beats out notepad ;)
In the spirit of the eternal battle between vi and emacs, I'm going to have to recommend emacs:
http://www.gnu.org/software/emacs/emacs.html
It also has syntax highlighting, and can do just about whatever you want. You can even use it as an irc client:
http://www.emacswiki.org/cgi-bin/wiki?EmacsIRCClient
 
from here
When I use an editor, I don't want eight extra KILOBYTES of worthless
help screens and cursor positioning code! I just want an EDitor!!
Not a "viitor". Not a "emacsitor". Those aren't even WORDS!!!! ED!
ED! ED IS THE STANDARD!!!
 
Eeeeewwww... Sorry Christoph. But Ed reminds me way too much of edlin. Call me M$ infected, but when it comes to commandline editing, I haven't found anything as nice as edit ;)

JigPu
 
For educational purposes, the free environment BlueJ, while unimpressive from a "real work" standpoint, may be run on any platform with a Java Runtime Environment, and natively in OS X and Win32. BlueJ matches curlies/brackets/parens and does pretty print, which is nice (sort of).

ROFL @ "ED IS THE STANDARD TEXT EDITOR!" :D
 
Gnufsh said:
In the spirit of the eternal battle between vi and emacs, I'm going to have to recommend emacs:
Eighteen Megs And Constantly Swapping. ;)

ed makes my skin crawl. And I've attempted to generate files through nothing but cli regexp/string processor utilities. It's a fun 'project' if you're really, REALLY bored and can't risk being caught playing DooM ;)
 
Im forced to use Jbuilder in the class im taking so I try to get used to it. The auto completion stuff and self correction stuff is nice but i'ts indention scheme bugs the heck out of me. I spend more time moving the indents where I want them than actually coding (I could personalize it but they comps get whiped and reset to imagines every now and then). It dosent make JAR files either so its pretty lame. At home Ive got jbuilder, Jcreator, and NetBeans. Depending on what mood im in but I guess ill try some of the ones you guys are talking about too. Maybe I can find one that suits me without the annoyances and lack of features in jbuilder.
 
Jakalwarrior said:
Im forced to use Jbuilder in the class im taking so I try to get used to it. The auto completion stuff and self correction stuff is nice but i'ts indention scheme bugs the heck out of me. I spend more time moving the indents where I want them than actually coding (I could personalize it but they comps get whiped and reset to imagines every now and then). It dosent make JAR files either so its pretty lame. At home Ive got jbuilder, Jcreator, and NetBeans. Depending on what mood im in but I guess ill try some of the ones you guys are talking about too. Maybe I can find one that suits me without the annoyances and lack of features in jbuilder.
I am sure the indention scheme is actually set to an industry standard. It might be a good idea to get used to it.
 
Back