wood burning stoves 2.0*
The moose likes Cattle Drive and the fly likes Cattle Drive Assignment 1-B not outputing the right number of times Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Cattle Drive Assignment 1-B not outputing the right number of times" Watch "Cattle Drive Assignment 1-B not outputing the right number of times" New topic
Author

Cattle Drive Assignment 1-B not outputing the right number of times

Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
I've started doing the Cattle Drive. I completed Assignment 1-A without a problem, but I am having some difficulty getting the correct output for Assignment 1-B (
http://www.javaranch.com/hundred.jsp
-print screen for output).

I am able to get the correct formatting, but for some reason it is printing "name" 104 times and not the 100 times as required. I am not sure why the printing does not stop when "b" reaches 100. I have tried putting the "b++" in different places, but I have never gotten the correct output.

The command line I type is java Hundred Gertrude (so args does become Gertrude)


Code removed as requested below


Thanks
Nikki Agr
Greenhorn

Joined: Nov 09, 2008
Posts: 13
The problem is . Add an additional condition to that while loop to test if the value of a is less than 100. Also, you could replace that for loop with a while loop.

Cheers!
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Ok, I think I got figured it out. I also think I posted this problem in the wrong forum.

My solution was to add:
if(a==100) break;

just before:
System.out.print(name);

Not sure if this was the best solution to the problem, but it works so I'm going with it.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

kindof sloppy, don't you think?

try moving the a++ to the outside of the inner loop.... right where you put the if a == 100 break business.....

see if that works.


When you do things right, people won't be sure you've done anything at all.
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Janeice DelVecchio wrote:kindof sloppy, don't you think?

try moving the a++ to the outside of the inner loop.... right where you put the if a == 100 break business.....

see if that works.


It might be sloppy but it works.

Your solution still displays 104 times the name. The script is still in the outter loop for checking if printing has reached 100. Sure, once it checks after printing the last line, it sees that "a" is now bigger than 100, it stops the loop and the script ends. However, if I don't include the break at 100 in the inner loop, I will print the extra 4 names because it hasn't finished the print loop.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

I see it now... there's definately a way to do it without a break.


Need a hint?
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Janeice DelVecchio wrote:I see it now... there's definately a way to do it without a break.


Need a hint?


The more I look at the script, the less I like the "break" in it.
If you see another way out of it, without a break, I'll find it. Time to hit the books again to see what I'm missing, so I'll pass on your hint for now. However, you may be hearing from me again in a few hours if I find myself doing this
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

All right, let me know
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

Hi All,
I do not want to sound mean but I do not think codes about homeworks (cattle drive) should be posted anywhere. I think this will allow others who are just starting up to see a working code. Should someone remove the code for others?

Garry


star chaser..
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Garry Ba wrote:Hi All,
I do not want to sound mean but I do not think codes about homeworks (cattle drive) should be posted anywhere. I think this will allow others who are just starting up to see a working code. Should someone remove the code for others?

Garry


I thought about not putting my code in to my original post, but then I thought how is anyone going to see and understand what I'm talking about without the code? It becomes kind of a double edged sword I think. I can always edit the original posting and delete the code if necessary.

The whole thing becomes kind of Shakespearean "To put or not to put, that is the question, whether it is better to ....."
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Ian, go ahead and edit your post and remove your working code...

No need to rock the boat
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

Ian, Welcome to javaranch

Since I caught some of your attention, can I request for you guys to post on the "What editor do students use for assignment ". You too Janeice. Out of curiosity.


Thank you,
Garry
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

no problem, I replied to your thread.

Janeice

P.S. Ian -- do you need a hint yet?
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Garry Ba wrote:Ian, Welcome to javaranch

Since I caught some of your attention, can I request for you guys to post on the "What editor do students use for assignment ". You too Janeice. Out of curiosity.


Thank you,
Garry

Thanks for the welcome. I responded to your request.

Janeice DelVecchio wrote:
P.S. Ian -- do you need a hint yet?

I think I'm on the right track, trying to use a Boolean operator to get me out of the loop cleanly.
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
@Janeice

Don't need your hint anymore

Found another way to do it without using a "break" command. When I first redid the code, I put a new "cheat" to get out of the loop again without realizing it. But after I saw the new cheat in the code, I realized the new cheat I used was actually the solution, so a little modifying here and there and all was good in the hood. A clean exit stage left.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11



Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17
Ian Lubelsky wrote:@Janeice

Don't need your hint anymore
A clean exit stage left.


Woo!

PS: This is why we'd rather not have you post actual code... that feeling of getting it is invaluable when learning.
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Katrina Owen wrote:
Ian Lubelsky wrote:@Janeice

Don't need your hint anymore
A clean exit stage left.


Woo!

PS: This is why we'd rather not have you post actual code... that feeling of getting it is invaluable when learning.


You're right. I did have a big old cheshire grin when I did figure it out, as my last post showed.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17
I remember in college spending *weeks* in class not getting something. I tried so hard to figure out what the heck they meant. And then I read an article and in a few moments everything just... crystallized. It was like looking at a stereogram and suddenly seeing the 3D image. It was unbelievable.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
On the matter of posting your code for an assignment, it is better to use pseudo code such as "method of counting number of times printed and stopping at 100 goes here" rather posting actual code. If you are struggling mightily and need to discuss your actual code with someone, it is best to email a sample to your nitpicker, as Marilyn had me do on several occasions when I just couldn't get past a stumbling block. The reason for this is that we do not want to deprive anyone of the joy of the frustration of figuring this stuff out for themselves. Oh, and just for the record, "It might be sloppy, but it works!" will leave you open for a savage nitpick. Remember, we're not just looking for anything that works, but code that is simple yet elegant in design, easy to read and understand, and follows the style guidelines for code laid out by the trailboss. Happy Roping, Cowpokes!
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17
Carol Murphy wrote:[...]the joy of the frustration of figuring this stuff out for themselves.


Beautifully said
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Carol Murphy wrote:Oh, and just for the record, "It might be sloppy, but it works!" will leave you open for a savage nitpick.


+1 on that!

He said that and I was like.....

... I mean when I said it was sloppy, I really just wanted to say it was not a good habit to get into...... and kindof wrong to learn that way. He didn't take the hint, or I guess he did, eventually.

Just because it works doesn't mean it's good.
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Carol Murphy wrote:Oh, and just for the record, "It might be sloppy, but it works!" will leave you open for a savage nitpick. Remember, we're not just looking for anything that works, but code that is simple yet elegant in design, easy to read and understand, and follows the style guidelines for code laid out by the trailboss. Happy Roping, Cowpokes!


I knew as soon as I hit the Submit button when I wrote it may be sloppy, but it works, was just opening me up for all kinds of comments. One of the reasons I did go back and try to re-work the code to make it cleaner because I'm not really the kind of person who likes to have it just work and be done with it. I prefer having it work the way it is supposed and not in some half-ass manner.

At the moment I don't have a nitpicker to email to. I"m still deciding if I want to get the full nitpick advantage. I did take a look at paying, but I saw I could only pay with PayPal. I don't have a PayPal account nor do I want one. I would like to know if there is any other way I can pay with a credit card other than through PayPal.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

I have a jar with quarters.... one day when it gets full enough it will be my cattle drive fund.

Paypal isn't bad... you can use a credit card or your checking account. I have one just for online purchases. I find it safe and convenient.... the merchant never gets your information, just your email.
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Janeice DelVecchio wrote:I have a jar with quarters.... one day when it gets full enough it will be my cattle drive fund.

Paypal isn't bad... you can use a credit card or your checking account. I have one just for online purchases. I find it safe and convenient.... the merchant never gets your information, just your email.


At least you have American quarters, I only have Canadian ones.

I use plenty of services that I pay for online to make purchases, but PayPal is the only one I have never used nor have ever really trusted for some reason.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Ian, if you want to fully do the Drive, I'm sure you can send a check to the trailboss via snail mail. It will just take longer to get you hooked up. I don't have a paypal account either, so I totally understand your position. The nitpicks are extremely valuable in terms of getting the most out of these assignments, but you are welcome to do them without the benefit of the nitpicking. When I did the Drive, I started with no programming experience whatsoever, so when I wrote a program that worked like it was supposed to, I was stoked. Then I submitted it for nitpicking......... and discovered how much I didn't know about programming! I was trying to learn Java from books, but if I hadn't stumbled onto the Cattle Drive I think I would have given up. I found it extremely helpful and the entire staff of JavaRanch are an invaluable resource. They truly rock.
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
@Carol

I gave in finally to sign up for the drive and paid via Credit Card, without having to use PayPal.

Just like you when you started, I know next to nothing in programming. The last time I did any kind of "programming" I think it was back with BASICA, if that can give you any idea of how long it has been since I've even thought about writing a code for something.

I've already done the first 3 assignments and even though they work, I know I'd be better off if someone else took a look at my code to tell me if I am doing it the right way and not just in a way to get the script to run properly. I think it will be one of the few times in my life where I will look forwarding to being nitpicked on.

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Ian, I'm so glad you signed up! I have an intermediate understanding of Java, and I'm still getting torn apart.

Did you get confirmation from Katrina yet?
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
At least you have an intermediate understanding of Java. I only have 4 chapters worth of understanding.

Still waiting on my confirmation.
Did you get it yet?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

I took a class in college where they were more interested in it "just working" and you commenting the bejeezus out of it than good coding form. This is good for me because I'm interested in what people really think of the style than whether or not it just works.

I'm on my second assignment (hundred b)... and I've already learned a lot. I'm waiting for it to come back..... of course it didn't take me long for the first one. I just started last night.

I'm sure it won't be long before your confirmation goes through. It may have already cause your name is on the assignment log thread....
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Janeice DelVecchio wrote:I'm on my second assignment (hundred b)... and I've already learned a lot. I'm waiting for it to come back..... of course it didn't take me long for the first one. I just started last night.


Guess you won't be using my original code from this thread for 1-B, which started this whole thread off. I went from trying to figure out a way to make it work without "cheating" to , when I finally did.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

I had the code before.... I save the code that I help people with in Beginning Java, because I like to compile and verify I'm getting the same issues, and verify that they're posting real code (which I had no idea was against the rules in this neighborhood).

I didn't use your algorithm... I came up with my own (after much of my own might I add). She emailed me this morning and said it wasn't nitpick worthy... so I had to change it. I'm sure you will too
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
You made the program to simple? Is that what made it not nitpicking worthy?

How long did you have to wait to get your confirmation after paying?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Ian Lubelsky wrote:You made the program to simple? Is that what made it not nitpicking worthy?


**sigh** it just didn't make the grade
I thought it was SO GOOD!!! And Katrina sent it back and said no. Nothing gets by her and her nitpick-sense.....



I started my assignments last night. We signed up on the same day I think.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17
Ian, I'm still waiting for paul's confirmation on your sign up. It usually doesn't take long, but every once in a while he gets stuck in the boonies where the intertubes don't work, or his inbox gets flooded and he has to hire elves to help bail. I'll ping him to see if it slipped under a pile of mail so we can get down to the serious business of abuse!
Ian Lubelsky
Ranch Hand

Joined: Feb 10, 2010
Posts: 49
Thanks Katrina.

If you want, I can send you a PDF print out of the receipt if that will speed things up a bit.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17
Sure, send it over to me along with assignment 1a!

nitpick at javar... etc
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Cattle Drive Assignment 1-B not outputing the right number of times