Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Can Somebody please help me with my code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Can Somebody please help me with my code?

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
This is the code for the random Password generator I am trying to write. If anybody runs this code, they will see the problems I am having. Can somebody please help me see what is wrong with this code? Thanks in advance.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11168
    
  16

Hi David, and welcome to the JavaRanch!

We're glad you're here, and folks are delighted to help. However, there are some things you can do to make life easier for others to help you.

Please take a few minutes and browse around this page. It will give you some tips on the best way to present your problem.

One of which is to actually tell other what the problem is. If you say "I was expecting A, but got B", often someone can skim your code and focus in on the problem. A lot of folks will read your post and say "Too much work to do all that, I'll skip it and move on".

The other main thing is to please use the 'code' tags. there are little buttons that you can use to insert special 'tags' that you then paste your code between. This preserves the formatting and makes it much easier to read.

I'll try and edit your post to have those, but if you could please post exactly what your problem is, you're more likely to get the help you need.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Please put your code inside the [code] tag so that it is more readable


SCJP 5.0 and now fighting with ExtJS
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Hi David, welcome to javaranch.

First of all you code has a lot of redundancy. You could have replaced your long code with this type of code



to get a similar result. Also when you add char values like this

System.out.println(firstChar + secondChar + thirdChar);

Then they are added as INTs instead of characters. So you should try this

System.out.println(""+firstChar + secondChar + thirdChar);

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11168
    
  16

to elaborate on Ankit Garg's post...

When java sees something like "firstChar + secondChar + thirdChar" it says

"hmm... what are these things on either side of the '+' character?"

In this case, they are char variables, which java believes are actually numeric values. What does it mean to add two numeric values? you calculate the sum. So, I would expect you are calculating some big sum and printing garbage.

The suggested solution is a little different. Java sees the firs thing is a String (albeit an empty one). It then says "how do I add a String and a number?" The answer is it converts the number to a string, then concatenates them together. if you have a whole series (as in your case), it figures out what to do with the first pair. Then, it takes the result of that (which is a String), and figures it out all over again with the next value, and keeps working its way down the list.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Somebody please help me with my code?
 
Similar Threads
What am I doing wrong?
operator cannot be applied to ints.
need help with calculation in my spreadsheet array
random number
random.nextInt + If statements