Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# Java 1b

Tony VanHorn
Ranch Hand
Posts: 74
I am having some difficulty with a couple of questions or nitpicks on Java 1b. The questions are as follows, along with my line of thinking. Any suggestions or proddings in the right direction would be greatly appreciated.

1. What happens if you use a name that has the perfect length so you end up writing to the 80th character? ( like "tony" or "marilyn" )

It seems to me that it wouldn't cause a problem if I "write" to the last character. I am adding the space to be included with the name and then taking the length of the name and dividing it into the number of characters allowed. If my name is 8 characters long (including the space) then it writes the name to the line 10 times before returning to the next to start over. Where am I wrong in the logic, I have been struggling this one for a while now.

2. Try to start your for loops at zero.

This I understand for the most part. But in my code, the only way I saw to test it using the counter instead of a second variable was to see if it was divisible by the number of names per line. So, it would start with 1(first name) and then divided by number of names per line (lets say 8 names per line). 1 / 8 has a remainder of 1 which does not equal zero, so it would continue. It would go until the 8th name, 8 / 8 remainder is 0, which would give the new line. If I started my counter with zero, 0 / 8 remainder is 0, which would print one name and then put in a new line and continue as expected. Again, I had a hard time figuring out how to do this in the first place, now I am struggling to figure out how to start to my loop at zero and still count the number of names on the line (without putting i+1 into the boolean). Again, any insights on this one would be helpful. Thanks in advance

Tony

[ January 11, 2007: Message edited by: Pauline McNamara ]

Pauline McNamara
Sheriff
Posts: 4012
6
Hi Tony, welcome to the pain and pleasure.

Great questions, thanks for posting them. Going out of order, I'll start with the second one.
2. Try to start your for loops at zero.

This I understand for the most part. But in my code, the only way I saw to test it using the counter instead of a second variable was to see if it was divisible by the number of names per line. So, it would start with 1(first name) and then divided by number of names per line (lets say 8 names per line). 1 / 8 has a remainder of 1 which does not equal zero, so it would continue. It would go until the 8th name, 8 / 8 remainder is 0, which would give the new line. If I started my counter with zero, 0 / 8 remainder is 0, which would print one name and then put in a new line and continue as expected. Again, I had a hard time figuring out how to do this in the first place, now I am struggling to figure out how to start to my loop at zero and still count the number of names on the line (without putting i+1 into the boolean).

You're right on track with how you figure out when to insert a line break. And you've been experimenting around with the counter thing, that's a good way to come around to a solution. Looking specifically at this part:

If I started my counter with zero, 0 / 8 remainder is 0, which would print one name and then put in a new line and continue as expected.

Think about the order that you're doing things there.
You print out a name.
You check the remainder to see if it's line break time.
You get a line break.
You print out a name.
You check the remainder to see if it's line break time.
No line break.
You print out a name.
You check the remainder to see if it's line break time.
No line break.

etc.

How about another experiment: What happens if you change the order that you do things (especially where printing out a name and checking for a line break are concerned)?

(Bonus hint: maybe take another really close look at what the solution should look like on the assignment page.)

Gotta run right now, will get back to your first question in a bit...

Pauline McNamara
Sheriff
Posts: 4012
6
Oh, and you see that I removed the code. We don't allow code in the Cattle Drive so as not to spoil the fun for folks trying to figure it out on their own.

Hang in there!
Pauline

Katrina Owen
Sheriff
Posts: 1367
18
Hi Tony,

I also had a bit of trouble with the "what happens if you write to the 80th character" part, especially since I am working on a unix system, and my line length is whatever length my terminal (in X) or console window has. If I change the size of my terminal, the line length changes. So I didn't see what was happening.

Technically, if you try using a name with the perfect length, you should see some wrapping going on - i.e. the last name (which *should have* fit, doesn't).

Hm... I seem to have trouble expressing myself clearly. Let me know if this makes no sense whatsoever, and I will have a second go.

Tony VanHorn
Ranch Hand
Posts: 74
Sorry about posting the code, I wasn't for sure if I could actually explain myself well enough with out it. So I figured question 2 out, thanks Pauline. I guess it would help if I looked a little closer at the assignment and what the solution looked like. That made all the difference. As for question 1 Katrina, I think I am still a little confused. I have tried it with a multitude of different names and I never get the lines to wrap. If you could give it a second go and try to explain again I would appreciate it. Thanks to both of you for your help.

Tony

Okay, I found that names of 2 letters and 8 letters actually leave enough space on the end to actually place another name on the line, however I am still struggling trying to figure out how exactly to combat that. I have to work this evening, so I will mull it over and try again when I get home.
[ January 11, 2007: Message edited by: Tony VanHorn ]

Tony VanHorn
Ranch Hand
Posts: 74
Okay, so I got back from work and decide to sit down and figure the first question out. As I start to look over it again I thought that I might take a look at what is happening in the solution displayed with the problem so as not to make the same mistake I had. To my surprise, and dismay, I find that the solution ends with the same result as mine. It has left enough room at the end of an 80 character wide window to actually display the name one last time. So now I am totally confused. I don't know how to come up with the same results by doing it any other way. I take the name and then add the space to it. I then divide the screen length by the length of the name (plus the space) and that is the amount of times that it should be written to the screen. Again, it only does not print the final name when it is 2 char long or 8 char long (just like the solution). I am alittle lost to say the least. Any help would be appreciated.

Tony

Pauline McNamara
Sheriff
Posts: 4012
6
Hi Tony,

Those 80 characters are kind of tricky. Here's the thing. On some operating systems if you write all the way to the 80th character on a console that is 80 characters wide, it will insert a line break automatically for you. This results in a double line-break, which we don't want.

(The tricky part is that this behavior doesn't show up on *all* systems, making it hard to understand that nitpick when you can't see the problem.)

So the challenge is actually to *not* write all the way to the 80th character. There are really complicated solutions and really simple ones. We're looking for a realy simple one.

Pauline McNamara
Sheriff
Posts: 4012
6
Originally posted by Katrina Owen:

Technically, if you try using a name with the perfect length, you should see some wrapping going on - i.e. the last name (which *should have* fit, doesn't).

Er, actually, it's that double spacing that happens sometimes, not wrapping.

A name and space combination that adds up to, say, 8 spaces (like marilyn plus a space) *does* fit perfectly on a console 80 characters wide. And that's when some systems will throw in a line break in addition to the one your program does.

Maybe this helps Tony? Just yell if any of this doesn't make sense. Don't want no cowpokes yanking out *all* thar hair over this derned nit!

Tony VanHorn
Ranch Hand
Posts: 74
ahh, so is this why some people have switched over to using a unix system (well this and some other reasons I am sure). That makes sense and I understand it. All I need to do is change my numerator to 79 instead of 80, right? Well thats what I will do and then send it back in to get nitpicked, thanks for the help guys, hopefully I will start seeing some off the changes as I go along and won't need as much help.

Tony

Katrina Owen
Sheriff
Posts: 1367
18
Originally posted by Tony VanHorn:
ahh, so is this why some people have switched over to using a unix system (well this and some other reasons I am sure).

LOL

I was on unix way before I started learning java, for many of those other reasons <grin>

Sorry for being confusing :/ I never actually got any wrapping myself so I had to guess at what the solution was

Right on for figuring it out, Tony!