• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

First Week In Java, And First Little Program

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, hello I started learning Java this past Sunday, and on Tuesday made my first little program. I know no one that also knows Java (let alone any programming language), and I am learning on my own, so I was hoping I could get some feedback on this first little program. I am looking for any tips on things I might have done wrong, or could do better.

The program is a Random String Generator; it takes user input (type of string, and length), and then generates a string with their requested method. Its extremly simplistic, but had some interesting coding challenges considering I only just did 'Hello World' a few days back xP I actually already have quite a few additions and modifications I want to make to it, but wanted to get a bit of feedback on it otherwise.

The Main Class, RandomStringGenerator.java: http://www.pastebin.com/B0c0h2Nn
InputHandler.java: http://www.pastebin.com/ZexPK9M8
RandomGenerator.java: http://www.pastebin.com/41PtufAE

Please be gentle with me, only got a few days under my belt xD
 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please post your code here inside code tags. Many people don't go to pastebin or similar to look at code.
 
Corey Eacret
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had only used PasteBin because it was a bit of code, but here ya go. Oh and it's a slight bit different than the PasteBin version. I added the ability to choose the first character type, and fixed some minor issues.

Main Class: RandomStringGenerator.java



InputHandler.java


RandomGenerator.java

 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, but those lines are long. I have broken the worst offenders, so you can see how to do it, but they are probably still a bit too long.
Most of your methods are much too long, too. They need breaking into smaller methods.
You have all sorts of magic numbers floating around. Those are numbers which appear out of thin air with no explanation. What is 24? If you mean the length of the array, use the length of the array.
Never use == false or == true. They are poor style and error‑prone. Write if(b)... or if(!b)...
Classes have names starting with CapitalLetters. numGen is a poor choice. So is rNum a poor choice for a variable name. What do the errorCheck arrays mean? Their names are confusing at best.
Only ever use \r or \n if you have been told to use the CR and LF characters. Use String.format and the %n tag instead.
You need more whitespace in your code.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Corey Eacret wrote:The program is a Random String Generator; it takes user input (type of string, and length), and then generates a string with their requested method. Its extremly simplistic, but had some interesting coding challenges considering I only just did 'Hello World' a few days back xP I actually already have quite a few additions and modifications I want to make to it, but wanted to get a bit of feedback on it otherwise.


OK, here's a biggie:
When you're starting out. Start small. Your spec contains all sorts of "bells and whistles" that it really doesn't need - at least to start with.

My advice:
1. Try generating a string of random letters to start with: That only requires the user to enter one thing - length - so you can concentrate on the problem you're trying to solve, not all that user input. You can add all the other "nice to haves" in after you've got THAT working.

2. Separate all that user input stuff from the business of creating a random String - and the easiest way to do that is to create a separate class whose ONLY business is to create random Strings, given a length.

3. Use the Random class (java.util.Random ←click); not Math.random() - it's much simpler.

Just to get you started; this is what my class definition would look like if I was trying it:Do you see how this might help?

Now you have a class that is ONLY concerned with generating strings. And like I say: start out simple. Make sure it can generate a String of random letters (or digits) first; then add extra stuff. And add it incrementally - don't add six new rules for fancy prefixes or suffixes in straight off the bat.

Deal with one thing at a time.

HIH

Winston
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic