File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic

Can Somebody please help me with my code?

 
David Barry
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please put your code inside the [code] tag so that it is more readable
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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);
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic