To Space or not to space?

Discussion in 'Mac Programming' started by larswik, Oct 15, 2006.

  1. larswik macrumors 68000

    Sep 8, 2006
    I'm back at learning Java after a week of brain rest. Just for fun tonight I wrote a simple IF statement using the ! (Not). Which people were kind enough to explain for me last week.

    I wrote this code (With out the books help:) )

     import java.util.Scanner;
    public class theif
    	static Scanner sc = new Scanner(;
    	public static void main(String [] agrs)
    		System.out.print("Pick a number ");
    		int n = sc.nextInt();
    		if (n ! = 6)
    			System.out.println("That was not 6");
    			System.out.println("Your number was " + n );
    When I compiled it in the Termial it spit out an error when it came to the ! part. Long story short when I remove the space between the ! = so it looks like this != the program compiles and runs fine.

    In the first chapter in the Java for Dummies it said that java didn;t care about spaces and I could write code one letter or number per line. But when I leave a space between those two it won't compile and errors.



  2. dukebound85 macrumors P6


    Jul 17, 2005
    5045 feet above sea level
    well i think for sequences of keys such as for not equal you cant have a space. same with <= or >= and any other logical operators that make use of 2 keys. i think the spaces are not important in cases such as this

    if(statement) and if( statement ) are the same

    thats my thought may or may not be right lol
  3. iMeowbot macrumors G3


    Aug 30, 2003
    That's because even though they have related meanings, ! and != are actually different operators - if you think about it, you don't use them in quite the same contexts. It's sort of the same way that = and == are different things. Even though these operators are written as punctuation, the compiler treats them more like words.

    While you get a lot of formatting freedom, there are limits. "wh ile" won't act as a substitute for "while" for the same reason.
  4. larswik thread starter macrumors 68000

    Sep 8, 2006
    I get it programers joke NOT, ha~!

    I think you explained it and it cliked when you wrote it != is a logic operator. when those 2 are together it is like (n not equal to 6) but that space between them made that not not work since it was not a not logic statement anymore.

    I must say this board is fast. I type a problem in an not more the 10 minutes later there is an answer even 2 in the morning.

    Thanks!!! <---(That was not 3 NOT logic operators, it was happieness)

  5. Palad1 macrumors 6502a


    Feb 24, 2004
    London, UK
    Still sounds like a perl joke to me, sorry mate.

  6. savar macrumors 68000


    Jun 6, 2003
    District of Columbia
    No, the book didn't say that. Java doesn't care about white space in between keywords, operators, and symbols. But within one of those tokens, you can not have white space. != is an operator. When you separate them with a space, you're using two operators: ! and =. One is a unary operator, the other is an assignment operator, and they don't make sense in that order, so Java gives you an error.
  7. mufflon macrumors 6502

    Sep 15, 2006
    It's nice you're taking your learning of this language slowly - by slowly I don't mean dumb - but smart! I've seen so many people who begin programming (who might want to develop a specific application just like you (stated in another thread I believe)) - who gets carried away and makes something way beyond their level and doesn't understand why their code doesn't work even on logical terms, so just take it slow and move slowly towards you goal - the actual implementation once you get the hang of it is usually very easy.
  8. larswik thread starter macrumors 68000

    Sep 8, 2006
    Thanks, I think I use to be one of those people who went to fast and got frustrated. After a while you learn that's not the way. I looked over that part of the book again and it did talk about 'white spaces' and he wrote a simple code which had no logic operators. I do understand that now.

    I want to jump to page 505 to start on Swing it looks more fun then the Catching Exceptions section that I am in now.

    You are right about learninng things. I can sit down and look at a piece of code from the book and see what it is doing line by line. But even though I can see what is happening when I close the book and try to write that some program by my self, it's hard. Simple if programs I can do book closed but not the guessing game examples.

    Thanks again for all your help!

  9. rtharper macrumors regular


    Sep 6, 2006
    Oxford, UK
    GUI programming is evil. Not hard, evil. Event driven programming is the bane of a programmer's existence. It makes a distraction from writing actually FUNCTIONALITY to make it look pretty.

    Bitter UI designer of otherwise functional commandline-based Statistical Machine Translation tools.
  10. larswik thread starter macrumors 68000

    Sep 8, 2006
    I affraid to jump to the GUI until I have a good background of everything leadign up to it. I'm in Chapter 2 right now where it covers programing basics like variables, logic operators, if else and much more.

    Chapter 3 is called Object Oriented Programing.

    Chapter 4 Strings, Arrays and collections.

    Chapter 5 the much waited for SWING!

    Chapter 6 Web Programing which I think I might skip over since I am not planing on creating web pages.

    Chapter 7 Files and Databases which Imight need to learn to store the IP addresses user / pass info to the hard drive so I don't always have to type it in.

    So I should learn the chapters leading up to Chapter 5.



    Chapter 8 Fun and games. I think I will skip this one as well.
  11. mwpeters8182 macrumors 6502

    Apr 16, 2003
    Boston, MA
    I second this. As someone who's building a GUI-driven application for the first time, after doing mostly numeric programming, I feel your pain.

Share This Page