Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 6

Java Program to randomlly Nominate for Congress?

I think investors who own the media are the ones who choose who gets on the ballot. So I wrote this Java Program below to choose people at random from the population to run for congress. It takes the Power Ball drawing numbers and maps them to the initials of their First and Last name, and choose their birth dates from the next two number, and finally the congressional district with the last two numbers.

It is far from perfect, but throws in randomness that an investor would have a hard time to control. What do you think (I know the program is sloppy).?

It takes the power ball numbers (with spaces) from the command line.

//PoBaCoN developed by Richard Blythe, Terre Haute,IN 12/26/2013

//Power Ball Congressional Nomination program

import java.util.*;

public class PoBaCoN

{

public static void main(String[] args)

{

int[] number = new int[6];

String powerBallPick = "";

for(int k = 0;k<args.length;k++)

powerBallPick += args[k] + " "

;

System.out.println(powerBallPick);

StringTokenizer lotteryPick = new StringTokenizer(powerBallPick);

int k = 0;

while(lotteryPick.hasMoreTokens())

{

number[k] = Integer.parseInt(lotteryPick.nextToken());

k++;

}

char bChar = '\u0041';

char firstName = (char) ( bChar +(number[0]%27));

char lastName = (char) (bChar + (number[1]%27));

int month = number[2]%13;

int day = number[3]%32;

int Column1 = number[4]%10;

int Column2 = number[5]%10;

System.out.println(35/4);

System.out.print("Initials: " + firstName + " " + lastName);

System.out.print(" Birthday: Month: " + month + " ");

System.out.print("Day: " + day);

System.out.println("Congressional District " + Column1 + Column2);

}//end main

2 Answers

Relevance
  • 7 years ago
    Favorite Answer

    You're doing unnecessary work. You build up a string of the command line inputs then use string tokenizser to break it apart, you don't need to do that: just convert the commend line inputs to ints directly from args

    Like this:

    int[] numbers = new int[args.length];

    for( int k=0;k<args.length; k++ ) {

    ...numbers[k] = Integer.parseInt(args[k]);

    }

  • Leo D
    Lv 6
    7 years ago

    It looks interesting so far, but on top of what @modulo_function said, I think you're allowing too much on the user. There should be more restrictions and it should be more obvious what you are accepting, or it could cause some logic errors.

    For example, how is the user to know what letter counts as the 100th letter? args[0] and args[1] should be parsed as characters, not integers, and they should be restricted from A to Z, nothing else.

    It might also make sense to make a CongressionalCandidate class that has first_initial, last_initial, birthday and district fields and the necessary methods. This seems like something you might be going for :) Please contact me. I might be able to help you more. You can see my email in my profile.

Still have questions? Get your answers by asking now.