Need Java code Clarification

Discussion in 'Mac Programming' started by larswik, Sep 29, 2006.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    Hey, I am reading about looping methods right now and I understand what it is doing but there are 2 parts that I’m not clear about and it would be great if some one can explain it to me. Here is the basic part of it

    Code:
     {
          int  number = 2;
          while (true)
            {
                if (number == 12);
                   break;
         System.out.print (number + “ “ );
               Number += 2;
                  System.out.println();
             }
    }
    I don’t understand what the “ “ means in the (number + “ “ ); Normally the quotes are used to enter some test that will be printed on the screen?

    The other part of what I don’t get is the line under ‘number += 2; ‘ I know this line from reading the explanation adds 2 to the number variable since it counts up in even numbers. But the way it is written confuses me. Should it not be something like ‘ number = number + 2 ‘ why is it ‘+=’

    Thanks for the help.

    -Lars
     
  2. neoserver macrumors 6502

    Joined:
    Apr 24, 2003
    #2
    I'm assuming that the author of the code wanted to have a space added to then end of the number?
    " " is a space in quotes so it will add a space to the end of the number.
    += is short form for number = number +2

    programmers are lazy :p

    Hope this helps
     
  3. Grover macrumors member

    Joined:
    May 14, 2004
    #3
    There are syntax problems in your sample code. Try this:

    Code:
    
    	public static void main(String[] args)
    	{
    		int  number = 2;
          	
    		while (true)
            {
                if (number == 12)
                   break;
    			
    			System.out.print (number + " ");
               
    			number += 2;
                System.out.println();
             }
    	}
    
    The " " simply adds a blank space. So if the output looks like:

    2
    4
    6
    8

    The if you instead of the space had an "a" it would look like:

    2a
    4a
    6a
    8a

    and so on.

    The += operator does exactly what you suggest. It's just shorthand for that notation. Assuming i is equal to 1, the lines:

    i = i++;

    and

    i = i + 1;

    will both equal 2. Although you'll want to read the documentation carefully to understand the proper use of +=. If you don't feel comfortable with it, you don't have to use it.
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    Ok so the " " just creates a space. and the += is the same as 'i = i + 2'

    He didn't explain in the book the += thing and that confused me.

    Thanks for the explination!

    -Lars
     
  5. lmalave macrumors 68000

    lmalave

    Joined:
    Nov 8, 2002
    Location:
    Chinatown NYC
    #5
    Just to be clear on what the poster was saying above:

    n += 2; is the same as n = n + 2; n += 5; is the same a n = n + 5; etc.

    So += can be thought of as the "increment by" operator.

    Also, just a stylistic note: it's generally poor programming to have a while(true) and then a break within that loop. That kind of defeats the way the syntax was intended to be used. The same program could be written like this:

    int number = 2;
    while (number < 12)
    {
    System.out.println (number + “ “ );
    number = number + 2;
    }

    or, better yet, with a for loop:

    for (int number = 2; number < 12; number = number + 2)
    {
    System.out.println (number + “ “ );
    }

    Try the above code, you'll see gets the exact same result!! Also, you'll note that only a single System.out.println() is needed, because a println() is the same as print() except it appends a carriage return at the end.
     
  6. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #6
    You can't use
    Code:
    System.out.println(number);
    directly since System.out.println doesn't take integers (or floats, booleans etc). The "" (usually an empty "" is used, but " " works too) is a trick to change the input to a String. What's between the brackets is evaluated as a String before being used inside the method.

    Although, what's happening in that code looks like it is supposed to a line of numbers and the space separates them. But the thing about printing numbers is valid.
     
  7. Kunimodi macrumors member

    Joined:
    Sep 8, 2006
    Location:
    Ashland, OR, USA
    #7
    Actually, it is safe to use println with numbers: PrintStream JavaDoc.

    You will often see "" + num as it is more convenient than Integer.toString(num), etc. The '+' as a concatenation operator for String is the only operator overload that I know of (and String can't be extended) -- damn those hypocrites ;). For complex String generation, it is often more efficient to use StringBuilder (or StringBuffer).
     
  8. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    It was just for a part in the book that talked about using the WHILE for creating a loop and how to insert a BREAK to stop the loop.

    Thanks for the further explination though.

    -Lars
     
  9. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #9
    Just a side note: the = in compound form also works with other operators too. So if x = 12, then:

    x += 2 makes x 14
    x -=2 makes x 10
    x *= 2 makes x 24
    x /= 2 makes x 6
    x %= 5 makes x 2 (modulo, division remainder)
    x <<= 2 makes x 48 (1100 bit shifted two spaces left = 110000 = 48)
    x &= 7 makes x 4 (bitwise AND, 1100 AND 0111 = 0100 = 4)

    etc.
     
  10. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    I better stick to the book for now but good to know. I need to learn to drive before I put fuzzy dice on my mirror : )

    -Lars
     
  11. Kunimodi macrumors member

    Joined:
    Sep 8, 2006
    Location:
    Ashland, OR, USA
    #11
    Lars, is Java your first programming language? Have you thought about something easier (and funner) to work in? The creation of Java involved some pretty zealous ideologies that definitely dosn't represent the majority opinion of experienced developers (what a fight against arrogance adding reflection, generics, autoboxing, printf, etc has been). My point is that Java doesn't always teach well rounded programming skills and techniques.
     
  12. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #12
    My goal is not to become a programer. I have a video production company where we create videos that play in hotels. I am looking for a better way to use FTP and TELNET to upload and communitace with the units we have in the hotels.

    The Terminal in OS X has both the FTP and TELNET function. So I deceded to learn Java so I could build my own interface to automate uploads and deleting of files as well as controling the untis using TELNET commands to run play lists, play, stop and see if they are running.

    So I think Java is the right App for me since it is internet based.

    Thanks,

    -Lars
     
  13. Kunimodi macrumors member

    Joined:
    Sep 8, 2006
    Location:
    Ashland, OR, USA
    #13
    Thanks for the exposition, Lars; it's interesting. Are you interested in Java mainly for the ability to make nice GUIs then? Could what you are describing be (readily) done using a web page (perhaps with AJAX or Flash)?
     
  14. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #14
    I am looking for a GUI interface for these things. I have used flash before but it was in the late 90's and I think it was Flash 3 back then. Never heard of Ajax before so I don't if it would work or not.

    I need to upload / Download and delete files from the hotels. As well as create a TELNET area where I can type commands and see the responses from the untis. I need to enter in the IP's user/pass and save that information to a file so I don't need to type that in. Also when I connect to a hotel I want that hotel to automaticly be linked to a local folder on my hard drive where I have the new spots to upload to it so I don;t have to dig around for the folder.

    Things like that. I don't know how much programing experience I will need for this. I am looking forward to the 'Swing' section in the book (page 505) where we get into the GUI. Right now I am learning about IF ELSE (page 175). I would like to jump ahead to p505 but I am sure what I am learning now is important to suppport the GUI.

    But that is my mission and it is noce to know you people are hear to answer my trivial questions : )

    -Lars
     
  15. lmalave macrumors 68000

    lmalave

    Joined:
    Nov 8, 2002
    Location:
    Chinatown NYC
    #15
    Which language(s) would you recommend? I'm a professional programmer and I actually wish Java had been my first language. My first language was LISP (which was a neat language but a totally different paradigm than most widely used languages). My second language was C (which is a pain in the ass compared to Java), and my 3rd language was a version of C++ with some Java features built in (garbage collection, iterators, etc.). Then finally I learned Java.

    I would say that at least of the languages I've tried (which includes Visual Basic, TCL, JavaScipt, Flash Actionscript, various Unix shell scripting languages, and that's all I can remember off the top of my head...), Java has been the easiest and most fun. Plus you can use it for almost anything: script-like programming (just the main() function), Web programming (JSP), "Windows" programming, etc. Which ones are funner and/or easier? I know that Python and Ruby are more popular now. But I looked at one of them (I think it was Ruby), and it was NOT easier to learn. It seemed to emphasize how you could do a gazillion things in a few lines of code, which is NOT good programming in my opinion!!! Any programmer can write code that a computer can understand - good programmers write code that PEOPLE can understand. And on that front I think Java does very well...
     
  16. Kunimodi macrumors member

    Joined:
    Sep 8, 2006
    Location:
    Ashland, OR, USA
    #16
    Hi, lmalave. I'm surprised that you found Java easier than JavaScript or say Bash (now Korn shell maybe ;)). People like different things and if you find Java the funnest then more power to you. It certainly is a versatile language. People have individual styles and preferences and Java fits the bill for some folks. However, I note that you found Java easy after learning C and C++ and I have to wonder (particularly if you found C difficult) if you would have found it so easy to learn as a first language.

    I understand what you mean about writing few lines of cryptic code. These overly clever puzzles can be a maintenance nightmare and few languages bring this to my mind as much as Perl, but Ruby definitely allows this as well. Its style is more diverse than, say, Python, but I think its main 'flaw' is that the English documentation and examples for it are still sorely lacking. Particularly when trying to learn a new language, having good documentation is important. Still, ruby has some nice qualities that reward the dedicated learner.

    As a first language, I think Python is hard to beat. It can start off very, very simple and grow to show some very complex techniques in pretty solid ways.
     
  17. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #17
    Lars, have you considered hiring a consultant to do this work for you?
     
  18. lmalave macrumors 68000

    lmalave

    Joined:
    Nov 8, 2002
    Location:
    Chinatown NYC
    #18
    Learning to program in Javascript would be interesting in that the Browser environment already gives you objects that you can manipulate. But at the same time it's confusing because you have interaction between HTML elements on the page and Javascript. So it's kind of assuming you know how HTML and the Document Object Model works before you even get into how you can modify them at runtime in Javascript.

    I can see how scripting languages like bash would be an easy way to learn conditionals and looping structures, but I guess my opinion of those I've seen too many incomprehensible Unix scripts...

    So Python is the only one I might agree with: I've heard good things about it so I should look into it.


    In terms of how I learned to program: I didn't learn to program until I took a class in college, and in a classroom environment I definitely think Java is good because you can learn the object-oriented way of thinking from the start. So I don't think it's just that Java was easier for me because I already knew C and C++. I mean, I know several Unix shell scripting languages, and each one is somewhat esoteric and just as crappy as the previous one I learned, even though they're somewhat similar. I mean, Java was *designed* to be easy. The basic features were designed by James Gosling in 3 days or so less than 15 years ago, and it was geared toward rectifying things that were confusing or hard to implement in other languages...
     
  19. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #19
    I didn’t want to hire someone for this. I have time on my hands since I have no kids and I’m not married, and at age 35 I prefer to stay home in the evenings rather then head to the local bar in my small town of Solvang. I thought it would be a good time to take up a hobby, learn Java.

    Learning new things keeps the brain fresh and I have you guys as teachers when I don’t understand something. If I needed this program in a hurry I would have hired someone to write it for me. But I think I can learn enough to create a GUI interface that calls on Unix commands for FTP and TELNET to work with my systems.

    Again my goal is not to be a programmer, just a hobbyist : )

    -Lars
     
  20. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #20
    Java has a bit of a learning curve to it, I can say from experience. My AP Computer Science class was strictly Java, and the teacher just moderated the class. We were given no outside help, so learning the language for me was definetly not fun.

    I think though, the main thing is getting into a "programmer's mindset" and thinking like a computer programmer, rather than learning the symantics. Learning C++ is much easier, compared to when I learned java because many of the same principles are recurring, it's just that it's a different language that I have to use to do the same thing (loops, declaring variables, etc.... )
     
  21. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #21
    1) These aren't looping "methods". Methods are named pieces of code that you can call from other parts of your code. It's good to keep words like this straight in your head, it will curtail your confusion. Those looping words might be called "keywords". You can refer to the loops themselves as blocks, e.g.: "while block" or "if-then-else block".

    2) The quotes are used to convert the number into a string. This is kind of a lazy way to do it, but it's okay to do when you're writing some test code or proof-of-concept code. Using the + operator with a number and a string, the + operator acts as a string concatenation operator -- this is syntactical sugar -- so that "mark " + "haase" = "mark haase", 2 + " marks" = "2 marks", and 123 + "" = "123". Hope that makes sense.

    3) Yes, you're right. number += 2 is just a shorthand for number=number+2, because its such a common operation that you might want to save a few keystrokes.
     
  22. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #22
    You are right. I ment 'Method' in the way that I was learning different types of ways to use a loop function.

    I have to be carful what I write.

    Thanks.

    -Lars
     
  23. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #23
    Hey Lars, I responded rather quickly yesterday because I was trying to get out the door, but I read through the whole thread just now. If you're interested in internet programming, you might achieve faster results using PHP. I just thought I would throw that out there. PHP is less strict than Java, and so for people who aren't programmers it presents lots of dangerous opportunities to "do the right thing the wrong way", but it does offer results very quickly, and when use in combination with MySQL can build very sophisticated systems.
     
  24. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #24
    I already have alot of time invested in Java. I am around page 225 of 800 in the book and it isn't that confusing, yet. I think I will keep working with Java because I think that what I want to do is much simpler then writing a tennis game that is on my cell phone. I just want a gui interface to write in IP, User,PASS and save that to a file on the hard drive. Then some FTP and TELNET functions.

    I don't have the time to take a class and the book seems good. Plus you genuis's who write code in your sleep can help me when get stuck or don't understand something which is a big help.

    Thanks for the advice.

    -Lars
     
  25. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #25
    not to detract from your Java experience, but you could also just do a quick Applescript that can take the user ID and Password and send it to a terminal command.

    The plus of using Applescript is the fact that it can be used with XCode's interface builder
     

Share This Page