File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

Can Somebody please help me with my code?

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 84
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

Joined: Oct 02, 2003
Posts: 11955

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

Joined: Aug 03, 2008
Posts: 9462

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

Joined: Oct 02, 2003
Posts: 11955

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:
subject: Can Somebody please help me with my code?
It's not a secret anymore!