Dumping Ground for Coding Issues

Harder, Better, Faster, Stronger.
Post Reply
Message
Author
User avatar
Agent Bert Macklin
Posts: 1197
Joined: Mon Sep 26, 2011 3:20 am

Dumping Ground for Coding Issues

#1 Post by Agent Bert Macklin »

SInce I am a NEWB, I figured I'd create this thread for issues I or others may have relating to coding.

My current courseork involves loops in Java. The story of my life is such that once I change something I get hit with a roadblock.

"You want to be a programmer?"
"I do!"
"Well, here's a assignment that will drive you nucking futs."

Alright. My task is to use a single while loop to print out even and odd numbers between 50 and 100. The even numbers will be labled and printed on the same line while the odd numbers will follow the same format. For example:

Even numbers between 0 and 50: 50, 52, 54, 56, 58, ... , 100
Odd numbers between 0 and 50: 51, 53, 55, 57, 59, ... , 99

I have nailed the code to display the numbers on the same line but it only works properly when I disable one portion of the loop. If I leave all of the code intact, the output is as follows:

50
51, 52
53, 54
.
.
99, 100

Here's the code with some personal annotations.

Code: Select all

public static void main(String args[])
{
        //start at 50
        int evenNumber = 50;
        int oddNumber = 50;

        //execute the commands when both the even and odd numbers are less than or equal to 100
        while ((evenNumber <= 100) && (oddNumber <= 100))
        {
            if (evenNumber % 2 == 0)
            {
                System.out.print(evenNumber + ", ");
            }

            evenNumber++;
            
            //if I disable the if statement for the odd number, everything works fine. The same can be said if I disable the if statement for            
            //the even number and leave hte code for the odd
           
            if (oddNumber % 2 != 0)
            {
                System.out.println();
                System.out.print(oddNumber + ", ");
            }

            oddNumber++;
        }
}
Now, I thought about storing the values in a string and just printing those, but after many attempts I couldn't get it to work. Any advice? Please do not offer code because I won't learn that way.

User avatar
Flagg
Posts: 2123
Joined: Mon Sep 26, 2011 2:45 am

Re: Dumping Ground for Coding Issues

#2 Post by Flagg »

Kill yourself.
CUNTS! FOR! EYES!
The Liberal Hate Machine

User avatar
adr
Moon Prism Power, Make Up!
Posts: 1462
Joined: Fri Sep 07, 2012 10:59 pm

Re: Dumping Ground for Coding Issues

#3 Post by adr »

Yeah, storing the things in a string is what you want to do. Instead of printing it, just do a += on the string to add the portion to it. Then, after the loop, print both strings.

There's a trick you can do with the commas btw, but I'll let you play for a bit before saying it.
In the name of the moon, I will punish you!

Manus Dei
Posts: 292
Joined: Sun Oct 09, 2011 9:56 am
Location: a few seconds of arrhythmic thrusting

Re: Dumping Ground for Coding Issues

#4 Post by Manus Dei »

perl is weird
Ralin wrote:Finally I realized that when Walker fights the Satanic ponies I need to mention how his 'lower horn' is glowing and sparkling as it draws in and focuses Equestria's ambient magic.

Manus Dei
Posts: 292
Joined: Sun Oct 09, 2011 9:56 am
Location: a few seconds of arrhythmic thrusting

Re: Dumping Ground for Coding Issues

#5 Post by Manus Dei »

Manus Dei wrote:perl is weird
what with the boners
Ralin wrote:Finally I realized that when Walker fights the Satanic ponies I need to mention how his 'lower horn' is glowing and sparkling as it draws in and focuses Equestria's ambient magic.

Manus Dei
Posts: 292
Joined: Sun Oct 09, 2011 9:56 am
Location: a few seconds of arrhythmic thrusting

Re: Dumping Ground for Coding Issues

#6 Post by Manus Dei »

Manus Dei wrote:
Manus Dei wrote:perl is weird
what with the boners
can't get enough (seriously)
Ralin wrote:Finally I realized that when Walker fights the Satanic ponies I need to mention how his 'lower horn' is glowing and sparkling as it draws in and focuses Equestria's ambient magic.

User avatar
Agent Bert Macklin
Posts: 1197
Joined: Mon Sep 26, 2011 3:20 am

Re: Dumping Ground for Coding Issues

#7 Post by Agent Bert Macklin »

I got it to work!

Code: Select all

public class EvenOdd
{
    public static void main(String args[]) {
        //start at 50
        int number = 50;


        //declare strings to hold the even and odd numbers
        String evenNumbers = "";
        String oddNumbers = "";

        //execute the commands when both the even and odd numbers are between 50 and 100
        while (number <= 100)
        {
            if (number % 2 == 0)
            {


                //when the even number is 100, remove the comma after the number. this makes the output prettier
                if (number == 100)
                {
                    //assign 100 to a string
                    evenNumbers = evenNumbers + "" + number;
                }

                else
                {
                    //assign even numbers to a String
                    evenNumbers = evenNumbers + "" + number + ", ";
                }
            }

            else
            {


                //when the odd number is 99, remove the comma after the number. this makes the output prettier
                if (number == 99)
                {
                    //assign 99 to a string
                    oddNumbers = oddNumbers + "" + number;
                }

                else
                {
                    //assign odd numbers to a String
                    oddNumbers = oddNumbers + "" + number + ", ";
                }
            }

            //continue counting
            number++;
        }

        //print the result to the user by getting the length of the strings holding the even and odd numbers

        evenNumbers = evenNumbers.substring(0, evenNumbers.length());
        oddNumbers = oddNumbers.substring(0, oddNumbers.length());

        System.out.println("Even numbers between 50 and 100 : " + evenNumbers);
        System.out.println("Odd numbers between 50 and 100  : " + oddNumbers);
    }
}

Now it's time to do this with methods, which is how every program in the future should be written in the class. I've been using methods since the second week of class, so it will be a no brainer.

User avatar
adr
Moon Prism Power, Make Up!
Posts: 1462
Joined: Fri Sep 07, 2012 10:59 pm

Re: Dumping Ground for Coding Issues

#8 Post by adr »

Agent Bert Macklin wrote: evenNumbers = evenNumbers.substring(0, evenNumbers.length());
oddNumbers = oddNumbers.substring(0, oddNumbers.length());
What happens if you remove these two lines?



The tip with the commas btw: it is natural to think of putting them after a number since that's how it looks.... but there's another way to think of it which is simpler to code.
A comma (and space, the string is ", "), goes before all items in the list, except the first one.
Consider that for a bit and see if you can change the code a bit to incorporate it (the spoiler is just the other way I think of it, it isn't the final answer, so feel free to look if you want). Then I'll show you how I'd write it too later today.
In the name of the moon, I will punish you!

User avatar
Agent Bert Macklin
Posts: 1197
Joined: Mon Sep 26, 2011 3:20 am

Re: Dumping Ground for Coding Issues

#9 Post by Agent Bert Macklin »

adr wrote:
Agent Bert Macklin wrote: evenNumbers = evenNumbers.substring(0, evenNumbers.length());
oddNumbers = oddNumbers.substring(0, oddNumbers.length());
What happens if you remove these two lines?
Everything works as its supposed to. Hmm. I didn't see that coming but now I know why.

User avatar
Agent Bert Macklin
Posts: 1197
Joined: Mon Sep 26, 2011 3:20 am

Re: Dumping Ground for Coding Issues

#10 Post by Agent Bert Macklin »

I;m not having any issues, but I just wanted to pop in and say that I am in lvoe with using OSX's terminal for coding. I got my hands on Sublime Text to use as an editor. Coding this way is not as efficient as using my go-to IDE IntelliJ, but it's still pretty fun.

User avatar
Agent Bert Macklin
Posts: 1197
Joined: Mon Sep 26, 2011 3:20 am

Re: Dumping Ground for Coding Issues

#11 Post by Agent Bert Macklin »

I'm not having any coding issues, but I just wanted to pop in and say that what I'm learning in Java at this moment in class is far more interesting than the basics. We've covered the basics of OOP and are currently learning about exception handling and reading and writing files. It's pretty great.

I've also decided that my first personal project will be an application that allows camera owners to get a variety of information about their cameras. For instance, shutter count, battery life, copyright info, etc. I am doing this because my camera brand of choice (Canon) does not have shutter count information in the EXIF data. I have to resort to online applications or standalone applications. It's a pain in the cock, for sure.

User avatar
adr
Moon Prism Power, Make Up!
Posts: 1462
Joined: Fri Sep 07, 2012 10:59 pm

Re: Dumping Ground for Coding Issues

#12 Post by adr »

OOP was overrated for a while, called the best thing ever that solves all problems (around the time Java was created...), but nowadays I think it is underrated. Sure, it doesn't solve everything, but it does solve a bunch of things.

Exceptions just kinda rock, I love them.

Are you going into more theory behind OOP or just the basics of use? Two interesting things about it I like is for one, the Liskov substitution principle reminding you when to use inheritance (if you can't substitute child class for parent, it generally isn't appropriate to inherit) and just the interesting realization that comes off that: the most code reuse from OOP does *not* come from methods being inherited to base classes like a lot of newbies assume, but actually from other functions taking the base class. *Those* functions get to be reused for your children because you can substitute them for the parent and still call it.
In the name of the moon, I will punish you!

User avatar
Agent Bert Macklin
Posts: 1197
Joined: Mon Sep 26, 2011 3:20 am

Re: Dumping Ground for Coding Issues

#13 Post by Agent Bert Macklin »

I think we'll be doing more than the basics for OOP. We've talked about parent classes, child classes, and are currently on abstract classes. I've not yet looked ahead in the book to see what else is covered, but I do know that I will need to learn more on my own outside of class if I am to be successful in this field.

User avatar
Agent Bert Macklin
Posts: 1197
Joined: Mon Sep 26, 2011 3:20 am

Re: Dumping Ground for Coding Issues

#14 Post by Agent Bert Macklin »

GUI in Java is pretty cool. I like how setting things up properly is reminiscent of a stage play. It's awesome. Pain in the ass to type all of that stuff, but awesome nonetheless.

User avatar
Agent Bert Macklin
Posts: 1197
Joined: Mon Sep 26, 2011 3:20 am

Re: Dumping Ground for Coding Issues

#15 Post by Agent Bert Macklin »

Rant: WHy the hell does my school teach Java for two classes and then switch to C++ for data structures? It's difficult going from a newer language to an older. I'm getting the hang of OOP with C++ and classes, but it's difficult.

I willl say this: it feels like I am coding when I'm using C++.

User avatar
adr
Moon Prism Power, Make Up!
Posts: 1462
Joined: Fri Sep 07, 2012 10:59 pm

Re: Dumping Ground for Coding Issues

#16 Post by adr »

C++ was started before Java, but both are still actively changing so modern C++ isn't really much older than modern Java....

But the reason is probably that C++ offers more capabilities to really get into the structures' details than Java does. A lot of schools make that transition.
In the name of the moon, I will punish you!

Post Reply