File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can someone help me with the code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone help me with the code?" Watch "Can someone help me with the code?" New topic
Author

Can someone help me with the code?

Maggie Chan
Greenhorn

Joined: Jan 13, 2012
Posts: 3
I wrote a code as below, but it always shows error....I don't know how to fix it...Can someone help me correct it?

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Please TellTheDetails. Copy/paste the exact, complete error message, and indicate clearly which line is causing it.

Also, UseCodeTags so your code will be readable.
Maggie Chan
Greenhorn

Joined: Jan 13, 2012
Posts: 3
Jeff Verdegan wrote:Please TellTheDetails. Copy/paste the exact, complete error message, and indicate clearly which line is causing it.

Also, UseCodeTags so your code will be readable.


I'm trying to split the key and the value within an array and print out the key as well as the corresponding value. Since the the length of the keys are different, it turns out to be not in alignment for the values, so I'm trying to write a code to let the values be in alignment even though the corresponding names' length are different. But I was stock with a problem as indicated in the pictures below:



[Thumbnail for java_crop.JPG]


[Thumbnail for java_error.JPG]

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Jeff Verdegan wrote:Please TellTheDetails. Copy/paste the exact, complete error message, and indicate clearly which line is causing it.

Also, UseCodeTags so your code will be readable.


Dear Maggie, perhaps you didn't realise, but again, please read How To Ask Questions On Java Ranch and PostTextNotScreenshots. My data connection is being horrendously slow and downloading your pictures to help try debug your code really, really is not very appealing when you could just copy paste your code directly in here and used code tags. It's not really good idea either if I need to type out all your code by hand to be able to test it; we don't need mistakes in my typing to interfere with your own problemss triggering more error warnings.
Maggie Chan
Greenhorn

Joined: Jan 13, 2012
Posts: 3
Noam Ingalls wrote:
Jeff Verdegan wrote:Please TellTheDetails. Copy/paste the exact, complete error message, and indicate clearly which line is causing it.

Also, UseCodeTags so your code will be readable.


Dear Maggie, perhaps you didn't realise, but again, please read How To Ask Questions On Java Ranch and PostTextNotScreenshots. My data connection is being horrendously slow and downloading your pictures to help try debug your code really, really is not very appealing when you could just copy paste your code directly in here and used code tags. It's not really good idea either if I need to type out all your code by hand to be able to test it; we don't need mistakes in my typing to interfere with your own problemss triggering more error warnings.



I'm pretty new here but thanks for your nice tips! I've added code tags above and would you please look at it again and hopefully you can detect what exactly goes wrong there... Thanks!
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Hmm, your code tags seem to not be working: try hitting the "Code" button to insert the tags, then paste all your code in between the tags, seems to be easier to get it right that way. Also, thank you for adding the comments in your code. I was trying to figure out what you were doing with the last two for-loops. I'm curious though: why do you need the length of the key names?

I'm a very very new and green programmer myself, so I'm going to say that I'm guessing at the problem and can't say for sure where it is or what it is, but from your earlier screenshot, it seems that somehow the values to be paired with your keys in the hashmap are not being entered into the map, and so it returns a null value.

Also will someone else please look at this bit here and tell me if I'm right in thinking that's a logical error there? This bit of code seems hinky; if it does what I'm thinking it does that no wonder the "cannot find symbol" error crops up.



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The code is now readable, but I don't see the exact, complete error message copy/pasted. It needs to be text, not a screen shot.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Noam Ingalls wrote:I'm a very very new and green programmer myself, so I'm going to say that I'm guessing at the problem and can't say for sure where it is or what it is, but from your earlier screenshot, it seems that somehow the values to be paired with your keys in the hashmap are not being entered into the map, and so it returns a null value.


The only way the value isn't present is if the key isn't either. If she has called put(k, v) then both k and v are in the map. If she hasn't, then neither one is, and she'll get null as the result of calling get(k).

Also will someone else please look at this bit here and tell me if I'm right in thinking that's a logical error there? This bit of code seems hinky; if it does what I'm thinking it does that no wonder the "cannot find symbol" error crops up.





There's nothing hinky there. As long as max and map1 are declared before that loop, it's syntactically fine. It will find the longest key in the map. Not sure if that's what she's going for, or what the purpose in doing that would be, but that's what it does.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

Maggie Chan wrote:I'm pretty new here but thanks for your nice tips! I've added code tags above and would you please look at it again and hopefully you can detect what exactly goes wrong there... Thanks!

The problem is your
System.out.println(s + "\t" + map1.get(tmp));
line.
It references two variables (s and tmp) which are not in scope because the previous loop (which defines them) has already ended.

Defining them both in the main() method body will solve the problem, but I think you first need to ask yourself what that print statement is supposed to be doing.

Adding some comments to your code might help too.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone help me with the code?