Java help!

Do you have a technical question that doesn't really fit a specific console? Want some general info on electronics, hacking, making cookies, etc? Here's the place to ask! Go nuts.

Moderator: Moderators

Post Reply
Edutainment
Posts: 423
Joined: Tue Feb 20, 2007 8:33 pm
Location: Ontario, Canada

Java help!

Post by Edutainment »

This is the assignment:

Code: Select all

Sample Input:	Friend	Holiday

Sample Output:
   F                        H
  R R                      O O
 I I I                    L L L
 E E E                   I I I I
  N N                     D D D
   D                       A A
                            Y

I'm struggling. Here's my quickly thrown-together, probably completely wrong code, with inappropriate variable names: http://ziggystardustmedicaldoctor.googl ... amond.java
HazmatB
Posts: 197
Joined: Wed Nov 28, 2007 6:23 pm

Post by HazmatB »

Several problems here:

1: for(int i = 0;i<=arrLength/2;i++){

When the word is say: 5 letters long, then this arrLength/2 = 2.5, and based on your Holiday example, it should be 3.

2: after your first for loop, you reset omg to 1. What you really should do, is have an if statement to check if arrLength/2 = Math.ceil(arrLength/2); (i.e. is the word an odd number of letters), and omg-- if it is, otherwise leave OMG as it is.
Edit: Oh, I just reread the code, and realized that the way you were doing this it makes sense to set it to 1. but you will have to increment omg in the loop.

3. There is an off - by - one error in it as well, and more than likely you will get a index out of range exception.
Edutainment
Posts: 423
Joined: Tue Feb 20, 2007 8:33 pm
Location: Ontario, Canada

Post by Edutainment »

Yea that's the error it was giving me. In the original text, the FRIEND example just ended with two, instead of making the two middle letters the same width. That was what I was doing with this code, so I just let it round up. The teacher said he wasn't gonna be too strict about how we handle words with an even number of characters.
HazmatB
Posts: 197
Joined: Wed Nov 28, 2007 6:23 pm

Post by HazmatB »

Any progress? If you post the updated code, I will give you some more guidance.
vb_master
Moderator
Posts: 4793
Joined: Tue Jun 08, 2004 9:52 pm

Post by vb_master »

I'm fixing the one you originally posted right now. When I'm finished I'll post a commented source.
HazmatB
Posts: 197
Joined: Wed Nov 28, 2007 6:23 pm

Post by HazmatB »

You can post the corrected one... I was going to let him try to figure it out.

Whatever.
vb_master
Moderator
Posts: 4793
Joined: Tue Jun 08, 2004 9:52 pm

Post by vb_master »

HazmatB wrote:You can post the corrected one... I was going to let him try to figure it out.

Whatever.
Screw it.
HazmatB
Posts: 197
Joined: Wed Nov 28, 2007 6:23 pm

Post by HazmatB »

vb_master wrote:
HazmatB wrote:You can post the corrected one... I was going to let him try to figure it out.

Whatever.
Screw it.
I don't really know what this means... I Don't think there is anything wrong with giving it to him, but I just got the impression he didn't want it for free. I didn't mean to piss anyone off.
Edutainment
Posts: 423
Joined: Tue Feb 20, 2007 8:33 pm
Location: Ontario, Canada

Post by Edutainment »

Can't we all just get along? :lol:
project_failure
Posts: 812
Joined: Sun Dec 25, 2005 4:04 pm
Contact:

Post by project_failure »

just make the whole thing with printlines. :lol: just hope the user input is what you want it to be.
Sword_Gun wrote:Klef your asking a whole bunch of videogame players.. for girl advice.. Nice.
It is not junk! It is assorted goods with no current use.
-Project Failure
Post Reply