Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone help me with the code?

 
Maggie Chan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
java_crop.JPG
java_code
[Thumbnail for java_crop.JPG]
java_error.JPG
java_error
[Thumbnail for java_error.JPG]
 
Noam Ingalls
Ranch Hand
Posts: 60
Chrome Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
Chrome Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10232
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic